fexpand.inc 22 KB

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