fexpand.inc 23 KB

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