fexpand.inc 28 KB

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