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: files L "Files"
16 *
17 * Summary: File system functions.
18 *
19 * List: *#lng/methods#,file_close,file_getsize,file_gettime,file_open,
20 file_read,file_setpos,file_settime,file_write,
21 *#lng/funcs#,copyfile,copyfiles,createdir,deletedir,deletefile,delfiles,
22 direxist,fileexist,getcurdir,getdrives,getdrivetype,getfileattrib,
23 getmodulename,getmodulepath,gettempdir,isequalfiles,movefile,
24 setattribnormal,setcurdir,setfileattrib,verifypath,
25 *Search and fileinfo functions,tfinfo,ffind,ffind_opfor,ffind_init,
26 getfileinfo,
27 *@Related Methods,arrstr_read,arrstr_write,buf_read,buf_write,
28 buf_writeappend,str_read,str_write,str_writeappend,
29 *
30 -----------------------------------------------------------------------------*/
31
32 /*-----------------------------------------------------------------------------
33 * Id: copyfile F
34 *
35 * Summary: Copy a file.
36 *
37 * Params: name - The name of an existing file.
38 newname - A new file name and path. If the file already exists, it /
39 will be overwritten.
40 *
41 * Return: #lng/retf#
42 *
43 -----------------------------------------------------------------------------*/
44
45 func uint copyfile( str name, str newname )
46 {
47 return CopyFile( name->buf.data, newname->buf.data, 0 )
48 }
49
50 /*-----------------------------------------------------------------------------
51 * Id: createdir F
52 *
53 * Summary: Create a directory.
54 *
55 * Params: name - The name of the directory being created.
56 *
57 * Return: #lng/retf#
58 *
59 -----------------------------------------------------------------------------*/
60
61 func uint createdir( str name )
62 {
63 return CreateDirectory( name.ptr(), 0 )
64 }
65
66 /*-----------------------------------------------------------------------------
67 * Id: deletedir F
68 *
69 * Summary: Delete a directory.
70 *
71 * Params: name - The name of the directory being deleted.
72 *
73 * Return: #lng/retf#
74 *
75 -----------------------------------------------------------------------------*/
76
77 func uint deletedir( str name )
78 {
79 return RemoveDirectory( name.ptr() )
80 }
81
82 /*-----------------------------------------------------------------------------
83 * Id: deletefile F
84 *
85 * Summary: Delete a file.
86 *
87 * Params: name - The name of the file being deleted.
88 *
89 * Return: #lng/retf#
90 *
91 -----------------------------------------------------------------------------*/
92
93 func uint deletefile( str name )
94 {
95 return DeleteFile( name.ptr() )
96 }
97
98 /*-----------------------------------------------------------------------------
99 * Id: getdrivetype F
100 *
101 * Summary: Get the type of a disk.
102 *
103 * Params: drive - The name of a disk with a closing slash. /
104 For example: #b(C:\)
105 *
106 * Return: Returns one of the following values: $$[drivetypes]
107 *
108 -----------------------------------------------------------------------------*/
109
110 func uint getdrivetype( str name )
111 {
112 if !&name : return GetDriveType( 0 )
113
114 return GetDriveType( name.fappendslash().ptr())
115 }
116
117 /*-----------------------------------------------------------------------------
118 * Id: getfileattrib F
119 *
120 * Summary: Getting file attributes.
121 *
122 * Params: name - Filename.
123 *
124 * Return: The function returns file attributes. It returns 0xFFFFFFFF in case
125 of an error.$$[fileattribs]
126 *
127 -----------------------------------------------------------------------------*/
128
129 func uint getfileattrib( str name )
130 {
131 return GetFileAttributes( name.ptr())
132 }
133
134 /*-----------------------------------------------------------------------------
135 * Id: setfileattrib F
136 *
137 * Summary: Set file attributes.
138 *
139 * Params: name - Filename.
140 attrib - File attributes. $$[fileattribs]
141 *
142 * Return: #lng/retf#
143 *
144 -----------------------------------------------------------------------------*/
145
146 func uint setfileattrib( str name, uint attrib )
147 {
148 return SetFileAttributes( name.ptr(), attrib )
149 }
150
151 /*-----------------------------------------------------------------------------
152 * Id: setattribnormal F
153 *
154 * Summary: Setting the attribute $FILE_ATTRIBUTE_NORMAL.
155 *
156 * Params: name - Filename.
157 *
158 * Return: #lng/retf#
159 *
160 -----------------------------------------------------------------------------*/
161
162 func uint setattribnormal( str name )
163 {
164 return setfileattrib( name, $FILE_ATTRIBUTE_NORMAL )
165 }
166
167 /*-----------------------------------------------------------------------------
168 * Id: fileexist F
169 *
170 * Summary: Checking if a file exists.
171 *
172 * Params: name - Filename.
173 *
174 * Return: The function returns 1, if the specified file exists.
175 *
176 -----------------------------------------------------------------------------*/
177
178 func uint fileexist( str name )
179 {
180 uint attr = getfileattrib( name )
181 return attr != 0xFFFFFFFF && !(attr & $FILE_ATTRIBUTE_DIRECTORY)
182 }
183
184 /*-----------------------------------------------------------------------------
185 * Id: direxist F
186 *
187 * Summary: Checking if a directory exists.
188 *
189 * Params: name - Directory name.
190 *
191 * Return: The function returns 1, if the specified directory exists.
192 *
193 -----------------------------------------------------------------------------*/
194
195 func uint direxist( str name )
196 {
197 uint attr = getfileattrib( name )
198 return attr != 0xFFFFFFFF && attr & $FILE_ATTRIBUTE_DIRECTORY
199 }
200
201 /*-----------------------------------------------------------------------------
202 * Id: getmodulename F
203 *
204 * Summary: Get the file name of the currently running application.
205 *
206 * Params: dest - The string for getting the name.
207 *
208 * Return: #lng/retpar( dest )
209 *
210 -----------------------------------------------------------------------------*/
211
212 func str getmodulename( str dest )
213 {
214 uint i
215
216 dest.reserve( 512 )
217 i = GetModuleFileName( 0, dest->buf.data, 511 )
218 dest.setlen( i );
219
220 return dest
221 }
222
223 /*-----------------------------------------------------------------------------
224 * Id: getmodulepath F
225 *
226 * Summary: Get the path to the running EXE file.
227 *
228 * Params: dest - Result string.
229 subfolder - Additional path. This string will be added to the /
230 obtained result. It can be empty.
231 *
232 * Return: #lng/retpar( dest )
233 *
234 -----------------------------------------------------------------------------*/
235
236 func str getmodulepath( str dest, str subfolder )
237 {
238 dest.fgetdir( getmodulename( dest ))
239 if &subfolder && *subfolder : dest.faddname( subfolder )
240 return dest
241 }
242
243 /*-----------------------------------------------------------------------------
244 * Id: movefile F
245 *
246 * Summary: Rename, move a file or a directory.
247 *
248 * Params: name - The name of an existing file or a directory.
249 newname - A new file name and path.
250 *
251 * Return: #lng/retf#
252 *
253 -----------------------------------------------------------------------------*/
254
255 func uint movefile( str name, str newname )
256 {
257 return MoveFile( name->buf.data, newname->buf.data )
258 }
259
260 /*-----------------------------------------------------------------------------
261 * Id: verifypath F
262 *
263 * Summary: Verifying a path and creating all absent directories.
264 *
265 * Params: name - The name of the path to be verified.
266 dirs - An array for getting all the directories being created. /
267 It can be 0->arrstr.
268 *
269 * Return: The function returns 1 if directories have been verified and created
270 successfully. In case of an error, the function returns 0 and the
271 last dirs item contains the name where there occurred an error
272 while creating a directory.
273 *
274 -----------------------------------------------------------------------------*/
275
276 func uint verifypath( str name, arrstr dirs )
277 {
278 str fullname drive
279 uint i
280 arrstr names
281
282 fullname.ffullname( name )
283 fullname.fdelslash()
284 if dirs : dirs.delete()
285
286 if direxist( fullname ) : return 1
287
288 drive.fgetdrive( fullname )
289 fullname.del( 0, *drive )
290 fullname.split( names, '\', 0 )
291
292 fornum i = 0,*names
293 {
294 drive.faddname( names[i] )
295 if !direxist( drive )
296 {
297 if dirs : dirs[ dirs.expand( 1 ) ] = drive
298 if !createdir( drive ) : return 0
299 }
300 }
301 return 1
302 }
303
304 /*-----------------------------------------------------------------------------
305 * Id: getcurdir F
306 *
307 * Summary: Getting the current directory.
308 *
309 * Params: dir - The string for getting the result.
310 *
311 * Return: #lng/retpar( dir )
312 *
313 -----------------------------------------------------------------------------*/
314
315 func str getcurdir( str dir )
316 {
317 dir.clear()
318 dir.reserve( 512 )
319 GetCurrentDirectory( 512, dir.ptr())
320 return dir.setlenptr()
321 }
322
323 /*-----------------------------------------------------------------------------
324 * Id: setcurdir F
325 *
326 * Summary: Setting the current directory.
327 *
328 * Params: dir - The name of the new current directory.
329 *
330 * Return: #lng/retf#
331 *
332 -----------------------------------------------------------------------------*/
333
334 func uint setcurdir( str dir )
335 {
336 return SetCurrentDirectory( dir.ptr())
337 }
338
339 /*-----------------------------------------------------------------------------
340 * Id: gettempdir F
341 *
342 * Summary: Get the temporary directory of the application. When this function
343 is called for the first time, in the temporary directory there will
344 be created a directory named genteeXX, where XX is a unique number
345 for this running application. When the application is closed, the
346 directory will be deleted with all its files.
347 *
348 * Params: dir - The string for getting the result.
349 *
350 * Return: #lng/retpar( dir )
351 *
352 -----------------------------------------------------------------------------*/
353
354 func str gettempdir( str dir )
355 {
356 dir.clear()
357 return dir = gettemp( )
358 }
359
360 /*-----------------------------------------------------------------------------
361 ** Id: getdrives F1
362 *
363 * Summary: Get the names of available disks.
364 *
365 * Return: The array (arrstr) of the disk names.
366 *
367 -----------------------------------------------------------------------------*/
368
369 func arrstr getdrives <result>()
370 {
371 buf stemp
372
373 stemp.reserve( 512 )
374 stemp.use = GetLogicalDriveStrings( 512, stemp.ptr())
375 stemp.getmultistr( result )
376 }
377
378
379