[fpc-pascal] Converting StrLCopy to widestrings
Marc Weustink
marc at dommelstein.net
Sat Nov 17 13:25:17 CET 2007
Felipe Monteiro de Carvalho wrote:
> Hello,
>
> In a LCL code we have:
>
> var
> FileNameBuffer: PChar;
> FileNameWide: WideString;
> FileNameWideBuffer: PWideChar;
> FilterBuffer: WideString;
> begin
> {$ifdef WindowsUnicodeSupport}
> if UnicodeEnabledOS then
> FileNameWideBuffer := AllocMem(FileNameBufferLen * 2 + 2)
> else
> FileNameBuffer := AllocMem(FileNameBufferLen + 1);
> {$else}
> FileNameBuffer := AllocMem(FileNameBufferLen + 1);
> {$endif}
>
> .....
> {$ifdef WindowsUnicodeSupport}
> if UnicodeEnabledOS then
> begin
> FileNameWide := UTF8Decode(FileName);
>
> { StrLCopy is a PChar function, so it won't create a proper 2-byte
> sized ending and we ensure that it's there by cleaning the string }
> FillChar(FileNameWideBuffer^, FileNameBufferLen * 2 + 2, #0);
>
> StrLCopy(PChar(FileNameWideBuffer),
> PChar(PWideChar(FileNameWide)), FileNameBufferLen * 2);
> end
> else
> StrLCopy(FileNameBuffer, PChar(UTF8ToAnsi(FileName)), FileNameBufferLen);
> {$else}
> StrLCopy(FileNameBuffer, PChar(FileName), FileNameBufferLen);
> {$endif}
>
> ...
> {$ifdef WindowsUnicodeSupport}
> if UnicodeEnabledOS then
> begin
> lpStrFile := PChar(FileNameWideBuffer);
> .....
First question, why is the copying needed ?
Why isn't lpStrFile := PWChar(FileNameWide);
simply enough ?
IMO, you're copying way to much.
Marc
More information about the fpc-pascal
mailing list