Recent

Author Topic: Local characters in file path  (Read 2140 times)

Ronan

  • Full Member
  • ***
  • Posts: 132
Local characters in file path
« on: January 23, 2018, 03:26:32 pm »
Dear All,

In my filepath I have local characters( "ö", "ü") where the TFilestream coudln't find them to locate the file location. Preferably in Lazarus 0.9.30 version.

Best Regards,

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Local characters in file path
« Reply #1 on: January 23, 2018, 03:39:53 pm »
Have you tried Lazarus 1.8.0? It uses FPC 3.0.4, which has better unicode support.

http://wiki.freepascal.org/FPC_New_Features_3.0#Support_for_codepage-aware_strings

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Local characters in file path
« Reply #2 on: January 23, 2018, 08:03:35 pm »
Dear All,

In my filepath I have local characters( "ö", "ü") where the TFilestream coudln't find them to locate the file location. Preferably in Lazarus 0.9.30 version.

Best Regards,
Operating system?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Ronan

  • Full Member
  • ***
  • Posts: 132
Re: Local characters in file path
« Reply #3 on: January 24, 2018, 07:15:27 am »
@Handoko
For the time being I have to stick to the 0.9.30 vesion because my application compiles flawlessly and its almost inches away from the release candidate, so I don't want to spoil anything at all which will detoriorate its stable status.

@taazz
intended platform is Windows.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Local characters in file path
« Reply #4 on: January 24, 2018, 08:53:25 am »
try this unicode file stream instead and report any problems. It works for me on lazarus 1.4.4 (tested on windows 7 and 8 ) with out problems.

Code: Pascal  [Select][+][-]
  1. unit uUnicodeStream;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9. type
  10.   // fpc 2.6.x and lower is ansi on windows this stream supports unicode filename.
  11.   // on linux the filename is already utf8 and compatible with lcl.
  12.  
  13.   { TUnicodeFileStream }
  14.  
  15.   TUnicodeFileStream = class(THandleStream)
  16.   private
  17.     FFileName : UnicodeString;
  18.   public
  19.     constructor Create(const AFileName: UnicodeString; Mode: Word);overload;
  20.     constructor Create(const AFileName: UnicodeString; Mode: Word; Rights: Cardinal);overload;
  21.     destructor Destroy; override;
  22.     property FileName : UnicodeString Read FFilename;
  23.   end;
  24.  
  25. implementation
  26. uses {$IFDEF MSWINDOWS}windows, {$ELSE} LazFileUtils,{$ENDIF} RtlConsts;
  27.  
  28. {$IFDEF MSWINDOWS}
  29. const
  30.   AccessMode: array[0..2] of Cardinal  = (
  31.     GENERIC_READ,
  32.     GENERIC_WRITE,
  33.     GENERIC_READ or GENERIC_WRITE or FILE_WRITE_ATTRIBUTES);
  34.   ShareModes: array[0..4] of Integer = (
  35.                0,
  36.                0,
  37.                FILE_SHARE_READ,
  38.                FILE_SHARE_WRITE,
  39.                FILE_SHARE_READ or FILE_SHARE_WRITE);
  40.  
  41.  
  42. Function FileCreate (Const FileName : unicodeString; ShareMode : Integer; Rights : Integer) : THandle;overload;inline;
  43. begin
  44.   Result := CreateFileW(PWideChar(FileName), GENERIC_READ or GENERIC_WRITE,
  45.                        dword(ShareModes[(ShareMode and $F0) shr 4]), nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  46. end;
  47. Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle; inline;
  48. begin
  49.   result := CreateFileW(PWideChar(FileName), dword(AccessMode[Mode and 3]),
  50.                        dword(ShareModes[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
  51.                        FILE_ATTRIBUTE_NORMAL, 0);
  52. end;
  53.  
  54. {$ELSE}
  55. Function FileCreate (Const FileName : unicodeString; ShareMode : Integer; Rights : Integer) : THandle;overload;inline;
  56. begin
  57.   Result := FileCreateUTF8(UTF8Encode(FileName), ShareMode, Rights);
  58. end;
  59.  
  60. Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;inline;
  61. begin
  62.   result := FileOpenUTF8(UTF8Encode(FileName), Mode);
  63. end;
  64.  
  65. {$ENDIF}
  66.  
  67. Function FileCreate (Const FileName : unicodeString) : THandle; overload;
  68. begin
  69.   FileCreate := FileCreate(FileName, fmShareExclusive, 0);
  70. end;
  71.  
  72. Function FileCreate (Const FileName : unicodeString; Rights:longint) : THandle;overload;
  73. begin
  74.   FileCreate:=FileCreate(FileName, fmShareExclusive, Rights);
  75. end;
  76.  
  77. {$REGION ' TUnicodeFileStream '}
  78.  
  79. constructor TUnicodeFileStream.Create(const AFileName :UnicodeString; Mode :Word);
  80. begin
  81.   Create(AFileName,Mode,438);
  82. end;
  83.  
  84.  
  85. constructor TUnicodeFileStream.Create(const AFileName: UnicodeString; Mode: Word; Rights: Cardinal);
  86. var
  87.   vHandle :THandle;
  88. begin
  89.   FFileName:=AFileName;
  90.   If (Mode and fmCreate) > 0 then vHandle := FileCreate(AFileName,Mode,Rights)
  91.   else vHandle := FileOpen(AFileName,Mode);
  92.  
  93.   If (vHandle=feInvalidHandle) then
  94.     If Mode=fmcreate then raise EFCreateError.createfmt(SFCreateError,[AFileName])
  95.     else raise EFOpenError.Createfmt(SFOpenError,[AFilename]);
  96.   Create(vHandle);
  97. end;
  98.  
  99.  
  100. destructor TUnicodeFileStream.Destroy;
  101. begin
  102.   FileClose(Handle);
  103.   inherited;
  104. end;
  105. {$ENDREGION}
  106.  
  107. end.
  108.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4469
  • I like bugs.
Re: Local characters in file path
« Reply #5 on: January 24, 2018, 10:59:48 am »
For the time being I have to stick to the 0.9.30 vesion because my application compiles flawlessly and its almost inches away from the release candidate, so I don't want to spoil anything at all which will detoriorate its stable status.
Lazarus 0.9.30 happened in March 28, 2011 which was about 7 years ago.
The current version of is much better and more stable. I would recommend updating.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018