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 return getfileattrib( name ) != 0xFFFFFFFF
181 }
182
183 /*-----------------------------------------------------------------------------
184 * Id: direxist F
185 *
186 * Summary: Checking if a directory exists.
187 *
188 * Params: name - Directory name.
189 *
190 * Return: The function returns 1, if the specified directory exists.
191 *
192 -----------------------------------------------------------------------------*/
193
194 func uint direxist( str name )
195 {
196 uint attr = getfileattrib( name )
197 return attr != 0xFFFFFFFF && attr & $FILE_ATTRIBUTE_DIRECTORY
198 }
199
200 /*-----------------------------------------------------------------------------
201 * Id: getmodulename F
202 *
203 * Summary: Get the file name of the currently running application.
204 *
205 * Params: dest - The string for getting the name.
206 *
207 * Return: #lng/retpar( dest )
208 *
209 -----------------------------------------------------------------------------*/
210
211 func str getmodulename( str dest )
212 {
213 uint i
214
215 dest.reserve( 512 )
216 i = GetModuleFileName( 0, dest->buf.data, 511 )
217 dest.setlen( i );
218
219 return dest
220 }
221
222 /*-----------------------------------------------------------------------------
223 * Id: getmodulepath F
224 *
225 * Summary: Get the path to the running EXE file.
226 *
227 * Params: dest - Result string.
228 subfolder - Additional path. This string will be added to the /
229 obtained result. It can be empty.
230 *
231 * Return: #lng/retpar( dest )
232 *
233 -----------------------------------------------------------------------------*/
234
235 func str getmodulepath( str dest, str subfolder )
236 {
237 dest.fgetdir( getmodulename( dest ))
238 if &subfolder && *subfolder : dest.faddname( subfolder )
239 return dest
240 }
241
242 /*-----------------------------------------------------------------------------
243 * Id: movefile F
244 *
245 * Summary: Rename, move a file or a directory.
246 *
247 * Params: name - The name of an existing file or a directory.
248 newname - A new file name and path.
249 *
250 * Return: #lng/retf#
251 *
252 -----------------------------------------------------------------------------*/
253
254 func uint movefile( str name, str newname )
255 {
256 return MoveFile( name->buf.data, newname->buf.data )
257 }
258
259 /*-----------------------------------------------------------------------------
260 * Id: verifypath F
261 *
262 * Summary: Verifying a path and creating all absent directories.
263 *
264 * Params: name - The name of the path to be verified.
265 dirs - An array for getting all the directories being created. /
266 It can be 0->arrstr.
267 *
268 * Return: The function returns 1 if directories have been verified and created
269 successfully. In case of an error, the function returns 0 and the
270 last dirs item contains the name where there occurred an error
271 while creating a directory.
272 *
273 -----------------------------------------------------------------------------*/
274
275 func uint verifypath( str name, arrstr dirs )
276 {
277 str fullname drive
278 uint i
279 arrstr names
280
281 fullname.ffullname( name )
282 fullname.fdelslash()
283 if dirs : dirs.delete()
284
285 if direxist( fullname ) : return 1
286
287 drive.fgetdrive( fullname )
288 fullname.del( 0, *drive )
289 fullname.split( names, '\', 0 )
290
291 fornum i = 0,*names
292 {
293 drive.faddname( names[i] )
294 if !direxist( drive )
295 {
296 if dirs : dirs[ dirs.expand( 1 ) ] = drive
297 if !createdir( drive ) : return 0
298 }
299 }
300 return 1
301 }
302
303 /*-----------------------------------------------------------------------------
304 * Id: getcurdir F
305 *
306 * Summary: Getting the current directory.
307 *
308 * Params: dir - The string for getting the result.
309 *
310 * Return: #lng/retpar( dir )
311 *
312 -----------------------------------------------------------------------------*/
313
314 func str getcurdir( str dir )
315 {
316 dir.clear()
317 dir.reserve( 512 )
318 GetCurrentDirectory( 512, dir.ptr())
319 return dir.setlenptr()
320 }
321
322 /*-----------------------------------------------------------------------------
323 * Id: setcurdir F
324 *
325 * Summary: Setting the current directory.
326 *
327 * Params: dir - The name of the new current directory.
328 *
329 * Return: #lng/retf#
330 *
331 -----------------------------------------------------------------------------*/
332
333 func uint setcurdir( str dir )
334 {
335 return SetCurrentDirectory( dir.ptr())
336 }
337
338 /*-----------------------------------------------------------------------------
339 * Id: gettempdir F
340 *
341 * Summary: Get the temporary directory of the application. When this function
342 is called for the first time, in the temporary directory there will
343 be created a directory named genteeXX, where XX is a unique number
344 for this running application. When the application is closed, the
345 directory will be deleted with all its files.
346 *
347 * Params: dir - The string for getting the result.
348 *
349 * Return: #lng/retpar( dir )
350 *
351 -----------------------------------------------------------------------------*/
352
353 func str gettempdir( str dir )
354 {
355 dir.clear()
356 return dir = gettemp( )
357 }
358
359 /*-----------------------------------------------------------------------------
360 ** Id: getdrives F1
361 *
362 * Summary: Get the names of available disks.
363 *
364 * Return: The array (arrstr) of the disk names.
365 *
366 -----------------------------------------------------------------------------*/
367
368 func arrstr getdrives <result>()
369 {
370 buf stemp
371
372 stemp.reserve( 512 )
373 stemp.use = GetLogicalDriveStrings( 512, stemp.ptr())
374 stemp.getmultistr( result )
375 }
376
377