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