fexpand.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1997-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. A platform independent FExpand implementation
  13. ****************************************************************************}
  14. {$IFDEF FPC_FEXPAND_VOLUMES}
  15. {$IFNDEF FPC_FEXPAND_DRIVES}
  16. (* Volumes are just a special case of drives. *)
  17. {$DEFINE FPC_FEXPAND_DRIVES}
  18. {$ENDIF FPC_FEXPAND_DRIVES}
  19. {$ENDIF FPC_FEXPAND_VOLUMES}
  20. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  21. {$IFNDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  22. (* If DriveSeparator is used for upper directory, *)
  23. (* it cannot be used for marking root at the same time. *)
  24. {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  25. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  26. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  27. procedure GetDirIO (DriveNr: byte; var Dir: OpenString);
  28. (* GetDirIO is supposed to return the root of the given drive *)
  29. (* in case of an error for compatibility of FExpand with TP/BP. *)
  30. var
  31. OldInOutRes: word;
  32. begin
  33. OldInOutRes := InOutRes;
  34. InOutRes := 0;
  35. GetDir (DriveNr, Dir);
  36. InOutRes := OldInOutRes;
  37. end;
  38. {$IFDEF FPC_FEXPAND_VOLUMES}
  39. {$IFNDEF FPC_FEXPAND_NO_DEFAULT_PATHS}
  40. procedure GetDirIO (const VolumeName: OpenString; var Dir: OpenString);
  41. var
  42. OldInOutRes: word;
  43. begin
  44. OldInOutRes := InOutRes;
  45. InOutRes := 0;
  46. GetDir (VolumeName, Dir);
  47. InOutRes := OldInOutRes;
  48. end;
  49. {$ENDIF FPC_FEXPAND_NO_DEFAULT_PATHS}
  50. {$ENDIF FPC_FEXPAND_VOLUMES}
  51. function FExpand (const Path: PathStr): PathStr;
  52. (* LFNSupport boolean constant, variable or function must be declared for all
  53. the platforms, at least locally in the Dos unit implementation part.
  54. In addition, FPC_FEXPAND_UNC, FPC_FEXPAND_DRIVES, FPC_FEXPAND_GETENV_PCHAR,
  55. FPC_FEXPAND_TILDE, FPC_FEXPAND_VOLUMES, FPC_FEXPAND_NO_DEFAULT_PATHS,
  56. FPC_FEXPAND_DRIVESEP_IS_ROOT, FPC_FEXPAND_NO_CURDIR,
  57. FPC_FEXPAND_NO_DOTS_UPDIR and FPC_FEXPAND_DIRSEP_IS_UPDIR conditionals might
  58. be defined to specify FExpand behaviour.
  59. *)
  60. {$IFDEF FPC_FEXPAND_DRIVES}
  61. var
  62. PathStart: longint;
  63. {$ELSE FPC_FEXPAND_DRIVES}
  64. const
  65. PathStart = 1;
  66. {$ENDIF FPC_FEXPAND_DRIVES}
  67. {$IFDEF FPC_FEXPAND_UNC}
  68. var
  69. RootNotNeeded: boolean;
  70. {$ELSE FPC_FEXPAND_UNC}
  71. const
  72. RootNotNeeded = false;
  73. {$ENDIF FPC_FEXPAND_UNC}
  74. var S, Pa, Dirs: PathStr;
  75. I, J: longint;
  76. begin
  77. {$IFDEF FPC_FEXPAND_UNC}
  78. RootNotNeeded := false;
  79. {$ENDIF FPC_FEXPAND_UNC}
  80. (* First convert the path to uppercase if appropriate for current platform. *)
  81. if FileNameCaseSensitive then
  82. Pa := Path
  83. else
  84. Pa := UpCase (Path);
  85. (* Allow both '/' and '\' as directory separators *)
  86. (* by converting all to the native one. *)
  87. if DirectorySeparator = '\' then
  88. {Allow slash as backslash}
  89. begin
  90. for I := 1 to Length (Pa) do
  91. if Pa [I] = '/' then
  92. Pa [I] := DirectorySeparator
  93. end
  94. else
  95. {Allow backslash as slash}
  96. begin
  97. for I := 1 to Length (Pa) do
  98. if Pa [I] = '\' then
  99. Pa [I] := DirectorySeparator;
  100. end;
  101. (* PathStart is amount of characters to strip to get beginning *)
  102. (* of path without volume/drive specification. *)
  103. {$IFDEF FPC_FEXPAND_DRIVES}
  104. {$IFDEF FPC_FEXPAND_VOLUMES}
  105. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  106. PathStart := Pos (DriveSeparator, Pa);
  107. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  108. PathStart := Succ (Pos (DriveSeparator, Pa));
  109. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  110. {$ELSE FPC_FEXPAND_VOLUMES}
  111. PathStart := 3;
  112. {$ENDIF FPC_FEXPAND_VOLUMES}
  113. {$ENDIF FPC_FEXPAND_DRIVES}
  114. (* Expand tilde to home directory if appropriate. *)
  115. {$IFDEF FPC_FEXPAND_TILDE}
  116. {Replace ~/ with $HOME/}
  117. if (Length (Pa) >= 1) and (Pa [1] = '~') and
  118. ((Pa [2] = DirectorySeparator) or (Length (Pa) = 1)) then
  119. begin
  120. {$IFDEF FPC_FEXPAND_GETENV_PCHAR}
  121. S := StrPas (GetEnv ('HOME'));
  122. {$ELSE FPC_FEXPAND_GETENV_PCHAR}
  123. S := GetEnv ('HOME');
  124. {$ENDIF FPC_FEXPAND_GETENV_PCHAR}
  125. if (S = '') or (Length (S) = 1)
  126. and (S [1] = DirectorySeparator) then
  127. Delete (Pa, 1, 1)
  128. else
  129. if S [Length (S)] = DirectorySeparator then
  130. Pa := S + Copy (Pa, 3, Length (Pa) - 2)
  131. else
  132. Pa := S + Copy (Pa, 2, Pred (Length (Pa)));
  133. end;
  134. {$ENDIF FPC_FEXPAND_TILDE}
  135. (* Do we have a drive/volume specification? *)
  136. {$IFDEF FPC_FEXPAND_VOLUMES}
  137. if PathStart > 1 then
  138. {$ELSE FPC_FEXPAND_VOLUMES}
  139. if (Length (Pa) > 1) and (Pa [1] in ['A'..'Z', 'a'..'z']) and
  140. (Pa [2] = DriveSeparator) then
  141. {$ENDIF FPC_FEXPAND_VOLUMES}
  142. begin
  143. (* We need to know current directory on given *)
  144. (* volume/drive _if_ such a thing is defined. *)
  145. {$IFDEF FPC_FEXPAND_DRIVES}
  146. {$IFNDEF FPC_FEXPAND_NO_DEFAULT_PATHS}
  147. {$IFDEF FPC_FEXPAND_VOLUMES}
  148. GetDirIO (Copy (Pa, 1, PathStart - 2), S);
  149. {$ELSE FPC_FEXPAND_VOLUMES}
  150. { Always uppercase driveletter }
  151. if (Pa [1] in ['a'..'z']) then
  152. Pa [1] := Chr (Ord (Pa [1]) and not ($20));
  153. GetDirIO (Ord (Pa [1]) - Ord ('A') + 1, S);
  154. {$ENDIF FPC_FEXPAND_VOLUMES}
  155. (* Do we have more than just drive/volume specification? *)
  156. if Length (Pa) = Pred (PathStart) then
  157. (* If not, just use the current directory for that drive/volume. *)
  158. Pa := S
  159. else
  160. (* If yes, find out whether the following path is relative or absolute. *)
  161. if Pa [PathStart] <> DirectorySeparator then
  162. {$IFDEF FPC_FEXPAND_VOLUMES}
  163. if Copy (Pa, 1, PathStart - 2) = Copy (S, 1, PathStart - 2)
  164. then
  165. {$ELSE FPC_FEXPAND_VOLUMES}
  166. if Pa [1] = S [1] then
  167. {$ENDIF FPC_FEXPAND_VOLUMES}
  168. begin
  169. { remove ending slash if it already exists }
  170. if S [Length (S)] = DirectorySeparator then
  171. Dec (S [0]);
  172. Pa := S + DirectorySeparator +
  173. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  174. end
  175. else
  176. {$IFDEF FPC_FEXPAND_VOLUMES}
  177. Pa := Copy (Pa, 1, PathStart - 2) + DriveSeparator
  178. + DirectorySeparator +
  179. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  180. {$ELSE FPC_FEXPAND_VOLUMES}
  181. Pa := Pa [1] + DriveSeparator + DirectorySeparator +
  182. Copy (Pa, PathStart, Length (Pa) - PathStart + 1)
  183. {$ENDIF FPC_FEXPAND_VOLUMES}
  184. {$ENDIF FPC_FEXPAND_NO_DEFAULT_PATHS}
  185. end
  186. else
  187. {$ELSE FPC_FEXPAND_DRIVES}
  188. (* If drives are not supported, but a drive *)
  189. (* was supplied anyway, ignore (remove) it. *)
  190. Delete (Pa, 1, 2);
  191. end;
  192. {Check whether we don't have an absolute path already}
  193. if (Length (Pa) >= PathStart) and (Pa [PathStart] <> DirectorySeparator) or
  194. (Length (Pa) < PathStart) then
  195. {$ENDIF FPC_FEXPAND_DRIVES}
  196. begin
  197. (* Get current directory on selected drive/volume. *)
  198. GetDirIO (0, S);
  199. {$IFDEF FPC_FEXPAND_VOLUMES}
  200. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  201. PathStart := Pos (DriveSeparator, S);
  202. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  203. PathStart := Succ (Pos (DriveSeparator, S));
  204. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  205. {$ENDIF FPC_FEXPAND_VOLUMES}
  206. (* Do we have an absolute path? *)
  207. {$IFDEF FPC_FEXPAND_DRIVES}
  208. if (Length (Pa) > 0)
  209. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  210. and (Pa [1] = DriveSeparator)
  211. {$ELSE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  212. and (Pa [1] = DirectorySeparator)
  213. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  214. then
  215. begin
  216. {$IFDEF FPC_FEXPAND_UNC}
  217. {Do not touch network drive names}
  218. if (Length (Pa) > 1) and (Pa [2] = DirectorySeparator)
  219. and LFNSupport then
  220. begin
  221. PathStart := 3;
  222. {Find the start of the string of directories}
  223. while (PathStart <= Length (Pa)) and
  224. (Pa [PathStart] <> DirectorySeparator) do
  225. Inc (PathStart);
  226. if PathStart > Length (Pa) then
  227. {We have just a machine name...}
  228. if Length (Pa) = 2 then
  229. {...or not even that one}
  230. PathStart := 2
  231. else
  232. Pa := Pa + DirectorySeparator else
  233. if PathStart < Length (Pa) then
  234. {We have a resource name as well}
  235. begin
  236. RootNotNeeded := true;
  237. {Let's continue in searching}
  238. repeat
  239. Inc (PathStart);
  240. until (PathStart > Length (Pa)) or
  241. (Pa [PathStart] = DirectorySeparator);
  242. end;
  243. end
  244. else
  245. {$ENDIF FPC_FEXPAND_UNC}
  246. {$IFDEF FPC_FEXPAND_VOLUMES}
  247. begin
  248. I := Pos (DriveSeparator, S);
  249. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  250. {$IFDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  251. if (Pa [1] = DriveSeparator) then
  252. Delete (Pa, 1, 1);
  253. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  254. Pa := Copy (S, 1, I) + Pa;
  255. PathStart := I;
  256. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  257. Pa := Copy (S, 1, Pred (I)) + DriveSeparator + Pa;
  258. PathStart := Succ (I);
  259. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  260. end;
  261. {$ELSE FPC_FEXPAND_VOLUMES}
  262. Pa := S [1] + DriveSeparator + Pa;
  263. {$ENDIF FPC_FEXPAND_VOLUMES}
  264. end
  265. else
  266. {$ENDIF FPC_FEXPAND_DRIVES}
  267. (* We already have a slash if root is the curent directory. *)
  268. if Length (S) = PathStart then
  269. Pa := S + Pa
  270. else
  271. (* We need an ending slash if FExpand was called *)
  272. (* with an empty string for compatibility, except *)
  273. (* for platforms where this is invalid. *)
  274. if Length (Pa) = 0 then
  275. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  276. Pa := S
  277. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  278. Pa := S + DirectorySeparator
  279. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  280. else
  281. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  282. if Pa [1] = DirectorySeparator then
  283. Pa := S + Pa
  284. else
  285. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  286. Pa := S + DirectorySeparator + Pa;
  287. end;
  288. {Get string of directories to only process relative references on this one}
  289. Dirs := Copy (Pa, Succ (PathStart), Length (Pa) - PathStart);
  290. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  291. {First remove all references to '\.\'}
  292. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  293. while I <> 0 do
  294. begin
  295. Delete (Dirs, I, 2);
  296. I := Pos (DirectorySeparator + '.' + DirectorySeparator, Dirs);
  297. end;
  298. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  299. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  300. {Now remove also all references to '\..\' + of course previous dirs..}
  301. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  302. while I <> 0 do
  303. begin
  304. J := Pred (I);
  305. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  306. Dec (J);
  307. Delete (Dirs, Succ (J), I - J + 3);
  308. I := Pos (DirectorySeparator + '..' + DirectorySeparator, Dirs);
  309. end;
  310. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  311. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  312. (* Now remove all references to '//' plus previous directories... *)
  313. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  314. while I <> 0 do
  315. begin
  316. J := Pred (I);
  317. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  318. Dec (J);
  319. Delete (Dirs, Succ (J), Succ (I - J));
  320. I := Pos (DirectorySeparator + DirectorySeparator, Dirs);
  321. end;
  322. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  323. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  324. {Then remove also a reference to '\..' at the end of line
  325. + the previous directory, of course,...}
  326. I := Pos (DirectorySeparator + '..', Dirs);
  327. if (I <> 0) and (I = Length (Dirs) - 2) then
  328. begin
  329. J := Pred (I);
  330. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  331. Dec (J);
  332. if (J = 0) then
  333. Dirs := ''
  334. else
  335. Delete (Dirs, Succ (J), I - J + 2);
  336. end;
  337. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  338. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  339. (* Remove a possible reference to '/' at the *)
  340. (* end of line plus the previous directory. *)
  341. I := Length (Dirs);
  342. if (I > 0) and (Dirs [I] = DirectorySeparator) then
  343. begin
  344. J := Pred (I);
  345. while (J > 0) and (Dirs [J] <> DirectorySeparator) do
  346. Dec (J);
  347. if (J = 0) then
  348. Dirs := ''
  349. else
  350. Delete (Dirs, J, Succ (I - J));
  351. end;
  352. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  353. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  354. {...and also a possible reference to '\.'}
  355. if (Length (Dirs) = 1) then
  356. begin
  357. if (Dirs [1] = '.') then
  358. {A special case}
  359. Dirs := ''
  360. end
  361. else
  362. if (Length (Dirs) <> 0) and (Dirs [Length (Dirs)] = '.') and
  363. (Dirs [Pred (Length (Dirs))] = DirectorySeparator) then
  364. Dec (Dirs [0], 2);
  365. {Finally remove '.\' at the beginning of the string of directories...}
  366. while (Length (Dirs) >= 2) and (Dirs [1] = '.')
  367. and (Dirs [2] = DirectorySeparator) do
  368. Delete (Dirs, 1, 2);
  369. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  370. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  371. (* Remove possible (invalid) references to '/' at the beginning. *)
  372. while (Length (Dirs) >= 1) and (Dirs [1] = '/') do
  373. Delete (Dirs, 1, 1);
  374. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  375. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  376. {...and possible (invalid) references to '..\' as well}
  377. while (Length (Dirs) >= 3) and (Dirs [1] = '.') and (Dirs [2] = '.') and
  378. (Dirs [3] = DirectorySeparator) do
  379. Delete (Dirs, 1, 3);
  380. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  381. {Two special cases - '.' and '..' alone}
  382. {$IFNDEF FPC_FEXPAND_NO_CURDIR}
  383. if (Length (Dirs) = 1) and (Dirs [1] = '.') then
  384. Dirs := '';
  385. {$ENDIF FPC_FEXPAND_NO_CURDIR}
  386. {$IFNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  387. if (Length (Dirs) = 2) and (Dirs [1] = '.') and (Dirs [2] = '.') then
  388. Dirs := '';
  389. {$ENDIF FPC_FEXPAND_NO_DOTS_UPDIR}
  390. {Join the parts back to create the complete path}
  391. if Length (Dirs) = 0 then
  392. begin
  393. Pa := Copy (Pa, 1, PathStart);
  394. {$IFNDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  395. if Pa [PathStart] <> DirectorySeparator then
  396. Pa := Pa + DirectorySeparator;
  397. {$ENDIF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  398. end
  399. else
  400. Pa := Copy (Pa, 1, PathStart) + Dirs;
  401. {$IFNDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  402. {Remove ending \ if not supplied originally, the original string
  403. wasn't empty (to stay compatible) and if not really needed}
  404. if (Pa [Length (Pa)] = DirectorySeparator)
  405. and ((Length (Pa) > PathStart) or
  406. {A special case with UNC paths}
  407. (RootNotNeeded and (Length (Pa) = PathStart))) and
  408. (Length (Path) <> 0)
  409. and (Path [Length (Path)] <> DirectorySeparator) then
  410. Dec (Pa [0]);
  411. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  412. FExpand := Pa;
  413. end;
  414. {
  415. $Log$
  416. Revision 1.15 2002-12-07 16:26:39 hajny
  417. * '//' behaviour for Amiga corrected
  418. Revision 1.14 2002/12/01 20:46:44 hajny
  419. * Amiga support hopefully finished
  420. Revision 1.13 2002/11/25 21:03:57 hajny
  421. * Amiga fixes (among others)
  422. Revision 1.12 2002/11/24 15:49:22 hajny
  423. * make use of constants available in the system unit
  424. Revision 1.11 2002/09/07 15:07:45 peter
  425. * old logs removed and tabs fixed
  426. Revision 1.10 2002/05/14 19:25:24 hajny
  427. * fix for bug 1964 merged
  428. Revision 1.9 2002/03/03 15:19:36 carl
  429. * fixes unix conversion of slashes
  430. }