sysdir.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit for the Win32 API.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {*****************************************************************************
  13. Directory Handling
  14. *****************************************************************************}
  15. type
  16. TDirFnType=function(name:pointer):longbool;stdcall;
  17. {$ifdef FPC_UNICODE_RTL}
  18. function CreateDirectoryTrunc(name:pointer):longbool;stdcall;
  19. begin
  20. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  21. end;
  22. procedure dirfn(afunc : TDirFnType;s:unicodestring);
  23. begin
  24. DoDirSeparators(s);
  25. if not aFunc(punicodechar(s)) then
  26. begin
  27. errno:=GetLastError;
  28. Errno2InoutRes;
  29. end;
  30. end;
  31. Procedure do_MkDir(const s: UnicodeString);[IOCheck];
  32. begin
  33. If (length(s)=0) or (InOutRes <> 0) then
  34. exit;
  35. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  36. end;
  37. Procedure do_RmDir(const s: UnicodeString);[IOCheck];
  38. begin
  39. if (s ='.') then
  40. InOutRes := 16;
  41. {$ifdef WINCE}
  42. if (s='..') then
  43. InOutRes := 5;
  44. {$endif WINCE}
  45. If (s='') or (InOutRes <> 0) then
  46. exit;
  47. dirfn(TDirFnType(@RemoveDirectory),s);
  48. {$ifdef WINCE}
  49. if (Inoutres=3) and (Pos(DirectorySeparator, s)<2) then
  50. Inoutres:=2;
  51. {$endif WINCE}
  52. end;
  53. Procedure do_ChDir(const s: UnicodeString);[IOCheck];
  54. begin
  55. {$ifndef WINCE}
  56. If (s='') or (InOutRes <> 0) then
  57. exit;
  58. dirfn(TDirFnType(@SetCurrentDirectory),s);
  59. if Inoutres=2 then
  60. Inoutres:=3;
  61. {$else WINCE}
  62. InOutRes:=3;
  63. {$endif WINCE}
  64. end;
  65. procedure do_GetDir (DriveNr: byte; var Dir: Unicodestring);
  66. {$ifndef WINCE}
  67. var
  68. Drive:array[0..3]of char;
  69. defaultdrive:boolean;
  70. savebuf: UnicodeString;
  71. len : integer;
  72. {$endif WINCE}
  73. begin
  74. {$ifndef WINCE}
  75. defaultdrive:=drivenr=0;
  76. if not defaultdrive then
  77. begin
  78. Drive[0]:=widechar(Drivenr+64);
  79. Drive[1]:=':';
  80. Drive[2]:=#0;
  81. Drive[3]:=#0;
  82. len:=GetCurrentDirectory(0,nil); // in TChar
  83. setlength(savebuf,len-1); // -1 because len is #0 inclusive
  84. GetCurrentDirectory(high(SaveBuf)+1,punicodechar(SaveBuf)); // in TChar
  85. if not SetCurrentDirectory(@Drive) then
  86. begin
  87. errno := word (GetLastError);
  88. Errno2InoutRes;
  89. Dir := widechar (DriveNr + 64) + ':\';
  90. SetCurrentDirectory(@SaveBuf);
  91. Exit;
  92. end;
  93. end;
  94. len:=GetCurrentDirectory(0,nil);
  95. setlength(dir,len-1); // -1 because len is #0 inclusive
  96. GetCurrentDirectory(len,punicodechar(dir));
  97. if not defaultdrive then
  98. SetCurrentDirectory(@SaveBuf);
  99. if not FileNameCasePreserving then
  100. dir:=upcase(dir);
  101. {todo: massive loss of encoding and number of chars}
  102. {$else WINCE}
  103. Dir:='\';
  104. {$endif WINCE}
  105. end;
  106. Procedure do_MkDir(const s: RawByteString);[IOCheck];
  107. begin
  108. do_mkdir(UnicodeString(s));
  109. end;
  110. Procedure do_RmDir(const s: RawByteString);[IOCheck];
  111. begin
  112. do_RmDir(UnicodeString(s));
  113. end;
  114. Procedure do_ChDir(const s: RawByteString);[IOCheck];
  115. begin
  116. do_ChDir(UnicodeString(s));
  117. end;
  118. procedure do_GetDir (DriveNr: byte; var Dir: RawByteString);
  119. var ldir : Unicodestring;
  120. begin
  121. do_GetDir(DriveNr,ldir);
  122. dir:=ToSingleByteFileSystemEncodedFileName(ldir);
  123. end;
  124. {$else}
  125. procedure dirfn(afunc : TDirFnType;dir:RawByteString);
  126. begin
  127. DoDirSeparators(dir);
  128. if not aFunc(pchar(dir)) then
  129. begin
  130. errno:=GetLastError;
  131. Errno2InoutRes;
  132. end;
  133. end;
  134. function CreateDirectoryTrunc(name:pointer):longbool;stdcall;
  135. begin
  136. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  137. end;
  138. Procedure do_MkDir(const s: RawByteString);[IOCheck];
  139. begin
  140. If (s='') or (InOutRes <> 0) then
  141. exit;
  142. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  143. end;
  144. Procedure do_RmDir(const s: RawByteString);[IOCheck];
  145. begin
  146. if (s ='.') then
  147. InOutRes := 16;
  148. If (s='') or (InOutRes <> 0) then
  149. exit;
  150. {$ifdef WINCE}
  151. if (len=2) and (s[0]='.') and (s[1]='.') then
  152. InOutRes := 5;
  153. {$endif WINCE}
  154. dirfn(TDirFnType(@RemoveDirectory),s);
  155. {$ifdef WINCE}
  156. if (Inoutres=3) and (Pos(DirectorySeparator, s)<2) then
  157. Inoutres:=2;
  158. {$endif WINCE}
  159. end;
  160. Procedure do_ChDir(const s: RawByteString);[IOCheck];
  161. begin
  162. {$ifndef WINCE}
  163. If (s='.') or (InOutRes <> 0) then
  164. exit;
  165. dirfn(TDirFnType(@SetCurrentDirectory),s);
  166. if Inoutres=2 then
  167. Inoutres:=3;
  168. {$else WINCE}
  169. InOutRes:=3;
  170. {$endif WINCE}
  171. end;
  172. procedure do_GetDir (DriveNr: byte; var Dir: RawByteString);
  173. // this old implementation is wired -A and thus is >260 char.
  174. {$ifndef WINCE}
  175. var
  176. Drive:array[0..3]of char;
  177. defaultdrive:boolean;
  178. DirBuf,SaveBuf:array[0..259] of Char;
  179. {$endif WINCE}
  180. begin
  181. {$ifndef WINCE}
  182. defaultdrive:=drivenr=0;
  183. if not defaultdrive then
  184. begin
  185. byte(Drive[0]):=Drivenr+64;
  186. Drive[1]:=':';
  187. Drive[2]:=#0;
  188. Drive[3]:=#0;
  189. GetCurrentDirectory(SizeOf(SaveBuf),SaveBuf);
  190. if not SetCurrentDirectory(@Drive) then
  191. begin
  192. errno := word (GetLastError);
  193. Errno2InoutRes;
  194. Dir := char (DriveNr + 64) + ':\';
  195. SetCurrentDirectory(@SaveBuf);
  196. Exit;
  197. end;
  198. end;
  199. GetCurrentDirectory(SizeOf(DirBuf),DirBuf);
  200. if not defaultdrive then
  201. SetCurrentDirectory(@SaveBuf);
  202. dir:=DirBuf;
  203. if not FileNameCasePreserving then
  204. dir:=upcase(dir);
  205. {$else WINCE}
  206. Dir:='\';
  207. {$endif WINCE}
  208. end;
  209. {$endif}