fexpand.inc 23 KB

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