Debian Codegraph @

src/base/castleutils_filenames.inc

raw
1 {%MainUnit castleutils.pas}
2 {
3 Copyright 2002-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Operations on filenames (i.e. they don't TOUCH, even read, real files) }
18
19 {$ifdef read_interface}
20
21 const
22 { Root dir name. Empty if not applicable to this OS. }
23 RootDir = {$ifdef UNIX} '/' {$endif}
24 {$ifdef MSWINDOWS} '' {$endif} ;
25
26 ExeExtension = {$ifdef MSWINDOWS} '.exe' {$else} '' {$endif};
27
28 {$ifndef FPC}
29 AllowDirectorySeparators = {$ifdef MSWINDOWS} ['/', '\'] {$else} ['/'] {$endif};
30 FileNameCaseSensitive = {$ifdef MSWINDOWS} false {$else} true {$endif};
31 {$endif}
32
33 { Remove from the FileName the last extension (including the dot).
34 Note that if the FileName had a couple of extensions (e.g. @code(blah.x3d.gz))
35 this will remove only the last one.
36 Will remove nothing if filename has no extension.
37
38 @italic(It is not adviced to use this function,
39 better to operate on URLs and MIME types instead of filenames
40 and extensions, see CastleUriUtils.) }
41 function DeleteFileExt(const FileName: string): string;
42
43 { Extracts @italic(two) last extensions from the filename, if it has two extensions.
44 If the filename has only one extension, returns that one extension,
45 if the filename has no extension --- returns empty string,
46 similar to ExtractFileExt.
47
48 This is useful to detect file types from filenames like @code(model.x3d.gz),
49 where ExtractFileExt returns only @code(.gz).
50 This function will return @code(.x3d.gz).
51
52 @italic(It is not adviced to use this function,
53 better to operate on URLs and MIME types instead of filenames
54 and extensions, see CastleUriUtils.) }
55 function ExtractFileDoubleExt(const FileName: string): string;
56
57 { Extracts from FileName the name of file, without directory,
58 without last extension and without any Windows drive letter.
59
60 @deprecated Deprecated, since we use URLs everywhere and also
61 because this has very low usage. Use DeleteURIExt(ExtractURIName(URL))
62 if you really need to. }
63 function ExtractOnlyFilename(const FileName: string): string; deprecated;
64
65 { Returns FileName with directory (path) part replaced
66 with given NewPath. NewPath @bold(must) contain trailing PathDelim.
67
68 @deprecated Deprecated, since we use URLs everywhere and also
69 because this has very low usage. }
70 function ChangeFilePath(const FileName, NewPath: string): string; deprecated;
71
72 { Include / exclude the last path delimiter, if necessary.
73 These are just comfortable shorter names for
74 IncludeTrailingPathDelimiter and ExcludeTrailingPathDelimiter.
75
76 For Delphi, we also fix treatment of slash on Windows:
77 it should be treated as a path delimiter, as Windows accepts
78 both slashes and backslashes.
79
80 @groupBegin }
81 function InclPathDelim(const s: string): string;
82 function ExclPathDelim(const s: string): string;
83 { @groupEnd }
84
85 { Check is the given Path absolute.
86
87 Path may point to directory or normal file,
88 it doesn't matter. Also it doesn't matter whether Path ends with PathDelim or not.
89
90 Note for Windows: while it's obvious that @code('c:\autoexec.bat') is an
91 absolute path, and @code('autoexec.bat') is not, there's a question
92 whether path like @code('\autoexec.bat') is absolute? It doesn't specify
93 drive letter, but it does specify full directory hierarchy on some drive.
94 This function treats this as @italic(not absolute), on the reasoning that
95 "not all information is contained in Path".
96
97 @seealso IsPathAbsoluteOnDrive }
98 function IsPathAbsolute(const Path: string): boolean;
99
100 { Just like IsPathAbsolute, but on Windows accepts also paths that specify
101 full directory tree without drive letter.
102
103 @seealso IsPathAbsolute }
104 function IsPathAbsoluteOnDrive(const Path: string): boolean;
105
106 { Checks is the directory name special, like "." or "..".
107
108 The precise definition of "special" is that you cannot ever create
109 or even have any filenames / directories named like this. }
110 function SpecialDirName(const DirectoryName: string): boolean;
111
112 { Add Suffix to the filename, right before extension.
113 Returns @code(DeleteFileExt(FileName) + Suffix + ExtractFileExt(FileName)). }
114 function AppendToFilename(const FileName, Suffix: string): string;
115
116 {$endif read_interface}
117
118 {$ifdef read_implementation}
119
120 function DeleteFileExt(const FileName: string): string;
121 begin
122 Result := ChangeFileExt(FileName, '');
123 end;
124
125 function ExtractFileDoubleExt(const FileName: string): string;
126 begin
127 Result := ExtractFileExt(FileName);
128 if Result <> '' then
129 Result := ExtractFileExt(ChangeFileExt(FileName, '')) + Result;
130 end;
131
132 function ExtractOnlyFileName(const FileName: string): string;
133 begin
134 result := DeleteFileExt(ExtractFileName(FileName));
135 end;
136
137 function ChangeFilePath(const FileName, NewPath: string): string;
138 begin
139 result := NewPath + ExtractFileName(FileName);
140 end;
141
142 function InclPathDelim(const s: string): string;
143 begin
144 {$if defined(MSWINDOWS) and not defined(FPC)}
145 { On Windows, also accept / as final path delimiter.
146 FPC does it automatically. }
147 if (S <> '') and (S[Length(S)] = '/') then Exit(S);
148 {$endif}
149 Result := IncludeTrailingPathDelimiter(S);
150 end;
151
152 function ExclPathDelim(const s: string): string;
153 begin
154 {$if defined(MSWINDOWS) and not defined(FPC)}
155 { On Windows, also accept / as final path delimiter.
156 FPC does it automatically. }
157 if (S <> '') and (S[Length(S)] = '/') then Exit(Copy(S, 1, Length(S) - 1));
158 {$endif}
159 Result := ExcludeTrailingPathDelimiter(S);
160 end;
161
162 { This is also defined in CastleStringUtils publicly,
163 but we don't want to use CastleStringUtils here, to keep our dependencies simple. }
164 function SCharIs(const s: string; index: integer; c: char): boolean; overload;
165 begin
166 Result := (index <= Length(s)) and (s[index] = c);
167 end;
168
169 type
170 TSetOfChars = SysUtils.TSysCharSet;
171
172 function SCharIs(const S: String; Index: Integer; const Chars: TSetOfChars): Boolean; overload;
173 begin
174 Result := (Index <= Length(s)) and CharInSet(S[index], Chars);
175 end;
176
177 function IsPathAbsolute(const Path: string): boolean;
178 begin
179 Result := {$ifdef UNIX} SCharIs(Path, 1, PathDelim) {$endif}
180 {$ifdef MSWINDOWS} SCharIs(Path, 2, DriveDelim) {$endif};
181 end;
182
183 function IsPathAbsoluteOnDrive(const Path: string): boolean;
184 begin
185 Result := IsPathAbsolute(Path)
186 {$ifdef MSWINDOWS} or SCharIs(Path, 1, AllowDirectorySeparators) {$endif}
187 end;
188
189 function SpecialDirName(const DirectoryName: string): boolean;
190 begin
191 Result := (DirectoryName = '.') or (DirectoryName = '..');
192 end;
193
194 function AppendToFilename(const FileName, Suffix: string): string;
195 begin
196 { Could be optimized, as DeleteFileExt and ExtractFileExt
197 do almost the same work twice. }
198 Result := DeleteFileExt(FileName) + Suffix + ExtractFileExt(FileName);
199 end;
200
201 {$endif read_implementation}