This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gfile.h

203 lines
7.4 KiB
C
Raw Normal View History

2000-02-25 10:15:17 +00:00
// This may look like C code, but it is really -*- C++ -*-
// ------------------------------------------------------------------
// The Goldware Library
// Copyright (C) 1990-1999 Odinn Sorensen
// Copyright (C) 1999-2000 Alexander S. Aganichev
// ------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307, USA
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// File I/O class.
// ------------------------------------------------------------------
#ifndef __gfilbase_h
#define __gfilbase_h
// ------------------------------------------------------------------
#include <gfilutil.h>
// ------------------------------------------------------------------
// An invalid path/filename
#define GFILE_INVALID "::INVALID::"
// ------------------------------------------------------------------
// Stream/Unix-style file I/O class
2006-04-24 16:38:44 +00:00
class gfile
{
2000-02-25 10:15:17 +00:00
public:
// ----------------------------------------------------------------
// Internal variables
int fh; // File handle for Unix I/O
FILE* fp; // File pointer for ANSI stream I/O
// --------------------------------------------------------------
// State variables
int status; // Contains last errno value
// --------------------------------------------------------------
// Handy utility functions
2006-04-24 16:38:44 +00:00
int okay() { return (0 == status); }
2005-10-25 06:11:09 +00:00
bool isopen(); // true if the file is open
2006-04-24 16:38:44 +00:00
operator bool() { return isopen(); }
2000-02-25 10:15:17 +00:00
// --------------------------------------------------------------
// Constructors and destructor
gfile(); // Bare constructor
2006-04-26 17:06:23 +00:00
// gfile(int __fh); // Construct from Unix file handle
// gfile(FILE* __fp); // Construct from ANSI stream pointer
2000-02-25 10:15:17 +00:00
gfile(const char* __path, int __access, int __shflag=SH_DENYNO, int __mode=S_IREAD|S_IWRITE);
gfile(const char* __path, const char* __mode, int __shflag=SH_DENYNO);
~gfile(); // Destructor (closes file)
// --------------------------------------------------------------
// Set file handle or stream pointer
int setfh(int __fh);
FILE* setfp(FILE* __fp);
// --------------------------------------------------------------
// UNIX-style raw I/O
2006-04-24 16:38:44 +00:00
int Open (const char* __path, int __access, int __shflag=SH_DENYNO, int __mode=S_IREAD|S_IWRITE);
int Open (const char* __path, int __access, const char* __fmode, int __shflag=SH_DENYNO, int __mode=S_IREAD|S_IWRITE);
int Close ();
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int Read (void* __ptr, size_t __len);
int Write (void* __ptr, size_t __len);
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
long Tell ();
long Lseek (long __offset, int __direction);
long Lseek (long __record, long __recordsize, int __direction) { return Lseek(__record*__recordsize, __direction); }
long LseekSet (long __record, long __recordsize = 1) { return Lseek(__record*__recordsize, SEEK_SET); }
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
long FileLength ();
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int ChSize (long __size);
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int Lock (long __offset, long __len);
int Unlock (long __offset, long __len);
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int GetFTime (dword* __ftime);
2000-02-25 10:15:17 +00:00
// --------------------------------------------------------------
// ANSI-style streaming buffered I/O
2006-04-24 16:38:44 +00:00
FILE* Fopen (const char* __path, const char* __mode, int __shflag=SH_DENYNO);
FILE* Fopen (const std::string& __path, const char* __mode, int __shflag=SH_DENYNO) { return Fopen(__path.c_str(), __mode, __shflag); }
FILE* Fdopen (const char* __mode);
int Fclose ();
2000-02-25 10:15:17 +00:00
2006-04-26 17:06:23 +00:00
FILE* Popen (const char* __path, const char* __mode);
FILE* Popen (const std::string& __path, const char* __mode) { return Popen(__path.c_str(), __mode); }
int Pclose ();
2006-04-24 16:38:44 +00:00
size_t Fread (void* __ptr, size_t __size, size_t __items=1);
size_t Fwrite (const void* __ptr, size_t __size, size_t __items=1);
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int Fgetc ();
int Fputc (int __ch);
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
char* Fgets (char* __str, size_t __len);
int Fputs (const char* __str);
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int Printf (const char* __format, ...) __attribute__ ((format (printf, 2, 3)));
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int Fflush ();
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
long Ftell ();
int Fseek (long __offset, int __direction);
int Fseek (long __record, long __recordsize, int __direction) { return Fseek(__record*__recordsize, __direction); }
int FseekSet(long __record, long __recordsize = 1) { return Fseek(__record*__recordsize, SEEK_SET); }
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
void Rewind () { rewind(fp); }
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int SetvBuf (char* __buf=NULL, int __type=_IOFBF, size_t __size=8192);
int SetvBuf (size_t __size) { return SetvBuf(NULL, _IOFBF, __size); }
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
int feof_ () { return feof(fp); }
int ferror_ () { return ferror(fp); }
2000-02-25 10:15:17 +00:00
// ----------------------------------------------------------------
#ifdef __GOLDWARE_HAS_BOOL
2006-04-24 16:38:44 +00:00
gfile& operator>> (bool& i) { Fread(&i, sizeof(bool)); return *this; }
2000-02-25 10:15:17 +00:00
#endif
2006-04-24 16:38:44 +00:00
gfile& operator>> (uint8_t& i) { Fread(&i, sizeof(uint8_t)); return *this; }
gfile& operator>> (uint16_t& i) { Fread(&i, sizeof(uint16_t)); return *this; }
gfile& operator>> (uint32_t& i) { Fread(&i, sizeof(uint32_t)); return *this; }
#if !defined(__CYGWIN__)
2006-04-24 16:38:44 +00:00
gfile& operator>> (unsigned long& i) { Fread(&i, sizeof(unsigned long)); return *this; }
#endif
gfile& operator>> (char& i) { return operator>>((uint8_t&)i); }
#if !defined(__SUNOS__)
gfile& operator>> (int8_t& i) { return operator>>((uint8_t&)i); }
#endif
gfile& operator>> (int16_t& i) { return operator>>((uint16_t&)i); }
gfile& operator>> (int32_t& i) { return operator>>((uint32_t&)i); }
#if !defined(__CYGWIN__)
gfile& operator>> (long& i) { return operator>>((unsigned long&)i); }
#endif
2000-02-25 10:15:17 +00:00
#ifdef __GOLDWARE_HAS_BOOL
2006-04-24 16:38:44 +00:00
gfile& operator<< (bool o) { Fwrite(&o, sizeof(o)); return *this; }
2000-02-25 10:15:17 +00:00
#endif
2006-04-24 16:38:44 +00:00
gfile& operator<< (uint8_t o) { Fwrite(&o, sizeof(o)); return *this; }
gfile& operator<< (uint16_t o) { Fwrite(&o, sizeof(o)); return *this; }
gfile& operator<< (uint32_t o) { Fwrite(&o, sizeof(o)); return *this; }
#if !defined(__CYGWIN__)
2006-04-24 16:38:44 +00:00
gfile& operator<< (unsigned long o) { Fwrite(&o, sizeof(o)); return *this; }
#endif
gfile& operator<< (char o) { return operator<<((uint8_t )o); }
#if !defined(__SUNOS__)
gfile& operator<< (int8_t o) { return operator<<((uint8_t )o); }
#endif
gfile& operator<< (int16_t o) { return operator<<((uint16_t)o); }
gfile& operator<< (int32_t o) { return operator<<((uint32_t)o); }
#if !defined(__CYGWIN__)
gfile& operator<< (long o) { return operator<<((unsigned long)o); }
#endif
2000-02-25 10:15:17 +00:00
};
// ------------------------------------------------------------------
#endif
// ------------------------------------------------------------------