fexpand.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. { already done before this routine is called from sysutils }
  132. {$IFNDEF FPC_FEXPAND_SYSUTILS}
  133. (* Allow both '/' and '\' as directory separators *)
  134. (* by converting all to the native one. *)
  135. {$warnings off}
  136. for I := 1 to Length (Pa) do
  137. if CharInSet(Pa [I], AllowDirectorySeparators) then
  138. Pa [I] := DirectorySeparator;
  139. {$warnings on}
  140. {$ENDIF not FPC_FEXPAND_SYSUTILS}
  141. (* PathStart is amount of characters to strip to get beginning *)
  142. (* of path without volume/drive specification. *)
  143. {$IFDEF FPC_FEXPAND_DRIVES}
  144. {$IFDEF FPC_FEXPAND_VOLUMES}
  145. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  146. PathStart := Pos (DriveSeparator, Pa);
  147. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  148. PathStart := Succ (Pos (DriveSeparator, Pa));
  149. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  150. {$ELSE FPC_FEXPAND_VOLUMES}
  151. PathStart := 3;
  152. {$ENDIF FPC_FEXPAND_VOLUMES}
  153. {$ENDIF FPC_FEXPAND_DRIVES}
  154. (* Expand tilde to home directory if appropriate. *)
  155. {$IFDEF FPC_FEXPAND_TILDE}
  156. {Replace ~/ with $HOME/}
  157. if (Length (Pa) >= 1) and (Pa [1] = '~') and
  158. ((Pa [2] = DirectorySeparator) or (Length (Pa) = 1)) then
  159. begin
  160. {$IFDEF FPC_FEXPAND_SYSUTILS}
  161. {$IFDEF SYSUTILSUNICODE}
  162. S := PathStr(GetEnvironmentVariable ('HOME'));
  163. {$ELSE SYSUTILSUNICODE}
  164. S := ToSingleByteFileSystemEncodedFileName(GetEnvironmentVariable ('HOME'));
  165. {$ENDIF SYSUTILSUNICODE}
  166. {$ELSE FPC_FEXPAND_SYSUTILS}
  167. {$IFDEF FPC_FEXPAND_GETENV_PCHAR}
  168. S := StrPas (GetEnv ('HOME'));
  169. {$ELSE FPC_FEXPAND_GETENV_PCHAR}
  170. S := GetEnv ('HOME');
  171. {$ENDIF FPC_FEXPAND_GETENV_PCHAR}
  172. {$ENDIF FPC_FEXPAND_SYSUTILS}
  173. if (S = '') or (Length (S) = 1) and (Length (Pa) > 1)
  174. and (S [1] = DirectorySeparator) then
  175. Delete (Pa, 1, 1)
  176. else
  177. if S [Length (S)] = DirectorySeparator then
  178. Pa := S + Copy (Pa, 3, Length (Pa) - 2)
  179. else
  180. Pa := S + Copy (Pa, 2, Pred (Length (Pa)));
  181. end;
  182. {$ENDIF FPC_FEXPAND_TILDE}
  183. (* Do we have a drive/volume specification? *)
  184. {$IFDEF FPC_FEXPAND_VOLUMES}
  185. if PathStart > 1 then
  186. {$ELSE FPC_FEXPAND_VOLUMES}
  187. if (Length (Pa) > 1) and CharInSet(Pa [1], ['A'..'Z', 'a'..'z']) and
  188. (Pa [2] = DriveSeparator) and (DriveSeparator <> DirectorySeparator) then
  189. {$ENDIF FPC_FEXPAND_VOLUMES}
  190. begin
  191. (* We need to know current directory on given *)
  192. (* volume/drive _if_ such a thing is defined. *)
  193. {$IFDEF FPC_FEXPAND_DRIVES}
  194. {$IFNDEF FPC_FEXPAND_NO_DEFAULT_PATHS}
  195. {$IFDEF FPC_FEXPAND_VOLUMES}
  196. GetDirIO (Copy (Pa, 1, PathStart - 2), S);
  197. {$ELSE FPC_FEXPAND_VOLUMES}
  198. { Always uppercase driveletter }
  199. if CharInSet(Pa [1], ['a'..'z']) then
  200. Pa [1] := Chr (Ord (Pa [1]) and not ($20));
  201. GetDirIO (Ord (Pa [1]) - Ord ('A') + 1, S);
  202. {$ENDIF FPC_FEXPAND_VOLUMES}
  203. (* Do we have more than just drive/volume specification? *)
  204. if Length (Pa) = Pred (PathStart) then
  205. (* If not, just use the current directory for that drive/volume. *)
  206. Pa := S
  207. else
  208. (* If yes, find out whether the following path is relative or absolute. *)
  209. if Pa [PathStart] <> DirectorySeparator then
  210. {$IFDEF FPC_FEXPAND_VOLUMES}
  211. if Copy (Pa, 1, PathStart - 2) = Copy (S, 1, PathStart - 2)
  212. then
  213. {$ELSE FPC_FEXPAND_VOLUMES}
  214. if UpCase(Pa [1]) = UpCase(S [1]) then
  215. {$ENDIF FPC_FEXPAND_VOLUMES}
  216. begin
  217. { remove ending slash if it already exists }
  218. if S [Length (S)] = DirectorySeparator then
  219. SetLength(S,Length(S)-1);
  220. {$IFDEF FPC_FEXPAND_SYSUTILS}
  221. { not "Pa := S + DirectorySeparator + ..." because
  222. that will convert the result to
  223. DefaultSystemCodePage in case of RawByteString due
  224. to DirectorySeparator being an ansichar }
  225. TmpS := S;
  226. SetLength(TmpS, Length(TmpS) + 1);
  227. TmpS[Length(TmpS)] := DirectorySeparator;
  228. Pa := TmpS +
  229. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  230. {$ELSE FPC_FEXPAND_SYSUTILS}
  231. Pa := S + DirectorySeparator +
  232. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  233. {$ENDIF FPC_FEXPAND_SYSUTILS}
  234. end
  235. else
  236. begin
  237. TmpS := DriveSeparator + DirectorySeparator;
  238. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  239. SetCodePage(TmpS, DefaultFileSystemCodePage, false);
  240. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  241. {$IFDEF FPC_FEXPAND_VOLUMES}
  242. Pa := Copy (Pa, 1, PathStart - 2) + TmpS +
  243. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  244. {$ELSE FPC_FEXPAND_VOLUMES}
  245. { copy() instead of Pa[1] to preserve string code page }
  246. Pa := Copy (Pa, 1, 1) + TmpS +
  247. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  248. {$ENDIF FPC_FEXPAND_VOLUMES}
  249. end
  250. {$ENDIF FPC_FEXPAND_NO_DEFAULT_PATHS}
  251. end
  252. else
  253. {$ELSE FPC_FEXPAND_DRIVES}
  254. (* If drives are not supported, but a drive *)
  255. (* was supplied anyway, ignore (remove) it. *)
  256. Delete (Pa, 1, 2);
  257. end;
  258. {Check whether we don't have an absolute path already}
  259. if (Length (Pa) >= PathStart) and (Pa [PathStart] <> DirectorySeparator) or
  260. (Length (Pa) < PathStart) then
  261. {$ENDIF FPC_FEXPAND_DRIVES}
  262. begin
  263. (* Get current directory on selected drive/volume. *)
  264. GetDirIO (0, S);
  265. {$IFDEF FPC_FEXPAND_VOLUMES}
  266. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  267. PathStart := Pos (DriveSeparator, S);
  268. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  269. PathStart := Succ (Pos (DriveSeparator, S));
  270. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  271. {$ENDIF FPC_FEXPAND_VOLUMES}
  272. (* Do we have an absolute path without drive or volume? *)
  273. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  274. {$IFDEF FPC_FEXPAND_DRIVES}
  275. if (Length (Pa) > 0)
  276. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  277. and (Pa [1] = DriveSeparator)
  278. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  279. and (Pa [1] = DirectorySeparator)
  280. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  281. then
  282. begin
  283. {$IFDEF FPC_FEXPAND_UNC}
  284. {Do not touch network drive names}
  285. if (Length (Pa) > 1) and (Pa [2] = DirectorySeparator)
  286. and LFNSupport then
  287. begin
  288. PathStart := 3;
  289. {Find the start of the string of directories}
  290. while (PathStart <= Length (Pa)) and
  291. (Pa [PathStart] <> DirectorySeparator) do
  292. Inc (PathStart);
  293. if PathStart > Length (Pa) then
  294. {We have just a machine name...}
  295. if Length (Pa) = 2 then
  296. {...or not even that one}
  297. PathStart := 2
  298. else
  299. begin
  300. {$IFDEF FPC_FEXPAND_SYSUTILS}
  301. { no string concatenation to prevent code page
  302. conversion for RawByteString }
  303. SetLength(Pa, Length(Pa) + 1);
  304. Pa[Length(Pa)] := DirectorySeparator
  305. {$ELSE FPC_FEXPAND_SYSUTILS}
  306. Pa := Pa + DirectorySeparator;
  307. {$ENDIF FPC_FEXPAND_SYSUTILS}
  308. end
  309. else if PathStart < Length (Pa) then
  310. {We have a resource name as well}
  311. begin
  312. RootNotNeeded := true;
  313. {Let's continue in searching}
  314. repeat
  315. Inc (PathStart);
  316. until (PathStart > Length (Pa)) or
  317. (Pa [PathStart] = DirectorySeparator);
  318. end;
  319. end
  320. else
  321. {$ENDIF FPC_FEXPAND_UNC}
  322. begin
  323. {$IFDEF FPC_FEXPAND_VOLUMES}
  324. I := Pos (DriveSeparator, S);
  325. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  326. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  327. if (Pa [1] = DriveSeparator) then
  328. Delete (Pa, 1, 1);
  329. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  330. Pa := Copy (S, 1, I) + Pa;
  331. PathStart := I;
  332. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  333. TmpS := Copy (S, 1, Pred (I));
  334. SetLength(TmpS, Length(TmpS) + 1);
  335. TmpS[Length(TmpS)] := DriveSeparator;
  336. Pa := TmpS + Pa;
  337. PathStart := Succ (I);
  338. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  339. {$ELSE FPC_FEXPAND_VOLUMES}
  340. TmpS := S[1] + DriveSeparator;
  341. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  342. SetCodePage(TmpS, DefaultFileSystemCodePage, false);
  343. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  344. Pa := TmpS + Pa;
  345. {$ENDIF FPC_FEXPAND_VOLUMES}
  346. end;
  347. end
  348. else
  349. {$ENDIF FPC_FEXPAND_DRIVES}
  350. (* We already have a slash if root is the curent directory. *)
  351. if Length (S) = PathStart then
  352. Pa := S + Pa
  353. {$ELSE FPC_FEXPAND_DIRSEP_IS_CURDIR}
  354. (* More complex with DirectorySeparator as current directory *)
  355. if (S [Length (S)] = DriveSeparator)
  356. and (Pa [1] = DirectorySeparator) then
  357. Pa := S + Copy (Pa, 2, Pred (Length (Pa)))
  358. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  359. else
  360. (* We need an ending slash if FExpand was called *)
  361. (* with an empty string for compatibility, except *)
  362. (* for platforms where this is invalid. *)
  363. if Length (Pa) = 0 then
  364. begin
  365. Pa := S;
  366. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  367. {$IFDEF FPC_FEXPAND_SYSUTILS}
  368. { no string concatenation to prevent code page
  369. conversion for RawByteString }
  370. SetLength(Pa, Length(Pa) + 1);
  371. Pa[Length(Pa)] := DirectorySeparator
  372. {$ELSE FPC_FEXPAND_SYSUTILS}
  373. Pa := Pa + DirectorySeparator;
  374. {$ENDIF FPC_FEXPAND_SYSUTILS}
  375. {$ENDIF not FPC_FEXPAND_DIRSEP_IS_UPDIR}
  376. end
  377. else
  378. {$IFDEF FPC_FEXPAND_UPDIR_HELPER}
  379. if Pa [1] = DirectorySeparator then
  380. Pa := S + Pa
  381. else
  382. {$ENDIF FPC_FEXPAND_UPDIR_HELPER}
  383. begin
  384. {$IFDEF FPC_FEXPAND_SYSUTILS}
  385. { not "Pa := S + DirectorySeparator + Pa" because
  386. that will convert the result to
  387. DefaultSystemCodePage in case of RawByteString due
  388. to DirectorySeparator being an ansichar. Don't
  389. always use this code because in case of
  390. truncation with shortstrings the result will be
  391. different }
  392. TmpS := S;
  393. SetLength(TmpS, Length(TmpS) + 1);
  394. TmpS[Length(TmpS)] := DirectorySeparator;
  395. Pa := TmpS + Pa;
  396. {$ELSE FPC_FEXPAND_SYSUTILS}
  397. Pa := S + DirectorySeparator + Pa
  398. {$ENDIF FPC_FEXPAND_SYSUTILS}
  399. end;
  400. end;
  401. {Get string of directories to only process relative references on this one}
  402. Dirs := Copy (Pa, Succ (PathStart), Length (Pa) - PathStart);
  403. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  404. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  405. {First remove all references to '\.\'}
  406. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  407. while I <> 0 do
  408. begin
  409. Delete (Dirs, I, 2);
  410. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  411. end;
  412. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  413. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  414. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  415. {$IFDEF FPC_FEXPAND_MULTIPLE_UPDIR}
  416. {Now replace all references to '\...' with '\..\..'}
  417. I := Pos (DirectorySeparator + '...', Dirs);
  418. while I <> 0 do
  419. begin
  420. Insert (DirectorySeparator + '.', Dirs, I + 3);
  421. I := Pos (DirectorySeparator + '...', Dirs);
  422. end;
  423. {$ENDIF FPC_FEXPAND_MULTIPLE_UPDIR}
  424. {Now remove also all references to '\..\' + of course previous dirs..}
  425. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  426. while I <> 0 do
  427. begin
  428. J := Pred (I);
  429. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  430. Dec (J);
  431. Delete (Dirs, Succ (J), I - J + 3);
  432. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  433. end;
  434. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  435. {$IFDEF FPC_FEXPAND_UPDIR_HELPER}
  436. (* Now remove all references to '//' plus previous directories... *)
  437. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  438. while I <> 0 do
  439. begin
  440. J := Pred (I);
  441. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  442. Dec (J);
  443. Delete (Dirs, Succ (J), Succ (I - J));
  444. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  445. end;
  446. {$ENDIF FPC_FEXPAND_UPDIR_HELPER}
  447. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  448. {Then remove also a reference to '\..' at the end of line
  449. + the previous directory, of course,...}
  450. I := Pos (DirectorySeparator + '..', Dirs);
  451. if (I <> 0) and (I = Length (Dirs) - 2) then
  452. begin
  453. J := Pred (I);
  454. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  455. Dec (J);
  456. if (J = 0) then
  457. Dirs := ''
  458. else
  459. Delete (Dirs, Succ (J), I - J + 2);
  460. end;
  461. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  462. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  463. (* Remove a possible reference to '/' at the *)
  464. (* end of line plus the previous directory. *)
  465. I := Length (Dirs);
  466. if (I > 0) and (Dirs [I] = DirectorySeparator) then
  467. begin
  468. J := Pred (I);
  469. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  470. Dec (J);
  471. if (J = 0) then
  472. Dirs := ''
  473. else
  474. Delete (Dirs, J, Succ (I - J));
  475. end;
  476. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  477. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  478. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  479. {...and also a possible reference to '\.'}
  480. if (Length (Dirs) = 1) then
  481. begin
  482. if (Dirs [1] = '.') then
  483. {A special case}
  484. Dirs := ''
  485. end
  486. else
  487. if (Length (Dirs) <> 0) and (Dirs [Length (Dirs)] = '.') and
  488. (Dirs [Pred (Length (Dirs))] = DirectorySeparator) then
  489. Delete (Dirs,length(Dirs)-1,2);
  490. {Finally remove '.\' at the beginning of the string of directories...}
  491. while (Length (Dirs) >= 2) and (Dirs [1] = '.')
  492. and (Dirs [2] = DirectorySeparator) do
  493. Delete (Dirs, 1, 2);
  494. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  495. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  496. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  497. (* Remove possible (invalid) references to '/' at the beginning. *)
  498. while (Length (Dirs) >= 1) and (Dirs [1] = '/') do
  499. Delete (Dirs, 1, 1);
  500. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  501. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  502. {...and possible (invalid) references to '..\' as well}
  503. while (Length (Dirs) >= 3) and (Dirs [1] = '.') and (Dirs [2] = '.') and
  504. (Dirs [3] = DirectorySeparator) do
  505. Delete (Dirs, 1, 3);
  506. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  507. {Two special cases - '.' and '..' alone}
  508. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  509. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  510. if (Length (Dirs) = 1) and (Dirs [1] = '.') then
  511. Dirs := '';
  512. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  513. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  514. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  515. if (Length (Dirs) = 2) and (Dirs [1] = '.') and (Dirs [2] = '.') then
  516. Dirs := '';
  517. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  518. {Join the parts back to create the complete path}
  519. if Length (Dirs) = 0 then
  520. begin
  521. Pa := Copy (Pa, 1, PathStart);
  522. {$IFNDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  523. if Pa [PathStart] <> DirectorySeparator then
  524. begin
  525. {$IFDEF FPC_FEXPAND_SYSUTILS}
  526. { no string concatenation to prevent code page
  527. conversion for RawByteString }
  528. SetLength(Pa, Length(Pa) + 1);
  529. Pa[Length(Pa)] := DirectorySeparator
  530. {$ELSE FPC_FEXPAND_SYSUTILS}
  531. Pa := Pa + DirectorySeparator;
  532. {$ENDIF FPC_FEXPAND_SYSUTILS}
  533. end
  534. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  535. end
  536. else
  537. Pa := Copy (Pa, 1, PathStart) + Dirs;
  538. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  539. {Remove ending \ if not supplied originally, the original string
  540. wasn't empty (to stay compatible) and if not really needed}
  541. if (Pa [Length (Pa)] = DirectorySeparator)
  542. and ((Length (Pa) > PathStart) or
  543. {A special case with UNC paths}
  544. (RootNotNeeded and (Length (Pa) = PathStart)))
  545. {Reference to current directory at the end should be removed}
  546. and (Length (Path) <> 0)
  547. and (Path [Length (Path)] <> DirectorySeparator)
  548. then
  549. Delete (PA,length(PA),1);
  550. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  551. FExpand := Pa;
  552. end;
  553. (* Description of individual conditional defines supported for FExpand
  554. (disregard the used directory separators in examples, constant
  555. System.DirectorySeparator is used in the real implemenation, of course):
  556. FPC_FEXPAND_UNC - UNC ("Universal Naming Convention") paths are
  557. supported (usually used for networking, used in DOS (with
  558. networking support installed), OS/2, Win32 and at least some
  559. Netware versions as far as I remember. An example of such a path
  560. is '\\servername\sharename\some\path'.
  561. FPC_FEXPAND_DRIVES - drive letters are supported (DOS-like
  562. environments - DOS, OS/2, Win32). Example is 'C:\TEST'.
  563. FPC_FEXPAND_GETENV_PCHAR - an implementation of GetEnv returning
  564. PChar instead of a shortstring is available (Unix) to support
  565. long values of environment variables.
  566. FPC_FEXPAND_TILDE - expansion of '~/' to GetEnv('HOME') - Unix.
  567. Example: '~/some/path'.
  568. FPC_FEXPAND_VOLUMES - volumes are supported (similar to drives,
  569. but the name can be longer; used under Netware, Amiga and
  570. probably MacOS as far as I understand it correctly). Example:
  571. 'VolumeName:Some:Path' or 'ServerName/Volume:Some\Path'
  572. (Netware).
  573. FPC_FEXPAND_NO_DEFAULT_PATHS - Dos keeps information about the
  574. current directory for every drive. If some platform supports
  575. drives or volumes, but keeps no track of current directories for
  576. them (i.e. there's no support for "GetDir(DriveNumber, Dir)" or
  577. "GetDir(Volume, Dir)", but only for "GetDir (0, Dir)" (i.e. the
  578. overall current directory), you should define this. Otherwise
  579. constructs like 'C:Some\Path' refer a path relative to the
  580. current directory on the C: drive.
  581. FPC_FEXPAND_DRIVESEP_IS_ROOT - this means that DriveSeparator
  582. should be used as beginning of the "real" path for a particular
  583. drive or volume instead of the DirectorySeparator. This would be
  584. used in case that there is only one character (DriveSeparator)
  585. delimitting the drive letter or volume name from the remaining
  586. path _and_ the DriveSeparator marks the root of an absolute path
  587. in that case. Example - 'Volume:This/Is/Absolute/Path'.
  588. FPC_FEXPAND_NO_CURDIR - there is no support to refer to current
  589. directory explicitely (like '.' used under both Unix and DOS-like
  590. environments).
  591. FPC_FEXPAND_NO_DOTS_UPDIR - '..' cannot be used to refer to the
  592. upper directory.
  593. FPC_FEXPAND_DIRSEP_IS_UPDIR - DirectorySeparator at the beginning of
  594. a path (or doubled DirectorySeparator inside the path) refer to the
  595. parent directory, one more DirectorySeparator to parent directory of
  596. parent directory and so on (Amiga). Please, note that you can decide
  597. to support both '..' and DirectorySeparator as references to the parent
  598. directory at the same time for compatibility reasons - however this
  599. support makes it impossible to use anotherwise possibly valid name
  600. of '..'.
  601. FPC_FEXPAND_DIRSEP_IS_CURDIR - DirectorySeparator at the beginning of
  602. a path refers to the current directory (i.e. path beginning with
  603. DirectorySeparator is always a relative path). Two DirectorySeparator
  604. characters refer to the parent directory, three refer to parent
  605. directory of the parent directory and so on (MacOS).
  606. FPC_FEXPAND_MULTIPLE_UPDIR - grouping of more characters specifying
  607. upper directory references higher directory levels. Example: '...'
  608. (Netware).
  609. FPC_FEXPAND_SYSUTILS allows to reuse the same implementation for
  610. SysUtils.ExpandFileName by avoiding things specific for unit Dos.
  611. *)