fexpand.inc 23 KB

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