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 define <export> {
15 /*-----------------------------------------------------------------------------
16 * Id: fileflags D
17 *
18 * Summary: File flags for file.open.
19 *
20 -----------------------------------------------------------------------------*/
21 OP_READONLY = 0x0001 // Open as read-only.
22 OP_EXCLUSIVE = 0x0002 // Open in the exclusive mode.
23 OP_CREATE = 0x0004 // Create the file.
24 OP_ALWAYS = 0x0008 // Create the file only if it does not exist.
25
26 //-----------------------------------------------------------------------------
27 }
28
29 /*-----------------------------------------------------------------------------
30 * Id: tfile T file
31 *
32 * Summary: File structure.
33 *
34 -----------------------------------------------------------------------------*/
35
36 type file {
37 uint fopen // 1 if the file is opened.
38 uint handle // The handle of the opened file.
39 str name // The name of the file.
40 }
41
42 /*-----------------------------------------------------------------------------
43 * Id: file_close F3
44 *
45 * Summary: Close a file.
46 *
47 * Return: #lng/retf#
48 *
49 -----------------------------------------------------------------------------*/
50
51 method uint file.close( )
52 {
53 if .fopen
54 {
55 .fopen = 0
56 return CloseHandle( .handle )
57 }
58 return 0
59 }
60
61 /*-----------------------------------------------------------------------------
62 * Id: file_open F2
63 *
64 * Summary: Open a file.
65 *
66 * Params: name - The name of the file to be opened.
67 flag - The following flags can be used.$$[fileflags]
68 *
69 * Return: #lng/retf#
70 *
71 -----------------------------------------------------------------------------*/
72
73 method uint file.open( str name, uint flag )
74 {
75 .name = name
76 .handle = CreateFile(
77 name.ptr(),
78 ?( flag & $OP_READONLY, $GENERIC_READ, $GENERIC_RW ),
79 ?( flag & $OP_EXCLUSIVE, 0,
80 ?( flag & $OP_READONLY, $FILE_SHARE_RW, $FILE_SHARE_RW )),
81 0,
82 ?( flag & $OP_CREATE, $CREATE_ALWAYS,
83 ?( flag & $OP_ALWAYS, $OPEN_ALWAYS, $OPEN_EXISTING )),
84 0,
85 0 )
86 return .fopen = .handle != $INVALID_HANDLE_VALUE
87 }
88
89 /*-----------------------------------------------------------------------------
90 * Id: file_getsize F3
91 *
92 * Summary: Get the size of the file.
93 *
94 * Return: The size of the file less 4GB.
95 *
96 -----------------------------------------------------------------------------*/
97
98 method uint file.getsize( )
99 {
100 if .fopen
101 {
102 return GetFileSize( .handle, 0 )
103 }
104 return 0
105 }
106
107 /*-----------------------------------------------------------------------------
108 * Id: file_read F2
109 *
110 * Summary: Reading a file.
111 *
112 * Params: ptr - The pointer where the file will be read.
113 size - The size of the data being read.
114 *
115 * Return: The function returns the size of the read data.
116 *
117 -----------------------------------------------------------------------------*/
118
119 method uint file.read( uint ptr, uint size )
120 {
121 uint read
122
123 if .fopen
124 {
125 /* if size > 16000000
126 {
127 uint fsize = .getsize()
128 if size > fsize : size = fsize
129 }
130 rbuf.expand( rbuf.use + size + 1 );*/
131
132 if ReadFile( .handle, ptr, size, &read, 0 )
133 {
134 return read
135 }
136 }
137 return 0
138 }
139
140 /*-----------------------------------------------------------------------------
141 * Id: file_read_1 FA
142 *
143 * Summary: Reading a file.
144 *
145 * Params: rbuf - The buffer where data will be read. Reading is carried out /
146 by adding data to the buffer. It means that read data will /
147 be added to those already existing in the buffer.
148 size - The size of the data being read.
149 *
150 * Return: The function returns the size of the read data.
151 *
152 -----------------------------------------------------------------------------*/
153
154 method uint file.read( buf rbuf, uint size )
155 {
156 if size > 16000000
157 {
158 uint fsize = .getsize()
159 if size > fsize : size = fsize
160 }
161 rbuf.expand( rbuf.use + size + 1 )
162 uint fread = this.read( rbuf.data + rbuf.use, size )
163 rbuf.use += fread
164 return fread
165 }
166
167 /*-----------------------------------------------------------------------------
168 * Id: file_setpos F2
169 *
170 * Summary: Set the current position in the file.
171 *
172 * Params: offset - Position offset.
173 mode - The type of moving the position.$$[filesetmode]
174 *
175 * Return: The function returns the current position in the file.
176 *
177 -----------------------------------------------------------------------------*/
178
179 method uint file.setpos( int offset, uint mode )
180 {
181 if .fopen
182 {
183 return SetFilePointer( .handle, offset, 0, mode )
184 }
185 return 0
186 }
187
188 /*-----------------------------------------------------------------------------
189 * Id: file_write F2
190 *
191 * Summary: Writing to a file.
192 *
193 * Params: data - The pointer to the memory which data will be written.
194 size - The size of the data being written.
195 *
196 * Return: The function returns the size of the written data.
197 *
198 -----------------------------------------------------------------------------*/
199
200 method uint file.write( uint data, uint size )
201 {
202 uint write
203 if .fopen
204 {
205 if WriteFile( .handle, data, size, &write, 0 ) && write == size
206 {
207 return write
208 }
209 }
210 return 0
211 }
212
213 /*-----------------------------------------------------------------------------
214 * Id: file_write_1 FA
215 *
216 * Summary: Writing to a file.
217 *
218 * Params: rbuf - The buffer from which data will be written.
219 *
220 * Return: The function returns the size of the written data.
221 *
222 -----------------------------------------------------------------------------*/
223
224 method uint file.write( buf rbuf )
225 {
226 return .write( rbuf.data, *rbuf )
227 }
228
229 /*-----------------------------------------------------------------------------
230 * Id: file_write_2 FA
231 *
232 * Summary: Writing to a file from the position.
233 *
234 * Params: pos - The start position for writing.
235 data - The pointer to the memory which data will be written.
236 size - The size of the data being written.
237 *
238 * Return: The function returns the size of the written data.
239 *
240 -----------------------------------------------------------------------------*/
241
242 method uint file.writepos( uint pos, uint data, uint size )
243 {
244 uint write
245
246 if .setpos( pos, $FILE_BEGIN ) != pos : return 0
247
248 return .write( data, size )
249 }
250
251 //В Linux нет
252 /*-----------------------------------------------------------------------------
253 * Id: file_gettime F2
254 *
255 * Summary: Get the time when the file was last modified.
256 *
257 * Params: ft - The variable for getting the time of a file.
258 *
259 * Return: #lng/retf#
260 *
261 -----------------------------------------------------------------------------*/
262
263 method uint file.gettime( filetime ft )
264 {
265 return GetFileTime( this.handle, 0->filetime, 0->filetime, ft )
266 }
267
268 /*-----------------------------------------------------------------------------
269 ** Id: file_settime F2
270 *
271 * Summary: Set time for a file.
272 *
273 * Params: ft - New time for the file.
274 *
275 * Return: #lng/retf#
276 *
277 -----------------------------------------------------------------------------*/
278
279 method uint file.settime( filetime ft )
280 {
281 return SetFileTime( this.handle, ft, ft, ft )
282 }
283
284 //-------------------------------------------------
285
286