fexpand.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1997-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. A platform independent FExpand implementation
  12. ****************************************************************************}
  13. {$IFDEF FPC_FEXPAND_VOLUMES}
  14. {$IFNDEF FPC_FEXPAND_DRIVES}
  15. (* Volumes are just a special case of drives. *)
  16. {$DEFINE FPC_FEXPAND_DRIVES}
  17. {$ENDIF FPC_FEXPAND_DRIVES}
  18. {$ENDIF FPC_FEXPAND_VOLUMES}
  19. {$IFDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  20. {$IFNDEF FPC_FEXPAND_DRIVES}
  21. (* If DirectorySeparator at the beginning marks a relative path, *)
  22. (* an absolute path must always begin with a drive or volume. *)
  23. {$DEFINE FPC_FEXPAND_DRIVES}
  24. {$ENDIF FPC_FEXPAND_DRIVES}
  25. {$IFNDEF FPC_FEXPAND_MULTIPLE_UPDIR}
  26. (* Traversing multiple levels at once explicitely allowed. *)
  27. {$DEFINE FPC_FEXPAND_MULTIPLE_UPDIR}
  28. {$ENDIF FPC_FEXPAND_MULTIPLE_UPDIR}
  29. (* Helper define used to support common features of FPC_FEXPAND_DIRSEP_IS_* *)
  30. {$DEFINE FPC_FEXPAND_UPDIR_HELPER}
  31. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  32. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  33. {$IFNDEF FPC_FEXPAND_DRIVES}
  34. (* If DirectorySeparator at the beginning marks a relative path, *)
  35. (* an absolute path must always begin with a drive or volume. *)
  36. {$DEFINE FPC_FEXPAND_DRIVES}
  37. {$ENDIF FPC_FEXPAND_DRIVES}
  38. {$IFNDEF FPC_FEXPAND_MULTIPLE_UPDIR}
  39. (* Traversing multiple levels at once explicitely allowed. *)
  40. {$DEFINE FPC_FEXPAND_MULTIPLE_UPDIR}
  41. {$ENDIF FPC_FEXPAND_MULTIPLE_UPDIR}
  42. (* Helper define used to support common features of FPC_FEXPAND_DIRSEP_IS_* *)
  43. {$DEFINE FPC_FEXPAND_UPDIR_HELPER}
  44. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  45. { this code is used both in sysutils and in the dos unit, and the dos
  46. unit does not have a charinset routine }
  47. {$if not defined(FPC_FEXPAND_SYSUTILS) and not defined(FPC_FEXPAND_HAS_CHARINSET)}
  48. {$define FPC_FEXPAND_HAS_CHARINSET}
  49. type
  50. TFExpandSysCharSet = set of ansichar;
  51. Function CharInSet(Ch:AnsiChar;Const CSet : TFExpandSysCharSet) : Boolean; inline;
  52. begin
  53. CharInSet:=ch in CSet;
  54. end;
  55. Function CharInSet(Ch:WideChar;Const CSet : TFExpandSysCharSet) : Boolean;
  56. begin
  57. CharInSet:=(Ch<=#$FF) and (ansichar(byte(ch)) in CSet);
  58. end;
  59. {$endif}
  60. procedure GetDirIO (DriveNr: byte; var Dir: {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}RawByteString{$else}PathStr{$endif});
  61. (* GetDirIO is supposed to return the root of the given drive *)
  62. (* in case of an error for compatibility of FExpand with TP/BP. *)
  63. var
  64. OldInOutRes: word;
  65. begin
  66. OldInOutRes := InOutRes;
  67. InOutRes := 0;
  68. GetDir (DriveNr, Dir);
  69. InOutRes := OldInOutRes;
  70. end;
  71. {$IFDEF FPC_FEXPAND_VOLUMES}
  72. {$IFNDEF FPC_FEXPAND_NO_DEFAULT_PATHS}
  73. procedure GetDirIO (const VolumeName: OpenString; var Dir: {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}RawByteString{$else}PathStr{$endif});
  74. var
  75. OldInOutRes: word;
  76. begin
  77. OldInOutRes := InOutRes;
  78. InOutRes := 0;
  79. GetDir (VolumeName, Dir);
  80. InOutRes := OldInOutRes;
  81. end;
  82. {$ENDIF FPC_FEXPAND_NO_DEFAULT_PATHS}
  83. {$ENDIF FPC_FEXPAND_VOLUMES}
  84. function FExpand (const Path: PathStr): PathStr;
  85. (* LFNSupport boolean constant, variable or function must be declared for all
  86. the platforms, at least locally in the Dos unit implementation part.
  87. In addition, FPC_FEXPAND_UNC, FPC_FEXPAND_DRIVES, FPC_FEXPAND_GETENV_PCHAR,
  88. FPC_FEXPAND_TILDE, FPC_FEXPAND_VOLUMES, FPC_FEXPAND_NO_DEFAULT_PATHS,
  89. FPC_FEXPAND_DRIVESEP_IS_ROOT, FPC_FEXPAND_NO_CURDIR,
  90. FPC_FEXPAND_NO_DOTS_UPDIR, FPC_FEXPAND_DIRSEP_IS_UPDIR,
  91. FPC_FEXPAND_DIRSEP_IS_CURDIR and FPC_FEXPAND_MULTIPLE_UPDIR conditionals
  92. might be defined to specify FExpand behaviour - see end of this file for
  93. individual descriptions. Finally, FPC_FEXPAND_SYSUTILS allows to reuse
  94. the same implementation for SysUtils.ExpandFileName.
  95. *)
  96. {$IFDEF FPC_FEXPAND_DRIVES}
  97. var
  98. PathStart: longint;
  99. {$ELSE FPC_FEXPAND_DRIVES}
  100. const
  101. PathStart = 1;
  102. {$ENDIF FPC_FEXPAND_DRIVES}
  103. {$IFDEF FPC_FEXPAND_UNC}
  104. var
  105. RootNotNeeded: boolean;
  106. {$ELSE FPC_FEXPAND_UNC}
  107. const
  108. RootNotNeeded = false;
  109. {$ENDIF FPC_FEXPAND_UNC}
  110. var S, Pa, Dirs, TmpS: {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}RawByteString{$else}PathStr{$endif};
  111. I, J: longint;
  112. begin
  113. {$IFDEF FPC_FEXPAND_UNC}
  114. RootNotNeeded := false;
  115. {$ENDIF FPC_FEXPAND_UNC}
  116. (* First convert the path to uppercase if appropriate for current platform. *)
  117. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  118. { for sysutils/rawbytestring, process everything in
  119. DefaultFileSystemCodePage to prevent risking data loss that may be
  120. relevant when the file name is used }
  121. if FileNameCasePreserving then
  122. Pa := ToSingleByteFileSystemEncodedFileName (Path)
  123. else
  124. Pa := UpCase (ToSingleByteFileSystemEncodedFileName (Path));
  125. {$ELSE FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  126. if FileNameCasePreserving then
  127. Pa := Path
  128. else
  129. Pa := UpCase (Path);
  130. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  131. (* Allow both '/' and '\' as directory separators *)
  132. (* by converting all to the native one. *)
  133. {$warnings off}
  134. for I := 1 to Length (Pa) do
  135. if CharInSet(Pa [I], AllowDirectorySeparators) then
  136. Pa [I] := DirectorySeparator;
  137. {$warnings on}
  138. (* PathStart is amount of characters to strip to get beginning *)
  139. (* of path without volume/drive specification. *)
  140. {$IFDEF FPC_FEXPAND_DRIVES}
  141. {$IFDEF FPC_FEXPAND_VOLUMES}
  142. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  143. PathStart := Pos (DriveSeparator, Pa);
  144. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  145. PathStart := Succ (Pos (DriveSeparator, Pa));
  146. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  147. {$ELSE FPC_FEXPAND_VOLUMES}
  148. PathStart := 3;
  149. {$ENDIF FPC_FEXPAND_VOLUMES}
  150. {$ENDIF FPC_FEXPAND_DRIVES}
  151. (* Expand tilde to home directory if appropriate. *)
  152. {$IFDEF FPC_FEXPAND_TILDE}
  153. {Replace ~/ with $HOME/}
  154. if (Length (Pa) >= 1) and (Pa [1] = '~') and
  155. ((Pa [2] = DirectorySeparator) or (Length (Pa) = 1)) then
  156. begin
  157. {$IFDEF FPC_FEXPAND_SYSUTILS}
  158. {$IFDEF SYSUTILSUNICODE}
  159. S := PathStr(GetEnvironmentVariable ('HOME'));
  160. {$ELSE SYSUTILSUNICODE}
  161. S := ToSingleByteFileSystemEncodedFileName(GetEnvironmentVariable ('HOME'));
  162. {$ENDIF SYSUTILSUNICODE}
  163. {$ELSE FPC_FEXPAND_SYSUTILS}
  164. {$IFDEF FPC_FEXPAND_GETENV_PCHAR}
  165. S := StrPas (GetEnv ('HOME'));
  166. {$ELSE FPC_FEXPAND_GETENV_PCHAR}
  167. S := GetEnv ('HOME');
  168. {$ENDIF FPC_FEXPAND_GETENV_PCHAR}
  169. {$ENDIF FPC_FEXPAND_SYSUTILS}
  170. if (S = '') or (Length (S) = 1) and (Length (Pa) > 1)
  171. and (S [1] = DirectorySeparator) then
  172. Delete (Pa, 1, 1)
  173. else
  174. if S [Length (S)] = DirectorySeparator then
  175. Pa := S + Copy (Pa, 3, Length (Pa) - 2)
  176. else
  177. Pa := S + Copy (Pa, 2, Pred (Length (Pa)));
  178. end;
  179. {$ENDIF FPC_FEXPAND_TILDE}
  180. (* Do we have a drive/volume specification? *)
  181. {$IFDEF FPC_FEXPAND_VOLUMES}
  182. if PathStart > 1 then
  183. {$ELSE FPC_FEXPAND_VOLUMES}
  184. if (Length (Pa) > 1) and CharInSet(Pa [1], ['A'..'Z', 'a'..'z']) and
  185. (Pa [2] = DriveSeparator) and (DriveSeparator <> DirectorySeparator) then
  186. {$ENDIF FPC_FEXPAND_VOLUMES}
  187. begin
  188. (* We need to know current directory on given *)
  189. (* volume/drive _if_ such a thing is defined. *)
  190. {$IFDEF FPC_FEXPAND_DRIVES}
  191. {$IFNDEF FPC_FEXPAND_NO_DEFAULT_PATHS}
  192. {$IFDEF FPC_FEXPAND_VOLUMES}
  193. GetDirIO (Copy (Pa, 1, PathStart - 2), S);
  194. {$ELSE FPC_FEXPAND_VOLUMES}
  195. { Always uppercase driveletter }
  196. if CharInSet(Pa [1], ['a'..'z']) then
  197. Pa [1] := Chr (Ord (Pa [1]) and not ($20));
  198. GetDirIO (Ord (Pa [1]) - Ord ('A') + 1, S);
  199. {$ENDIF FPC_FEXPAND_VOLUMES}
  200. (* Do we have more than just drive/volume specification? *)
  201. if Length (Pa) = Pred (PathStart) then
  202. (* If not, just use the current directory for that drive/volume. *)
  203. Pa := S
  204. else
  205. (* If yes, find out whether the following path is relative or absolute. *)
  206. if Pa [PathStart] <> DirectorySeparator then
  207. {$IFDEF FPC_FEXPAND_VOLUMES}
  208. if Copy (Pa, 1, PathStart - 2) = Copy (S, 1, PathStart - 2)
  209. then
  210. {$ELSE FPC_FEXPAND_VOLUMES}
  211. if UpCase(Pa [1]) = UpCase(S [1]) then
  212. {$ENDIF FPC_FEXPAND_VOLUMES}
  213. begin
  214. { remove ending slash if it already exists }
  215. if S [Length (S)] = DirectorySeparator then
  216. SetLength(S,Length(S)-1);
  217. {$IFDEF FPC_FEXPAND_SYSUTILS}
  218. { not "Pa := S + DirectorySeparator + ..." because
  219. that will convert the result to
  220. DefaultSystemCodePage in case of RawByteString due
  221. to DirectorySeparator being an ansichar }
  222. TmpS := S;
  223. SetLength(TmpS, Length(TmpS) + 1);
  224. TmpS[Length(TmpS)] := DirectorySeparator;
  225. Pa := TmpS +
  226. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  227. {$ELSE FPC_FEXPAND_SYSUTILS}
  228. Pa := S + DirectorySeparator +
  229. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  230. {$ENDIF FPC_FEXPAND_SYSUTILS}
  231. end
  232. else
  233. begin
  234. TmpS := DriveSeparator + DirectorySeparator;
  235. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  236. SetCodePage(TmpS, DefaultFileSystemCodePage, false);
  237. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  238. {$IFDEF FPC_FEXPAND_VOLUMES}
  239. Pa := Copy (Pa, 1, PathStart - 2) + TmpS +
  240. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  241. {$ELSE FPC_FEXPAND_VOLUMES}
  242. { copy() instead of Pa[1] to preserve string code page }
  243. Pa := Copy (Pa, 1, 1) + TmpS +
  244. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  245. {$ENDIF FPC_FEXPAND_VOLUMES}
  246. end
  247. {$ENDIF FPC_FEXPAND_NO_DEFAULT_PATHS}
  248. end
  249. else
  250. {$ELSE FPC_FEXPAND_DRIVES}
  251. (* If drives are not supported, but a drive *)
  252. (* was supplied anyway, ignore (remove) it. *)
  253. Delete (Pa, 1, 2);
  254. end;
  255. {Check whether we don't have an absolute path already}
  256. if (Length (Pa) >= PathStart) and (Pa [PathStart] <> DirectorySeparator) or
  257. (Length (Pa) < PathStart) then
  258. {$ENDIF FPC_FEXPAND_DRIVES}
  259. begin
  260. (* Get current directory on selected drive/volume. *)
  261. GetDirIO (0, S);
  262. {$IFDEF FPC_FEXPAND_VOLUMES}
  263. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  264. PathStart := Pos (DriveSeparator, S);
  265. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  266. PathStart := Succ (Pos (DriveSeparator, S));
  267. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  268. {$ENDIF FPC_FEXPAND_VOLUMES}
  269. (* Do we have an absolute path without drive or volume? *)
  270. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  271. {$IFDEF FPC_FEXPAND_DRIVES}
  272. if (Length (Pa) > 0)
  273. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  274. and (Pa [1] = DriveSeparator)
  275. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  276. and (Pa [1] = DirectorySeparator)
  277. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  278. then
  279. begin
  280. {$IFDEF FPC_FEXPAND_UNC}
  281. {Do not touch network drive names}
  282. if (Length (Pa) > 1) and (Pa [2] = DirectorySeparator)
  283. and LFNSupport then
  284. begin
  285. PathStart := 3;
  286. {Find the start of the string of directories}
  287. while (PathStart <= Length (Pa)) and
  288. (Pa [PathStart] <> DirectorySeparator) do
  289. Inc (PathStart);
  290. if PathStart > Length (Pa) then
  291. {We have just a machine name...}
  292. if Length (Pa) = 2 then
  293. {...or not even that one}
  294. PathStart := 2
  295. else
  296. begin
  297. {$IFDEF FPC_FEXPAND_SYSUTILS}
  298. { no string concatenation to prevent code page
  299. conversion for RawByteString }
  300. SetLength(Pa, Length(Pa) + 1);
  301. Pa[Length(Pa)] := DirectorySeparator
  302. {$ELSE FPC_FEXPAND_SYSUTILS}
  303. Pa := Pa + DirectorySeparator;
  304. {$ENDIF FPC_FEXPAND_SYSUTILS}
  305. end
  306. else if PathStart < Length (Pa) then
  307. {We have a resource name as well}
  308. begin
  309. RootNotNeeded := true;
  310. {Let's continue in searching}
  311. repeat
  312. Inc (PathStart);
  313. until (PathStart > Length (Pa)) or
  314. (Pa [PathStart] = DirectorySeparator);
  315. end;
  316. end
  317. else
  318. {$ENDIF FPC_FEXPAND_UNC}
  319. begin
  320. {$IFDEF FPC_FEXPAND_VOLUMES}
  321. I := Pos (DriveSeparator, S);
  322. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  323. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  324. if (Pa [1] = DriveSeparator) then
  325. Delete (Pa, 1, 1);
  326. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  327. Pa := Copy (S, 1, I) + Pa;
  328. PathStart := I;
  329. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  330. TmpS := Copy (S, 1, Pred (I));
  331. SetLength(TmpS, Length(TmpS) + 1);
  332. TmpS[Length(TmpS)] := DriveSeparator;
  333. Pa := TmpS + Pa;
  334. PathStart := Succ (I);
  335. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  336. {$ELSE FPC_FEXPAND_VOLUMES}
  337. TmpS := S[1] + DriveSeparator;
  338. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  339. SetCodePage(TmpS, DefaultFileSystemCodePage, false);
  340. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  341. Pa := TmpS + Pa;
  342. {$ENDIF FPC_FEXPAND_VOLUMES}
  343. end;
  344. end
  345. else
  346. {$ENDIF FPC_FEXPAND_DRIVES}
  347. (* We already have a slash if root is the curent directory. *)
  348. if Length (S) = PathStart then
  349. Pa := S + Pa
  350. {$ELSE FPC_FEXPAND_DIRSEP_IS_CURDIR}
  351. (* More complex with DirectorySeparator as current directory *)
  352. if (S [Length (S)] = DriveSeparator)
  353. and (Pa [1] = DirectorySeparator) then
  354. Pa := S + Copy (Pa, 2, Pred (Length (Pa)))
  355. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  356. else
  357. (* We need an ending slash if FExpand was called *)
  358. (* with an empty string for compatibility, except *)
  359. (* for platforms where this is invalid. *)
  360. if Length (Pa) = 0 then
  361. begin
  362. Pa := S;
  363. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  364. {$IFDEF FPC_FEXPAND_SYSUTILS}
  365. { no string concatenation to prevent code page
  366. conversion for RawByteString }
  367. SetLength(Pa, Length(Pa) + 1);
  368. Pa[Length(Pa)] := DirectorySeparator
  369. {$ELSE FPC_FEXPAND_SYSUTILS}
  370. Pa := Pa + DirectorySeparator;
  371. {$ENDIF FPC_FEXPAND_SYSUTILS}
  372. {$ENDIF not FPC_FEXPAND_DIRSEP_IS_UPDIR}
  373. end
  374. else
  375. {$IFDEF FPC_FEXPAND_UPDIR_HELPER}
  376. if Pa [1] = DirectorySeparator then
  377. Pa := S + Pa
  378. else
  379. {$ENDIF FPC_FEXPAND_UPDIR_HELPER}
  380. begin
  381. {$IFDEF FPC_FEXPAND_SYSUTILS}
  382. { not "Pa := S + DirectorySeparator + Pa" because
  383. that will convert the result to
  384. DefaultSystemCodePage in case of RawByteString due
  385. to DirectorySeparator being an ansichar. Don't
  386. always use this code because in case of
  387. truncation with shortstrings the result will be
  388. different }
  389. TmpS := S;
  390. SetLength(TmpS, Length(TmpS) + 1);
  391. TmpS[Length(TmpS)] := DirectorySeparator;
  392. Pa := TmpS + Pa;
  393. {$ELSE FPC_FEXPAND_SYSUTILS}
  394. Pa := S + DirectorySeparator + Pa
  395. {$ENDIF FPC_FEXPAND_SYSUTILS}
  396. end;
  397. end;
  398. {Get string of directories to only process relative references on this one}
  399. Dirs := Copy (Pa, Succ (PathStart), Length (Pa) - PathStart);
  400. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  401. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  402. {First remove all references to '\.\'}
  403. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  404. while I <> 0 do
  405. begin
  406. Delete (Dirs, I, 2);
  407. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  408. end;
  409. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  410. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  411. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  412. {$IFDEF FPC_FEXPAND_MULTIPLE_UPDIR}
  413. {Now replace all references to '\...' with '\..\..'}
  414. I := Pos (DirectorySeparator + '...', Dirs);
  415. while I <> 0 do
  416. begin
  417. Insert (DirectorySeparator + '.', Dirs, I + 3);
  418. I := Pos (DirectorySeparator + '...', Dirs);
  419. end;
  420. {$ENDIF FPC_FEXPAND_MULTIPLE_UPDIR}
  421. {Now remove also all references to '\..\' + of course previous dirs..}
  422. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  423. while I <> 0 do
  424. begin
  425. J := Pred (I);
  426. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  427. Dec (J);
  428. Delete (Dirs, Succ (J), I - J + 3);
  429. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  430. end;
  431. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  432. {$IFDEF FPC_FEXPAND_UPDIR_HELPER}
  433. (* Now remove all references to '//' plus previous directories... *)
  434. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  435. while I <> 0 do
  436. begin
  437. J := Pred (I);
  438. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  439. Dec (J);
  440. Delete (Dirs, Succ (J), Succ (I - J));
  441. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  442. end;
  443. {$ENDIF FPC_FEXPAND_UPDIR_HELPER}
  444. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  445. {Then remove also a reference to '\..' at the end of line
  446. + the previous directory, of course,...}
  447. I := Pos (DirectorySeparator + '..', Dirs);
  448. if (I <> 0) and (I = Length (Dirs) - 2) then
  449. begin
  450. J := Pred (I);
  451. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  452. Dec (J);
  453. if (J = 0) then
  454. Dirs := ''
  455. else
  456. Delete (Dirs, Succ (J), I - J + 2);
  457. end;
  458. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  459. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  460. (* Remove a possible reference to '/' at the *)
  461. (* end of line plus the previous directory. *)
  462. I := Length (Dirs);
  463. if (I > 0) and (Dirs [I] = DirectorySeparator) then
  464. begin
  465. J := Pred (I);
  466. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  467. Dec (J);
  468. if (J = 0) then
  469. Dirs := ''
  470. else
  471. Delete (Dirs, J, Succ (I - J));
  472. end;
  473. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  474. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  475. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  476. {...and also a possible reference to '\.'}
  477. if (Length (Dirs) = 1) then
  478. begin
  479. if (Dirs [1] = '.') then
  480. {A special case}
  481. Dirs := ''
  482. end
  483. else
  484. if (Length (Dirs) <> 0) and (Dirs [Length (Dirs)] = '.') and
  485. (Dirs [Pred (Length (Dirs))] = DirectorySeparator) then
  486. Delete (Dirs,length(Dirs)-1,2);
  487. {Finally remove '.\' at the beginning of the string of directories...}
  488. while (Length (Dirs) >= 2) and (Dirs [1] = '.')
  489. and (Dirs [2] = DirectorySeparator) do
  490. Delete (Dirs, 1, 2);
  491. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  492. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  493. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  494. (* Remove possible (invalid) references to '/' at the beginning. *)
  495. while (Length (Dirs) >= 1) and (Dirs [1] = '/') do
  496. Delete (Dirs, 1, 1);
  497. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  498. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  499. {...and possible (invalid) references to '..\' as well}
  500. while (Length (Dirs) >= 3) and (Dirs [1] = '.') and (Dirs [2] = '.') and
  501. (Dirs [3] = DirectorySeparator) do
  502. Delete (Dirs, 1, 3);
  503. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  504. {Two special cases - '.' and '..' alone}
  505. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  506. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  507. if (Length (Dirs) = 1) and (Dirs [1] = '.') then
  508. Dirs := '';
  509. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  510. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  511. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  512. if (Length (Dirs) = 2) and (Dirs [1] = '.') and (Dirs [2] = '.') then
  513. Dirs := '';
  514. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  515. {Join the parts back to create the complete path}
  516. if Length (Dirs) = 0 then
  517. begin
  518. Pa := Copy (Pa, 1, PathStart);
  519. {$IFNDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  520. if Pa [PathStart] <> DirectorySeparator then
  521. begin
  522. {$IFDEF FPC_FEXPAND_SYSUTILS}
  523. { no string concatenation to prevent code page
  524. conversion for RawByteString }
  525. SetLength(Pa, Length(Pa) + 1);
  526. Pa[Length(Pa)] := DirectorySeparator
  527. {$ELSE FPC_FEXPAND_SYSUTILS}
  528. Pa := Pa + DirectorySeparator;
  529. {$ENDIF FPC_FEXPAND_SYSUTILS}
  530. end
  531. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  532. end
  533. else
  534. Pa := Copy (Pa, 1, PathStart) + Dirs;
  535. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  536. {Remove ending \ if not supplied originally, the original string
  537. wasn't empty (to stay compatible) and if not really needed}
  538. if (Pa [Length (Pa)] = DirectorySeparator)
  539. and ((Length (Pa) > PathStart) or
  540. {A special case with UNC paths}
  541. (RootNotNeeded and (Length (Pa) = PathStart)))
  542. {Reference to current directory at the end should be removed}
  543. and (Length (Path) <> 0)
  544. and (Path [Length (Path)] <> DirectorySeparator)
  545. then
  546. Delete (PA,length(PA),1);
  547. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  548. FExpand := Pa;
  549. end;
  550. (* Description of individual conditional defines supported for FExpand
  551. (disregard the used directory separators in examples, constant
  552. System.DirectorySeparator is used in the real implemenation, of course):
  553. FPC_FEXPAND_UNC - UNC ("Universal Naming Convention") paths are
  554. supported (usually used for networking, used in DOS (with
  555. networking support installed), OS/2, Win32 and at least some
  556. Netware versions as far as I remember. An example of such a path
  557. is '\\servername\sharename\some\path'.
  558. FPC_FEXPAND_DRIVES - drive letters are supported (DOS-like
  559. environments - DOS, OS/2, Win32). Example is 'C:\TEST'.
  560. FPC_FEXPAND_GETENV_PCHAR - an implementation of GetEnv returning
  561. PChar instead of a shortstring is available (Unix) to support
  562. long values of environment variables.
  563. FPC_FEXPAND_TILDE - expansion of '~/' to GetEnv('HOME') - Unix.
  564. Example: '~/some/path'.
  565. FPC_FEXPAND_VOLUMES - volumes are supported (similar to drives,
  566. but the name can be longer; used under Netware, Amiga and
  567. probably MacOS as far as I understand it correctly). Example:
  568. 'VolumeName:Some:Path' or 'ServerName/Volume:Some\Path'
  569. (Netware).
  570. FPC_FEXPAND_NO_DEFAULT_PATHS - Dos keeps information about the
  571. current directory for every drive. If some platform supports
  572. drives or volumes, but keeps no track of current directories for
  573. them (i.e. there's no support for "GetDir(DriveNumber, Dir)" or
  574. "GetDir(Volume, Dir)", but only for "GetDir (0, Dir)" (i.e. the
  575. overall current directory), you should define this. Otherwise
  576. constructs like 'C:Some\Path' refer a path relative to the
  577. current directory on the C: drive.
  578. FPC_FEXPAND_DRIVESEP_IS_ROOT - this means that DriveSeparator
  579. should be used as beginning of the "real" path for a particular
  580. drive or volume instead of the DirectorySeparator. This would be
  581. used in case that there is only one character (DriveSeparator)
  582. delimitting the drive letter or volume name from the remaining
  583. path _and_ the DriveSeparator marks the root of an absolute path
  584. in that case. Example - 'Volume:This/Is/Absolute/Path'.
  585. FPC_FEXPAND_NO_CURDIR - there is no support to refer to current
  586. directory explicitely (like '.' used under both Unix and DOS-like
  587. environments).
  588. FPC_FEXPAND_NO_DOTS_UPDIR - '..' cannot be used to refer to the
  589. upper directory.
  590. FPC_FEXPAND_DIRSEP_IS_UPDIR - DirectorySeparator at the beginning of
  591. a path (or doubled DirectorySeparator inside the path) refer to the
  592. parent directory, one more DirectorySeparator to parent directory of
  593. parent directory and so on (Amiga). Please, note that you can decide
  594. to support both '..' and DirectorySeparator as references to the parent
  595. directory at the same time for compatibility reasons - however this
  596. support makes it impossible to use anotherwise possibly valid name
  597. of '..'.
  598. FPC_FEXPAND_DIRSEP_IS_CURDIR - DirectorySeparator at the beginning of
  599. a path refers to the current directory (i.e. path beginning with
  600. DirectorySeparator is always a relative path). Two DirectorySeparator
  601. characters refer to the parent directory, three refer to parent
  602. directory of the parent directory and so on (MacOS).
  603. FPC_FEXPAND_MULTIPLE_UPDIR - grouping of more characters specifying
  604. upper directory references higher directory levels. Example: '...'
  605. (Netware).
  606. FPC_FEXPAND_SYSUTILS allows to reuse the same implementation for
  607. SysUtils.ExpandFileName by avoiding things specific for unit Dos.
  608. *)