1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2007, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: stringfile L "String - Filename"
16 *
17 * Summary: Filename strings. Methods for working with file names.
18 *
19 * List: *,str_faddname,str_fappendslash,str_fdellash,str_ffullname,
20 str_fgetdir,str_fgetdrive,str_fgetext,str_fgetparts,str_fnameext,
21 str_fsetext,str_fsetname,
22 str_fsetparts,str_fsplit,str_fwildcard
23 *
24 -----------------------------------------------------------------------------*/
25
26 /*-----------------------------------------------------------------------------
27 * Id: str_fappendslash F3
28 *
29 * Summary: Adding a slash. Add '\' to the end of a string if it is not there.
30 *
31 * Return: #lng/retobj#
32 *
33 -----------------------------------------------------------------------------*/
34
35 method str str.fappendslash()
36 {
37 if !this.islast( '\' ) : this.appendch( '\' )
38
39 return this
40 }
41
42 /*-----------------------------------------------------------------------------
43 * Id: str_faddname F2
44 *
45 * Summary: Adding a name. Add a file name or a directory to a path.
46 *
47 * Params: name - The name being added. It will be added after a slash.
48 *
49 * Return: #lng/retobj#
50 *
51 -----------------------------------------------------------------------------*/
52
53 method str str.faddname( str name )
54 {
55 if *this && *name : this.fappendslash()
56
57 return this += name
58 }
59
60 /*-----------------------------------------------------------------------------
61 * Id: str_fdellash F3
62 *
63 * Summary: Deleting the final slash. Delete the final '\' if it is there.
64 *
65 * Return: #lng/retobj#
66 *
67 -----------------------------------------------------------------------------*/
68
69 method str str.fdelslash()
70 {
71 return this.trim( '\', $TRIM_RIGHT )
72 }
73
74 /*-----------------------------------------------------------------------------
75 * Id: str_fgetdir F2
76 *
77 * Summary: Getting the directory name. The method removes the final name of a
78 file or directory.
79 *
80 * Params: name - Initial filename.
81 *
82 * Return: #lng/retobj#
83 *
84 -----------------------------------------------------------------------------*/
85
86 method str str.fgetdir( str name )
87 {
88 uint len = name.findchr( '\' )
89
90 if len >= *name : len = 0
91 if &name == &this : this.setlen( len )
92 else : this.substr( name, 0, len )
93
94 return this
95 }
96
97 /*-----------------------------------------------------------------------------
98 * Id: str_ffullname F2
99 *
100 * Summary: Getting the full name. The method gets the full path and name
101 of a file.
102 *
103 * Params: name - Initial filename.
104 *
105 * Return: #lng/retobj#
106 *
107 -----------------------------------------------------------------------------*/
108
109 method str str.ffullname( str name )
110 {
111 uint off
112
113 this.reserve( 512 )
114 this.setlen( GetFullPathName( name.ptr(), 512, this.ptr(), &off ))
115
116 return this
117 }
118
119 /*-----------------------------------------------------------------------------
120 * Id: str_fgetdrive F2
121 *
122 * Summary: Getting the name of a disk. Get the network name
123 (\\computer\share\) or the name of a disk (c:\).
124 *
125 * Params: name - Initial filename.
126 *
127 * Return: #lng/retobj#
128 *
129 -----------------------------------------------------------------------------*/
130
131 method str str.fgetdrive( str name )
132 {
133 uint i
134
135 this.ffullname( name )
136
137 if this[ 1 ] == ':'
138 {
139 i = 2
140 }
141 elif this[0] == '\' && this[1] == '\'
142 {
143 i = this.findchnum( '\', 4 )
144 }
145 this.setlen( i )
146
147 return this.fappendslash()
148 }
149
150 /*-----------------------------------------------------------------------------
151 * Id: str_fsplit F2
152 *
153 * Summary: Getting the directory and name of a file. The method splits the full
154 path into the name of the final file or directory
155 and the rest of the path.
156 *
157 * Params: dir - The string for getting the directory.
158 name - The string for getting the name of a file or directory.
159 *
160 -----------------------------------------------------------------------------*/
161
162 method str.fsplit( str dir, str name )
163 {
164 uint separ = this.findchr( '\' )
165 uint len = *this
166 uint off
167
168 off = ?( separ >= len, 0, separ + 1 )
169
170 name.copy( this.ptr() + off )
171 dir.substr( this, 0, ?( separ && separ < len, separ, 0 ))
172 }
173
174 /*-----------------------------------------------------------------------------
175 * Id: str_fnameext F2
176 *
177 * Summary: Getting the name of a file. Get the name of the filename or
178 directory from the full path.
179 *
180 * Params: name - Initial filename.
181 *
182 -----------------------------------------------------------------------------*/
183
184 method str str.fnameext( str name )
185 {
186 uint separ = name.findchr( '\' )
187 uint off = ?( separ >= *name, 0, separ + 1 )
188
189 return this.copy( name.ptr() + off )
190 }
191
192 /*-----------------------------------------------------------------------------
193 * Id: str_fgetparts F2
194 *
195 * Summary: Getting name components. Get the directory, name and extensions
196 of a file.
197 *
198 * Params: dir - The string for getting the directory. It can be 0->str.
199 fname - The string for getting the file name. It can be 0->str.
200 ext - The string for getting the file extension. It can be 0->str.
201 *
202 -----------------------------------------------------------------------------*/
203
204 method str.fgetparts( str dir, str fname, str ext )
205 {
206 uint dot = this.findchr( '.' )
207 uint separ = this.findchr( '\' )
208 uint off
209
210 if ext
211 {
212 if ( dot > separ || separ >= *this ) && dot < *this
213 {
214 ext.substr( this, dot + 1, *this - dot - 1 )
215 }
216 else : ext.clear()
217 }
218 if fname
219 {
220 off = ?( separ >= *this, 0, separ + 1 )
221 fname.substr( this, off, dot - off )
222 }
223 if dir
224 {
225 dir.substr( this, 0, ?( separ && separ < *this, separ, 0 ))
226 }
227 }
228
229 /*-----------------------------------------------------------------------------
230 * Id: str_fsetparts F2
231 *
232 * Summary: Compounding or modifying the name. Compound the name of a file
233 out of the path, name and extension. This function can be also used
234 to modify the path, name or extension of a file. In this case
235 if some component equals 0->str, it is left unmodified.
236 *
237 * Params: dir - Directory.
238 fname - Filename.
239 ext - File extension.
240 *
241 * Return: #lng/retobj#
242 *
243 -----------------------------------------------------------------------------*/
244
245 method str str.fsetparts( str dir, str fname, str ext )
246 {
247 str cdir cname cext
248
249 this.fgetparts( cdir, cname, cext )
250 this.clear( )
251
252 this = ?( dir, dir, cdir )
253
254 this.faddname( ?( fname, fname, cname ))
255
256 if ext || *cext
257 {
258 this.appendch( '.' )
259 this += ?( ext, ext, cext )
260 }
261 return this
262 }
263
264 /*-----------------------------------------------------------------------------
265 * Id: str_fsetext F2
266 *
267 * Summary: Modifying the extension. The method gets the file name with a new
268 extension.
269 *
270 * Params: name - Initial file name.
271 ext - File extension.
272 *
273 * Return: #lng/retobj#
274 *
275 -----------------------------------------------------------------------------*/
276
277 method str str.fsetext( str name, str ext )
278 {
279 uint dot = name.findchr( '.' )
280 uint separ = name.findchr( '\' )
281
282 this = name
283 if separ < dot || separ >= *name
284 {
285 this.setlen( dot )
286 }
287 if ext : this += ".\(ext)"
288 return this
289 }
290
291 /*-----------------------------------------------------------------------------
292 * Id: str_fsetext_1 FA
293 *
294 * Summary: Modifying the extension in the filename.
295 *
296 * Params: ext - File extension.
297 *
298 -----------------------------------------------------------------------------*/
299
300 method str str.fsetext( str ext )
301 {
302 return this.fsetext( this, ext )
303 }
304
305 /*-----------------------------------------------------------------------------
306 * Id: str_fgetext F3
307 *
308 * Summary: Get the extension. The method writes the file extension into
309 the result string.
310 *
311 * Return: The result string with the extension.
312 *
313 -----------------------------------------------------------------------------*/
314
315 method str str.fgetext< result >
316 {
317 uint dot = this.findchr( '.' )
318 uint separ = this.findchr( '\' )
319
320 if separ < dot || separ >= *this
321 {
322 result.substr( this, dot + 1, *this - dot - 1 )
323 }
324 }
325
326 /*-----------------------------------------------------------------------------
327 ** Id: str_fsetname F2
328 *
329 * Summary: Modifying the name of the file. The method modifies the current
330 filename.
331 *
332 * Params: filename - A new filename.
333 *
334 * Return: #lng/retobj#
335 *
336 -----------------------------------------------------------------------------*/
337
338 method str str.fsetname( str filename )
339 {
340 return this.fgetdir( this ).faddname( filename )
341 }
342
343 //--------------------------------------------------------------------------