#ifndef __CFile_h__
#define __CFile_h__ 1
#include <DirectoryServiceCore/DSCThread.h> // for CThread::Yield()
#include <DirectoryServiceCore/DSMutexSemaphore.h>
#ifdef __LP64__
#error "Cannot compile CFile with 64 bit - ppc64"
#endif
typedef long long sInt64;
#include <unistd.h> // for sync()
#include <limits.h> // for PATH_MAX
#include <stdio.h> // for PATH_MAX, FILE, fopen(), etc.
#include <fstream> // for classes fstream and ios
#include <sys/stat.h> // for fstat(), stat() and structs
using namespace std;
static FILE * const kBadFileRef = NULL;
static const SInt32 kMaxFiles = 8;
typedef char CFileSpec[ PATH_MAX ];
typedef char *CFileSpecPtr;
#define kRollLogMessageStartStr "-- Start: Server rolled log on: %s --\n"
#define kRollLogMessageEndStr "-- End: Server rolled log on: %s --\n"
#define kMemFullErrStr "*** Error: Could not roll file, memory full ***\n"
#define kRenameErrorStr "*** Error: Received error %d during rename ***\n"
class CFile
{
public:
CFile ( void ) throw();
CFile ( const char *inFileSpec, const Boolean inCreate = false, const Boolean inRoll = false ) throw( SInt16 );
virtual ~CFile ( void );
virtual void open ( const char *inFileSpec, const Boolean inCreate = false ) throw( SInt16 );
virtual sInt64 freespace ( void ) const throw( SInt16 );
virtual void syncdisk ( void ) const throw();
virtual int is_open ( void ) const throw();
virtual CFile& seteof ( sInt64 lEOF ) throw( SInt16 );
virtual CFile& flush ( void ) throw( SInt16 );
virtual void close ( void ) throw( SInt16 );
virtual ssize_t ReadBlock ( void *s, streamsize n ) throw( SInt16 );
virtual CFile& Read ( void *s, streamsize n ) throw( SInt16 );
CFile& read ( char *s, streamsize n ) throw( SInt16 );
CFile& read ( unsigned char *s, streamsize n ) throw( SInt16 );
CFile& read ( signed char *s, streamsize n ) throw( SInt16 );
virtual CFile& write ( const void *s, streamsize n ) throw( SInt16 );
CFile& write ( const char *s, streamsize n ) throw( SInt16 );
CFile& write ( const unsigned char *s, streamsize n ) throw( SInt16 );
CFile& write ( const signed char *s, streamsize n ) throw( SInt16 );
virtual CFile& seekg ( sInt64 lOffset, ios::seekdir inMark = ios::beg ) throw( SInt16 );
virtual sInt64 tellg ( void ) throw( SInt16 );
virtual CFile& seekp ( sInt64 lOffset, ios::seekdir inMark = ios::beg ) throw( SInt16 );
virtual sInt64 tellp ( void ) throw( SInt16 );
virtual sInt64 FileSize ( void ) throw( SInt16 );
virtual void ModDate ( struct timespec *outModTime );
protected:
DSMutexSemaphore fLock;
char *fFilePath;
FILE *fFileRef;
time_t fOpenTime;
time_t fLastChecked;
bool fRollLog;
sInt64 fReadPos;
sInt64 fWritePos;
bool fReadPosOK;
bool fWritePosOK;
struct stat fStatStruct;
bool fWroteData;
};
inline CFile& CFile::flush ( void ) throw( SInt16 )
#if USE_UNIXIO
{ return *this; } #else
{ ::fflush ( fFileRef ); return *this; }
#endif
inline CFile& CFile::read ( char *s, streamsize n ) throw( SInt16 ) { return this->Read( (void *)s, n ); }
inline CFile& CFile::read ( unsigned char *s, streamsize n ) throw( SInt16 ) { return this->Read( (void *)s, n ); }
inline CFile& CFile::read ( signed char *s, streamsize n ) throw( SInt16 ) { return this->Read( (void *)s, n ); }
inline CFile& CFile::write ( const char *s, streamsize n ) throw( SInt16 ) { return this->write( (void *)s, n ); }
inline CFile& CFile::write ( const unsigned char *s, streamsize n ) throw( SInt16 ) { return this->write( (void *)s, n ); }
inline CFile& CFile::write ( const signed char *s, streamsize n ) throw( SInt16 ) { return this->write( (void *)s, n ); }
inline sInt64 CFile::tellg ( void ) throw( SInt16 ) { return( fReadPos ); }
inline sInt64 CFile::tellp ( void ) throw( SInt16 ) { return( fWritePos ); }
#endif // __CFile_h__