fexpand.inc 22 KB

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