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_fdelslash,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_fdelslash 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 this.trimrsys()
72 return this.trim( '\', $TRIM_RIGHT )
73 }
74
75 /*-----------------------------------------------------------------------------
76 * Id: str_fgetdir F2
77 *
78 * Summary: Getting the directory name. The method removes the final name of a
79 file or directory.
80 *
81 * Params: name - Initial filename.
82 *
83 * Return: #lng/retobj#
84 *
85 -----------------------------------------------------------------------------*/
86
87 method str str.fgetdir( str name )
88 {
89 uint len = name.findchr( '\' )
90
91 if len >= *name : len = 0
92 if &name == &this : this.setlen( len )
93 else : this.substr( name, 0, len )
94
95 return this
96 }
97
98 /*-----------------------------------------------------------------------------
99 * Id: str_ffullname F2
100 *
101 * Summary: Getting the full name. The method gets the full path and name
102 of a file.
103 *
104 * Params: name - Initial filename.
105 *
106 * Return: #lng/retobj#
107 *
108 -----------------------------------------------------------------------------*/
109
110 method str str.ffullname( str name )
111 {
112 uint off
113
114 this.reserve( 512 )
115 this.setlen( GetFullPathName( name.ptr(), 512, this.ptr(), &off ))
116
117 return this
118 }
119
120 /*-----------------------------------------------------------------------------
121 * Id: str_fgetdrive F2
122 *
123 * Summary: Getting the name of a disk. Get the network name
124 (\\computer\share\) or the name of a disk (c:\).
125 *
126 * Params: name - Initial filename.
127 *
128 * Return: #lng/retobj#
129 *
130 -----------------------------------------------------------------------------*/
131
132 method str str.fgetdrive( str name )
133 {
134 uint i
135
136 this.ffullname( name )
137
138 if this[ 1 ] == ':'
139 {
140 i = 2
141 }
142 elif this[0] == '\' && this[1] == '\'
143 {
144 i = this.findchnum( '\', 4 )
145 }
146 this.setlen( i )
147
148 return this.fappendslash()
149 }
150
151 /*-----------------------------------------------------------------------------
152 * Id: str_fsplit F2
153 *
154 * Summary: Getting the directory and name of a file. The method splits the full
155 path into the name of the final file or directory
156 and the rest of the path.
157 *
158 * Params: dir - The string for getting the directory.
159 name - The string for getting the name of a file or directory.
160 *
161 -----------------------------------------------------------------------------*/
162
163 method str.fsplit( str dir, str name )
164 {
165 uint separ = this.findchr( '\' )
166 uint len = *this
167 uint off
168
169 off = ?( separ >= len, 0, separ + 1 )
170
171 name.copy( this.ptr() + off )
172 dir.substr( this, 0, ?( separ && separ < len, separ, 0 ))
173 }
174
175 /*-----------------------------------------------------------------------------
176 * Id: str_fnameext F2
177 *
178 * Summary: Getting the name of a file. Get the name of the filename or
179 directory from the full path.
180 *
181 * Params: name - Initial filename.
182 *
183 -----------------------------------------------------------------------------*/
184
185 method str str.fnameext( str name )
186 {
187 uint separ = name.findchr( '\' )
188 uint off = ?( separ >= *name, 0, separ + 1 )
189
190 return this.copy( name.ptr() + off )
191 }
192
193 /*-----------------------------------------------------------------------------
194 * Id: str_fgetparts F2
195 *
196 * Summary: Getting name components. Get the directory, name and extensions
197 of a file.
198 *
199 * Params: dir - The string for getting the directory. It can be 0->str.
200 fname - The string for getting the file name. It can be 0->str.
201 ext - The string for getting the file extension. It can be 0->str.
202 *
203 -----------------------------------------------------------------------------*/
204
205 method str.fgetparts( str dir, str fname, str ext )
206 {
207 uint dot = this.findchr( '.' )
208 uint separ = this.findchr( '\' )
209 uint off
210
211 if ext
212 {
213 if ( dot > separ || separ >= *this ) && dot < *this
214 {
215 ext.substr( this, dot + 1, *this - dot - 1 )
216 }
217 else : ext.clear()
218 }
219 if fname
220 {
221 off = ?( separ >= *this, 0, separ + 1 )
222 fname.substr( this, off, dot - off )
223 }
224 if dir
225 {
226 dir.substr( this, 0, ?( separ && separ < *this, separ, 0 ))
227 }
228 }
229
230 /*-----------------------------------------------------------------------------
231 * Id: str_fsetparts F2
232 *
233 * Summary: Compounding or modifying the name. Compound the name of a file
234 out of the path, name and extension. This function can be also used
235 to modify the path, name or extension of a file. In this case
236 if some component equals 0->str, it is left unmodified.
237 *
238 * Params: dir - Directory.
239 fname - Filename.
240 ext - File extension.
241 *
242 * Return: #lng/retobj#
243 *
244 -----------------------------------------------------------------------------*/
245
246 method str str.fsetparts( str dir, str fname, str ext )
247 {
248 str cdir cname cext
249
250 this.fgetparts( cdir, cname, cext )
251 this.clear( )
252
253 this = ?( dir, dir, cdir )
254
255 this.faddname( ?( fname, fname, cname ))
256
257 if ext || *cext
258 {
259 this.appendch( '.' )
260 this += ?( ext, ext, cext )
261 }
262 return this
263 }
264
265 /*-----------------------------------------------------------------------------
266 * Id: str_fsetext F2
267 *
268 * Summary: Modifying the extension. The method gets the file name with a new
269 extension.
270 *
271 * Params: name - Initial file name.
272 ext - File extension.
273 *
274 * Return: #lng/retobj#
275 *
276 -----------------------------------------------------------------------------*/
277
278 method str str.fsetext( str name, str ext )
279 {
280 uint dot = name.findchr( '.' )
281 uint separ = name.findchr( '\' )
282
283 this = name
284 if separ < dot || separ >= *name
285 {
286 this.setlen( dot )
287 }
288 if ext : this += ".\(ext)"
289 return this
290 }
291
292 /*-----------------------------------------------------------------------------
293 * Id: str_fsetext_1 FA
294 *
295 * Summary: Modifying the extension in the filename.
296 *
297 * Params: ext - File extension.
298 *
299 -----------------------------------------------------------------------------*/
300
301 method str str.fsetext( str ext )
302 {
303 return this.fsetext( this, ext )
304 }
305
306 /*-----------------------------------------------------------------------------
307 * Id: str_fgetext F3
308 *
309 * Summary: Get the extension. The method writes the file extension into
310 the result string.
311 *
312 * Return: The result string with the extension.
313 *
314 -----------------------------------------------------------------------------*/
315
316 method str str.fgetext< result >
317 {
318 uint dot = this.findchr( '.' )
319 uint separ = this.findchr( '\' )
320
321 if separ < dot || separ >= *this
322 {
323 result.substr( this, dot + 1, *this - dot - 1 )
324 }
325 }
326
327 /*-----------------------------------------------------------------------------
328 ** Id: str_fsetname F2
329 *
330 * Summary: Modifying the name of the file. The method modifies the current
331 filename.
332 *
333 * Params: filename - A new filename.
334 *
335 * Return: #lng/retobj#
336 *
337 -----------------------------------------------------------------------------*/
338
339 method str str.fsetname( str filename )
340 {
341 return this.fgetdir( this ).faddname( filename )
342 }
343
344 //--------------------------------------------------------------------------
345