fexpand.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. {$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. ((Pa [2] = DirectorySeparator) or (Length (Pa) = 1)) 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 current directory on selected drive/volume. *)
  269. GetDirIO (0, S);
  270. {$IFDEF FPC_FEXPAND_VOLUMES}
  271. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  272. PathStart := Pos (DriveSeparator, S);
  273. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  274. PathStart := Succ (Pos (DriveSeparator, S));
  275. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  276. {$ENDIF FPC_FEXPAND_VOLUMES}
  277. (* Do we have an absolute path without drive or volume? *)
  278. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  279. {$IFDEF FPC_FEXPAND_DRIVES}
  280. if (Length (Pa) > 0)
  281. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  282. and (Pa [1] = DriveSeparator)
  283. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  284. and (Pa [1] = DirectorySeparator)
  285. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  286. then
  287. begin
  288. {$IFDEF FPC_FEXPAND_UNC}
  289. {Do not touch network drive names}
  290. if (Length (Pa) > 1) and (Pa [2] = DirectorySeparator)
  291. and LFNSupport then
  292. begin
  293. PathStart := 3;
  294. {Find the start of the string of directories}
  295. while (PathStart <= Length (Pa)) and
  296. (Pa [PathStart] <> DirectorySeparator) do
  297. Inc (PathStart);
  298. if PathStart > Length (Pa) then
  299. {We have just a machine name...}
  300. if Length (Pa) = 2 then
  301. {...or not even that one}
  302. PathStart := 2
  303. else
  304. begin
  305. {$IFDEF FPC_FEXPAND_SYSUTILS}
  306. { no string concatenation to prevent code page
  307. conversion for RawByteString }
  308. SetLength(Pa, Length(Pa) + 1);
  309. Pa[Length(Pa)] := DirectorySeparator
  310. {$ELSE FPC_FEXPAND_SYSUTILS}
  311. Pa := Pa + DirectorySeparator;
  312. {$ENDIF FPC_FEXPAND_SYSUTILS}
  313. end
  314. else if PathStart < Length (Pa) then
  315. {We have a resource name as well}
  316. begin
  317. RootNotNeeded := true;
  318. {Let's continue in searching}
  319. repeat
  320. Inc (PathStart);
  321. until (PathStart > Length (Pa)) or
  322. (Pa [PathStart] = DirectorySeparator);
  323. end;
  324. end
  325. else
  326. {$ENDIF FPC_FEXPAND_UNC}
  327. begin
  328. {$IFDEF FPC_FEXPAND_VOLUMES}
  329. I := Pos (DriveSeparator, S);
  330. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  331. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  332. if (Pa [1] = DriveSeparator) then
  333. Delete (Pa, 1, 1);
  334. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  335. Pa := Copy (S, 1, I) + Pa;
  336. PathStart := I;
  337. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  338. TmpS := Copy (S, 1, Pred (I));
  339. SetLength(TmpS, Length(TmpS) + 1);
  340. TmpS[Length(TmpS)] := DriveSeparator;
  341. Pa := TmpS + Pa;
  342. PathStart := Succ (I);
  343. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  344. {$ELSE FPC_FEXPAND_VOLUMES}
  345. TmpS := S[1] + DriveSeparator;
  346. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  347. SetCodePage(TmpS, DefaultFileSystemCodePage, false);
  348. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  349. Pa := TmpS + Pa;
  350. {$ENDIF FPC_FEXPAND_VOLUMES}
  351. end;
  352. end
  353. else
  354. {$ENDIF FPC_FEXPAND_DRIVES}
  355. (* We already have a slash if root is the curent directory. *)
  356. if Length (S) = PathStart then
  357. Pa := S + Pa
  358. {$ELSE FPC_FEXPAND_DIRSEP_IS_CURDIR}
  359. (* More complex with DirectorySeparator as current directory *)
  360. if (S [Length (S)] = DriveSeparator)
  361. and (Pa [1] = DirectorySeparator) then
  362. Pa := S + Copy (Pa, 2, Pred (Length (Pa)))
  363. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  364. else
  365. (* We need an ending slash if FExpand was called *)
  366. (* with an empty string for compatibility, except *)
  367. (* for platforms where this is invalid. *)
  368. if Length (Pa) = 0 then
  369. begin
  370. Pa := S;
  371. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  372. {$IFDEF FPC_FEXPAND_SYSUTILS}
  373. { no string concatenation to prevent code page
  374. conversion for RawByteString }
  375. SetLength(Pa, Length(Pa) + 1);
  376. Pa[Length(Pa)] := DirectorySeparator
  377. {$ELSE FPC_FEXPAND_SYSUTILS}
  378. Pa := Pa + DirectorySeparator;
  379. {$ENDIF FPC_FEXPAND_SYSUTILS}
  380. {$ENDIF not FPC_FEXPAND_DIRSEP_IS_UPDIR}
  381. end
  382. else
  383. {$IFDEF FPC_FEXPAND_UPDIR_HELPER}
  384. if Pa [1] = DirectorySeparator then
  385. Pa := S + Pa
  386. else
  387. {$ENDIF FPC_FEXPAND_UPDIR_HELPER}
  388. begin
  389. {$IFDEF FPC_FEXPAND_SYSUTILS}
  390. { not "Pa := S + DirectorySeparator + Pa" because
  391. that will convert the result to
  392. DefaultSystemCodePage in case of RawByteString due
  393. to DirectorySeparator being an ansichar. Don't
  394. always use this code because in case of
  395. truncation with shortstrings the result will be
  396. different }
  397. TmpS := S;
  398. SetLength(TmpS, Length(TmpS) + 1);
  399. TmpS[Length(TmpS)] := DirectorySeparator;
  400. Pa := TmpS + Pa;
  401. {$ELSE FPC_FEXPAND_SYSUTILS}
  402. Pa := S + DirectorySeparator + Pa
  403. {$ENDIF FPC_FEXPAND_SYSUTILS}
  404. end;
  405. end;
  406. {Get string of directories to only process relative references on this one}
  407. Dirs := Copy (Pa, Succ (PathStart), Length (Pa) - PathStart);
  408. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  409. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  410. {Before anything else, remove doubled DirectorySeparator characters
  411. - technically invalid or at least useless, but ignored by most operating
  412. systems except for plain DOS.}
  413. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  414. while I <> 0 do
  415. begin
  416. J := Succ (I);
  417. while (Length (Dirs) > J) and (Dirs [Succ (J)] = DirectorySeparator) do
  418. Inc (J);
  419. Delete (Dirs, Succ (I), J - I);
  420. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  421. end;
  422. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  423. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  424. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  425. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  426. {First remove all references to '\.\'}
  427. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  428. while I <> 0 do
  429. begin
  430. Delete (Dirs, I, 2);
  431. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  432. end;
  433. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  434. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  435. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  436. {$IFDEF FPC_FEXPAND_MULTIPLE_UPDIR}
  437. {Now replace all references to '\...' with '\..\..'}
  438. I := Pos (DirectorySeparator + '...', Dirs);
  439. while I <> 0 do
  440. begin
  441. Insert (DirectorySeparator + '.', Dirs, I + 3);
  442. I := Pos (DirectorySeparator + '...', Dirs);
  443. end;
  444. {$ENDIF FPC_FEXPAND_MULTIPLE_UPDIR}
  445. {Now remove also all references to '\..\' + of course previous dirs..}
  446. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  447. while I <> 0 do
  448. begin
  449. J := Pred (I);
  450. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  451. Dec (J);
  452. Delete (Dirs, Succ (J), I - J + 3);
  453. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  454. end;
  455. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  456. {$IFDEF FPC_FEXPAND_UPDIR_HELPER}
  457. { Now remove all references to '//' or '::' plus previous directories... }
  458. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  459. while I <> 0 do
  460. begin
  461. J := Pred (I);
  462. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  463. Dec (J);
  464. Delete (Dirs, Succ (J), Succ (I - J));
  465. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  466. end;
  467. {$ENDIF FPC_FEXPAND_UPDIR_HELPER}
  468. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  469. {Then remove also a reference to '\..' at the end of line
  470. + the previous directory, of course,...}
  471. I := Pos (DirectorySeparator + '..', Dirs);
  472. if (I <> 0) and (I = Length (Dirs) - 2) then
  473. begin
  474. J := Pred (I);
  475. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  476. Dec (J);
  477. if (J = 0) then
  478. Dirs := ''
  479. else
  480. Delete (Dirs, Succ (J), I - J + 2);
  481. end;
  482. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  483. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  484. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  485. {...and also a possible reference to '\.'}
  486. if (Length (Dirs) = 1) then
  487. begin
  488. if (Dirs [1] = '.') then
  489. {A special case}
  490. Dirs := ''
  491. end
  492. else
  493. if (Length (Dirs) <> 0) and (Dirs [Length (Dirs)] = '.') and
  494. (Dirs [Pred (Length (Dirs))] = DirectorySeparator) then
  495. Delete (Dirs,length(Dirs)-1,2);
  496. {Finally remove '.\' at the beginning of the string of directories...}
  497. while (Length (Dirs) >= 2) and (Dirs [1] = '.')
  498. and (Dirs [2] = DirectorySeparator) do
  499. Delete (Dirs, 1, 2);
  500. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  501. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  502. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  503. (* Remove possible (invalid) references to '/' at the beginning. *)
  504. while (Length (Dirs) >= 1) and (Dirs [1] = '/') do
  505. Delete (Dirs, 1, 1);
  506. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  507. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  508. {...and possible (invalid) references to '..\' as well}
  509. while (Length (Dirs) >= 3) and (Dirs [1] = '.') and (Dirs [2] = '.') and
  510. (Dirs [3] = DirectorySeparator) do
  511. Delete (Dirs, 1, 3);
  512. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  513. {Two special cases - '.' and '..' alone}
  514. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  515. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  516. if (Length (Dirs) = 1) and (Dirs [1] = '.') then
  517. Dirs := '';
  518. {$ENDIF FPC_FEXPAND_DIRSEP_IS_CURDIR}
  519. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  520. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  521. if (Length (Dirs) = 2) and (Dirs [1] = '.') and (Dirs [2] = '.') then
  522. Dirs := '';
  523. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  524. {Join the parts back to create the complete path}
  525. if Length (Dirs) = 0 then
  526. begin
  527. Pa := Copy (Pa, 1, PathStart);
  528. {$IFNDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  529. if Pa [PathStart] <> DirectorySeparator then
  530. begin
  531. {$IFDEF FPC_FEXPAND_SYSUTILS}
  532. { no string concatenation to prevent code page
  533. conversion for RawByteString }
  534. SetLength(Pa, Length(Pa) + 1);
  535. Pa[Length(Pa)] := DirectorySeparator
  536. {$ELSE FPC_FEXPAND_SYSUTILS}
  537. Pa := Pa + DirectorySeparator;
  538. {$ENDIF FPC_FEXPAND_SYSUTILS}
  539. end
  540. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  541. end
  542. else
  543. Pa := Copy (Pa, 1, PathStart) + Dirs;
  544. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  545. {Remove ending \ if not supplied originally, the original string
  546. wasn't empty (to stay compatible) and if not really needed}
  547. if (Pa [Length (Pa)] = DirectorySeparator)
  548. and ((Length (Pa) > PathStart) or
  549. {A special case with UNC paths}
  550. (RootNotNeeded and (Length (Pa) = PathStart)))
  551. {Reference to current directory at the end should be removed}
  552. and (Length (Path) <> 0)
  553. and (Path [Length (Path)] <> DirectorySeparator)
  554. then
  555. Delete (PA,length(PA),1);
  556. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  557. {$IF defined(FPC_FEXPAND_SYSUTILS) and not defined(SYSUTILSUNICODE)}
  558. { return result in expected code page }
  559. SetCodePage(Pa,DefaultRTLFileSystemCodePage);
  560. {$ENDIF FPC_FEXPAND_SYSUTILS and not SYSUTILSUNICODE}
  561. FExpand := Pa;
  562. end;
  563. (* Description of individual conditional defines supported for FExpand
  564. (disregard the used directory separators in examples, constant
  565. System.DirectorySeparator is used in the real implemenation, of course):
  566. FPC_FEXPAND_UNC - UNC ("Universal Naming Convention") paths are
  567. supported (usually used for networking, used in DOS (with
  568. networking support installed), OS/2, Win32 and at least some
  569. Netware versions as far as I remember. An example of such a path
  570. is '\\servername\sharename\some\path'.
  571. FPC_FEXPAND_DRIVES - drive letters are supported (DOS-like
  572. environments - DOS, OS/2, Win32). Example is 'C:\TEST'.
  573. FPC_FEXPAND_GETENV_PCHAR - an implementation of GetEnv returning
  574. PChar instead of a shortstring is available (Unix) to support
  575. long values of environment variables.
  576. FPC_FEXPAND_TILDE - expansion of '~/' to GetEnv('HOME') - Unix.
  577. Example: '~/some/path'.
  578. FPC_FEXPAND_VOLUMES - volumes are supported (similar to drives,
  579. but the name can be longer; used under Netware, Amiga and
  580. probably MacOS as far as I understand it correctly). Example:
  581. 'VolumeName:Some:Path' or 'ServerName/Volume:Some\Path'
  582. (Netware).
  583. FPC_FEXPAND_NO_DEFAULT_PATHS - Dos keeps information about the
  584. current directory for every drive. If some platform supports
  585. drives or volumes, but keeps no track of current directories for
  586. them (i.e. there's no support for "GetDir(DriveNumber, Dir)" or
  587. "GetDir(Volume, Dir)", but only for "GetDir (0, Dir)" (i.e. the
  588. overall current directory), you should define this. Otherwise
  589. constructs like 'C:Some\Path' refer a path relative to the
  590. current directory on the C: drive.
  591. FPC_FEXPAND_DRIVESEP_IS_ROOT - this means that DriveSeparator
  592. should be used as beginning of the "real" path for a particular
  593. drive or volume instead of the DirectorySeparator. This would be
  594. used in case that there is only one character (DriveSeparator)
  595. delimitting the drive letter or volume name from the remaining
  596. path _and_ the DriveSeparator marks the root of an absolute path
  597. in that case. Example - 'Volume:This/Is/Absolute/Path'.
  598. FPC_FEXPAND_NO_CURDIR - there is no support to refer to current
  599. directory explicitely (like '.' used under both Unix and DOS-like
  600. environments).
  601. FPC_FEXPAND_NO_DOTS_UPDIR - '..' cannot be used to refer to the
  602. upper directory.
  603. FPC_FEXPAND_DIRSEP_IS_UPDIR - DirectorySeparator at the beginning of
  604. a path (or doubled DirectorySeparator inside the path) refer to the
  605. parent directory, one more DirectorySeparator to parent directory of
  606. parent directory and so on (Amiga). Please, note that you can decide
  607. to support both '..' and DirectorySeparator as references to the parent
  608. directory at the same time for compatibility reasons - however this
  609. support makes it impossible to use otherwise possibly valid name
  610. of '..'.
  611. FPC_FEXPAND_DIRSEP_IS_CURDIR - DirectorySeparator at the beginning of
  612. a path refers to the current directory (i.e. path beginning with
  613. DirectorySeparator is always a relative path). Two DirectorySeparator
  614. characters refer to the parent directory, three refer to parent
  615. directory of the parent directory and so on (MacOS).
  616. FPC_FEXPAND_MULTIPLE_UPDIR - grouping of more characters specifying
  617. upper directory references higher directory levels. Example: '...'
  618. (Netware).
  619. FPC_FEXPAND_SYSUTILS allows to reuse the same implementation for
  620. SysUtils.ExpandFileName by avoiding things specific for unit Dos.
  621. *)