views:

114

answers:

1

For the Unicode version experts:

Is it possible to define a ansistring type that reflects the current OEM encoding ?

(assuming that ansistring without codepage identifiers reflect ansi codepage)

It's half a joke question, and half serious:

It would simplify my port of the CRT unit to Unicode greatly (making it nice and typed)

+6  A: 

It seems you should be able to use CP_OEMCP just as you can use CP_ACP to get the Ansi code page.

type
  OemString = type AnsiString(CP_OEMCP);

If that doesn't work, then declare your variable as a RawByteString. Fill it with the data it needs, and once you find out what the current OEM code page is (with GetOEMCP), use SetCodePage to assign that code page to the string (at which point it won't exactly be a RawByteString anymore, despite its declared type).

Rob Kennedy
I want to declarare certain procedures to accept only OEM. And to autoconvert if necessary. The first solution is good for this and accepted, the second makes less sense (since with manual conversion steps I might as well call simply oemtoansi and back)
Marco van de Voort
(note I only checked the logic, I didn't actually test it, or that multiple kinds of ansistrings are overloadable. TYPE x = type y doesn't always work for that)
Marco van de Voort
Is the extra `type` keyword even necessary? If you can overload on AnsiString versus Utf8String, then you should be able to overload based on OemString, too.
Rob Kennedy
I never found much use for type. It doesn't seem to help for overloading of simple types. I couldn't even come up with a piece of code in Borland Pascals where it matters I guess. But that is maybe something for a separate question
Marco van de Voort
You must use `type` when declaring new `AnsiString(N)`-based types.
Remy Lebeau - TeamB
It matters in the declaration of `TDateTime`, @Marco. Otherwise, for example, the Object Inspector will have you edit properties of that type as an ordinary `Double` instead of showing the date-time picker. Likewise for `TColor`.
Rob Kennedy
So such new defined type is only for RTTI, and not for overloading? Do you have other examples?
Marco van de Voort
@Marco: `grep -r '= type ' $(BDS)\Source\\*`; I don't know how many of those *really* need the new type. I thought it was only for RTTI, but if Remy says it's required for code-page-specific AnsiString types, then I'll take his word for it; I don't have a new enough Delphi version to confirm for myself. Maybe the compiler uses the RTTI to generate the right conversion code.
Rob Kennedy
ansistring is special since the types must be different to insert autoconversions. But apparantly this is prepared for ansistring[] in the compiler, but not for other types. I prefer not to grep through Embarcadero sources for possibly FPC related purposes out of cleanroom reasons. Anyway, thanks. The RTTI reason was new to me.
Marco van de Voort