1 // This may look like C code, but it is really -*- C++ -*-
2
3 // ------------------------------------------------------------------
4 // The Goldware Library
5 // Copyright (C) 1990-1999 Odinn Sorensen
6 // Copyright (C) 1999-2000 Alexander S. Aganichev
7 // ------------------------------------------------------------------
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this program; if not, write to the Free
20 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 // MA 02111-1307, USA
22 // ------------------------------------------------------------------
23 // $Id: gfile.cpp,v 1.10 2011/02/22 11:37:07 stas_degteff Exp $
24 // ------------------------------------------------------------------
25 // File I/O class.
26 // ------------------------------------------------------------------
27
28 #include <cerrno>
29 #include <cstdarg>
30 #include <gfile.h>
31
32 #if defined(_MSC_VER) /*&& (_MSC_VER >= 1400)*/
33
34 #define g_popen(comm, mode) _tpopen(comm, mode)
35 #define g_pclose(fp) _pclose(fp)
36
37 #define g_sopen(fn, of, sh, pm) _tsopen(fn, of, sh, pm)
38 #define g_close(fh) _close(fh)
39 #define g_read(fh, buf, cnt) _read(fh, buf, cnt)
40 #define g_write(fh, buf, cnt) _write(fh, buf, cnt)
41 #define g_tell(fh) _tell(fh)
42 #define g_lseek(fh, off, org) _lseek(fh, off, org)
43 #define g_filelength(fh) _filelength(fh)
44 #define g_chsize(fh, size) _chsize(fh, size)
45
46 #define g_fsopen(fn, of, sh) _tfsopen(fn, of, sh)
47 #define g_fdopen(fp, of) _tfdopen(fp, of)
48 #define g_fileno(fp) _fileno(fp)
49
50 #else
51
52 #define g_popen(comm, mode) popen(comm, mode)
53 #define g_pclose(fp) pclose(fp)
54
55 #define g_sopen(fn, of, sh, pm) sopen(fn, of, sh, pm)
56 #define g_close(fh) close(fh)
57 #define g_read(fh, buf, cnt) read(fh, buf, cnt)
58 #define g_write(fh, buf, cnt) write(fh, buf, cnt)
59 #define g_tell(fh) tell(fh)
60 #define g_lseek(fh, off, org) lseek(fh, off, org)
61 #define g_filelength(fh) filelength(fh)
62 #define g_chsize(fh, size) chsize(fh, size)
63
64 #define g_fsopen(fn, of, sh) fsopen(fn, of, sh)
65 #define g_fdopen(fp, of) fdopen(fp, of)
66 #define g_fileno(fp) fileno(fp)
67
68 #endif
69
70 #define g_lock(fh, off, len) lock(fh, off, len)
71 #define g_unlock(fh, off, len) unlock(fh, off, len)
72
73 #define g_fclose(fp) fclose(fp)
74 #define g_fread(buf, rsz, cnt, fp) fread(buf, rsz, cnt, fp)
75 #define g_fwrite(buf, rsz, cnt, fp) fwrite(buf, rsz, cnt, fp)
76 #define g_fgetc(fp) fgetc(fp)
77 #define g_fputc(c, fp) fputc(c, fp)
78 #define g_fgets(str, cnt, fp) fgets(str, cnt, fp)
79 #define g_fputs(str, fp) fputs(str, fp)
80 #define g_fflush(fp) fflush(fp)
81 #define g_ftell(fp) ftell(fp)
82
83
84 // ------------------------------------------------------------------
85
gfile()86 gfile::gfile()
87 {
88 fh = -1;
89 fp = NULL;
90 status = EBADF;
91 }
92
93
94 // ------------------------------------------------------------------
95 /*
96 gfile::gfile(int __fh)
97 {
98 fh = __fh;
99 fp = NULL;
100 status = 0;
101 }
102 */
103
104 // ------------------------------------------------------------------
105 /*
106 gfile::gfile(FILE* __fp)
107 {
108 fh = -1;
109 fp = __fp;
110 status = 0;
111 }
112 */
113
114 // ------------------------------------------------------------------
115
gfile(const char * __path,int __access,int __shflag,int __mode)116 gfile::gfile(const char* __path, int __access, int __shflag, int __mode)
117 {
118 fh = -1;
119 fp = NULL;
120 if( !( __path && *__path ) )
121 {
122 status = EINVAL;
123 return;
124 }
125 Open(__path, __access, __shflag, __mode);
126 }
127
128 // ------------------------------------------------------------------
129
gfile(const char * __path,const char * __mode,int __shflag)130 gfile::gfile(const char* __path, const char* __mode, int __shflag)
131 {
132 fh = -1;
133 fp = NULL;
134 if( !( __path && *__path ) )
135 {
136 status = EINVAL;
137 return;
138 }
139 Fopen(__path, __mode, __shflag);
140 }
141
142 // ------------------------------------------------------------------
143
~gfile()144 gfile::~gfile()
145 {
146 if (fp != NULL) Fclose();
147 if (fh != -1) Close();
148 }
149
150 // ------------------------------------------------------------------
151
isopen()152 bool gfile::isopen()
153 {
154 if ((fh != -1) or (fp != NULL)) return true;
155 return false;
156 }
157
158 // ------------------------------------------------------------------
159
setfh(int __fh)160 int gfile::setfh(int __fh)
161 {
162 fh = __fh;
163 status = 0;
164 return fh;
165 }
166
167 // ------------------------------------------------------------------
168
setfp(FILE * __fp)169 FILE* gfile::setfp(FILE* __fp)
170 {
171 fp = __fp;
172 status = 0;
173 return fp;
174 }
175
176 // ------------------------------------------------------------------
177
Open(const char * __path,int __access,int __shflag,int __mode)178 int gfile::Open(const char* __path, int __access, int __shflag, int __mode)
179 {
180 if( !( __path && *__path ) )
181 {
182 status = EINVAL;
183 return -1;
184 }
185
186 #if defined(_tsopen_s)
187 status = _tsopen_s(&fh, __path, __access, __shflag, __mode);
188 return fh;
189 #else
190 fh = g_sopen(__path, __access, __shflag, __mode);
191 status = (fh == -1) ? errno : 0;
192 return fh;
193 #endif
194 }
195
196
197 // ------------------------------------------------------------------
198
Open(const char * __path,int __access,const char * __fmode,int __shflag,int __mode)199 int gfile::Open(const char* __path, int __access, const char* __fmode, int __shflag, int __mode)
200 {
201 if( !(__path && *__path && __fmode && *__fmode) )
202 {
203 status = EINVAL;
204 return -1;
205 }
206 Open(__path, __access, __shflag, __mode);
207 Fdopen(__fmode);
208 return fh;
209 }
210
211 // ------------------------------------------------------------------
212
Close()213 int gfile::Close()
214 {
215 if (fp) return Fclose();
216 if (fh==-1)
217 {
218 status = 0;
219 return 0;
220 }
221 int _ret = g_close(fh);
222 status = _ret ? errno : 0;
223 fh = -1;
224 return _ret;
225 }
226
227 // ------------------------------------------------------------------
228
Read(void * __ptr,size_t __len)229 int gfile::Read(void* __ptr, size_t __len)
230 {
231 if( !__ptr )
232 {
233 status = EINVAL;
234 return -1;
235 }
236 int _ret = g_read(fh, __ptr, unsigned(__len));
237 status = (_ret == -1) ? errno : 0;
238 return _ret;
239 }
240
241 // ------------------------------------------------------------------
242
Write(const void * __ptr,size_t __len)243 int gfile::Write(const void* __ptr, size_t __len)
244 {
245 if( !__ptr )
246 {
247 status = EINVAL;
248 return -1;
249 }
250 int _ret = g_write(fh, __ptr, unsigned(__len));
251 status = (_ret == -1) ? errno : 0;
252 return _ret;
253 }
254
255 // ------------------------------------------------------------------
256
Tell()257 long gfile::Tell()
258 {
259 long _ret = g_tell(fh);
260 status = (_ret == -1) ? errno : 0;
261 return _ret;
262 }
263
264 // ------------------------------------------------------------------
265
Lseek(long __offset,int __direction)266 long gfile::Lseek(long __offset, int __direction)
267 {
268 long _ret = g_lseek(fh, __offset, __direction);
269 status = (_ret == -1) ? errno : 0;
270 return _ret;
271 }
272
273 // ------------------------------------------------------------------
274
FileLength()275 long gfile::FileLength()
276 {
277 long _ret = g_filelength(fh);
278 status = (_ret == -1) ? errno : 0;
279 return _ret;
280 }
281
282 // ------------------------------------------------------------------
283
ChSize(long __size)284 int gfile::ChSize(long __size)
285 {
286 int _ret = g_chsize(fh, __size);
287 status = _ret ? errno : 0;
288 return _ret;
289 }
290
291 // ------------------------------------------------------------------
292
Lock(long __offset,long __len)293 int gfile::Lock(long __offset, long __len)
294 {
295 int _ret = g_lock(fh, __offset, __len);
296 status = _ret ? errno : 0;
297 return _ret;
298 }
299
300 // ------------------------------------------------------------------
301
Unlock(long __offset,long __len)302 int gfile::Unlock(long __offset, long __len)
303 {
304 int _ret = g_unlock(fh, __offset, __len);
305 status = _ret ? errno : 0;
306 return _ret;
307 }
308
309 // ------------------------------------------------------------------
310
GetFTime(time32_t * __ftime)311 int gfile::GetFTime(time32_t *__ftime)
312 {
313 struct stat s;
314 if (fp) Fflush();
315 int rv = fstat(fh, &s);
316 status = (rv) ? errno : 0;
317 if (rv == 0) *__ftime = gfixstattime(time32_t(s.st_mtime));
318 else __ftime = 0;
319 return rv;
320 }
321
322
323 // ------------------------------------------------------------------
324
Fopen(const char * __path,const char * __mode,int __shflag)325 FILE* gfile::Fopen(const char* __path, const char* __mode, int __shflag)
326 {
327 if( !(__path && *__path && __mode && *__mode) )
328 {
329 status = EINVAL;
330 return NULL;
331 }
332 fp = g_fsopen(__path, __mode, __shflag);
333 status = (fp == NULL) ? errno : 0;
334 if (fp) fh = g_fileno(fp);
335 return fp;
336 }
337
338
339 // ------------------------------------------------------------------
340
Popen(const char * __path,const char * __mode)341 FILE* gfile::Popen(const char* __path, const char* __mode)
342 {
343 if( !(__path && *__path && __mode && *__mode) )
344 {
345 status = EINVAL;
346 return NULL;
347 }
348 fp = g_popen(__path, __mode);
349 status = (fp == NULL) ? errno : 0;
350 if (fp) fh = g_fileno(fp);
351 return fp;
352 }
353
354
355 // ------------------------------------------------------------------
356
Fdopen(const char * __mode)357 FILE* gfile::Fdopen(const char* __mode)
358 {
359 if( !(__mode) )
360 {
361 status = EINVAL;
362 return NULL;
363 }
364 fp = g_fdopen(fh, __mode);
365 status = fp ? 0 : errno;
366 if (fp) fh = g_fileno(fp);
367 return fp;
368 }
369
370
371 // ------------------------------------------------------------------
372
Fclose()373 int gfile::Fclose()
374 {
375 int _ret = 0;
376 if (fp) _ret = g_fclose(fp);
377 status = _ret ? errno : 0;
378 fp = NULL; fh = -1;
379 return _ret;
380 }
381
382
383 // ------------------------------------------------------------------
384
Pclose()385 int gfile::Pclose()
386 {
387 int _ret = 0;
388 if (fp) _ret = g_pclose(fp);
389 status = _ret ? errno : 0;
390 fp = NULL; fh = -1;
391 return _ret;
392 }
393
394 // ------------------------------------------------------------------
395
Fread(void * __ptr,size_t __size,size_t __items)396 size_t gfile::Fread(void* __ptr, size_t __size, size_t __items)
397 {
398 if( !(__ptr) )
399 {
400 status = EINVAL;
401 return 0;
402 }
403 size_t _ret = g_fread(__ptr, __size, __items, fp);
404 status = ferror_() ? errno : 0;
405 return _ret;
406 }
407
408
409 // ------------------------------------------------------------------
410
Fwrite(const void * __ptr,size_t __size,size_t __items)411 size_t gfile::Fwrite(const void* __ptr, size_t __size, size_t __items)
412 {
413 if( !(__ptr) )
414 {
415 status = EINVAL;
416 return 0;
417 }
418 size_t _ret = g_fwrite(__ptr, __size, __items, fp);
419 status = (_ret < __items) ? errno : 0;
420 return _ret;
421 }
422
423
424 // ------------------------------------------------------------------
425
Fgetc()426 int gfile::Fgetc()
427 {
428 int _ret = g_fgetc(fp);
429 status = ferror_() ? errno : 0;
430 return _ret;
431 }
432
433
434 // ------------------------------------------------------------------
435
Fputc(int __ch)436 int gfile::Fputc(int __ch)
437 {
438 int _ret = g_fputc(__ch, fp);
439 status = ferror_() ? errno : 0;
440 return _ret;
441 }
442
443 // ------------------------------------------------------------------
444
Fgets(char * __str,size_t __len)445 char* gfile::Fgets(char* __str, size_t __len)
446 {
447 if( !(__str) )
448 {
449 status = EINVAL;
450 return NULL;
451 }
452 char* _ret = g_fgets(__str, int(__len), fp);
453 status = (_ret == NULL) ? errno : 0;
454 return _ret;
455 }
456
457 // ------------------------------------------------------------------
458
Fputs(const char * __str)459 int gfile::Fputs(const char* __str)
460 {
461 if( !(__str) )
462 {
463 status = EINVAL;
464 return -1;
465 }
466 int _ret = g_fputs(__str, fp);
467 status = (_ret == EOF) ? errno : 0;
468 return _ret;
469 }
470
471
472 // ------------------------------------------------------------------
473
Printf(const char * __format,...)474 int gfile::Printf(const char* __format, ...)
475 {
476 if( !(__format && *__format) )
477 {
478 status = EINVAL;
479 return -1;
480 }
481 va_list _argptr;
482 va_start(_argptr, __format);
483 int _outcount = vfprintf(fp, __format, _argptr);
484 va_end(_argptr);
485 return _outcount;
486 }
487
488 // ------------------------------------------------------------------
489
Fflush()490 int gfile::Fflush()
491 {
492 int _ret = g_fflush(fp);
493 status = _ret ? errno : 0;
494 return _ret;
495 }
496
497 // ------------------------------------------------------------------
498
Ftell()499 long gfile::Ftell()
500 {
501 long _ret = g_ftell(fp);
502 status = (_ret == -1) ? errno : 0;
503 return _ret;
504 }
505
506 // ------------------------------------------------------------------
507
Fseek(long __offset,int __direction)508 int gfile::Fseek(long __offset, int __direction)
509 {
510 int _ret = ::fseek(fp, __offset, __direction);
511 status = _ret ? errno : 0;
512 return _ret;
513 }
514
515 // ------------------------------------------------------------------
516
SetvBuf(char * __buf,int __type,size_t __size)517 int gfile::SetvBuf(char* __buf, int __type, size_t __size)
518 {
519 if( !(__buf && __size) )
520 {
521 status = EINVAL;
522 return -1;
523 }
524 int _ret = ::setvbuf(fp, __buf, __type, __size);
525 status = _ret ? errno : 0;
526 return _ret;
527 }
528
529
530 // ------------------------------------------------------------------
531