fexpand.inc 29 KB

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