123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmFile.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include <sys/stat.h>
- cmFileH_t cmFileNullHandle = { NULL };
-
- typedef struct
- {
- FILE* fp;
- cmErr_t err;
- cmChar_t* fnStr;
- } cmFile_t;
-
- cmFile_t* _cmFileHandleToPtr( cmFileH_t h )
- {
- cmFile_t* p = (cmFile_t*)h.h;
- assert(p != NULL);
- return p;
- }
-
- cmFileRC_t _cmFileError( cmFile_t* p, cmFileRC_t rc, int errNumb, const cmChar_t* msg )
- {
- if(errNumb == 0)
- rc = cmErrMsg(&p->err,rc,"%s on file '%s'",msg,p->fnStr);
- else
- rc = cmErrMsg(&p->err,rc,"%s on file '%s'\nSystem Msg:%s",msg,p->fnStr,strerror(errNumb));
-
- return rc;
- }
-
- cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlags_t flags, cmRpt_t* rpt )
- {
- char mode[] = "/0/0/0";
- cmFile_t* p = NULL;
- cmErr_t err;
- cmFileRC_t rc;
-
- if((rc = cmFileClose(hp)) != kOkFileRC )
- return rc;
-
- cmErrSetup(&err,rpt,"File");
-
- hp->h = NULL;
-
- if( cmIsFlag(flags,kReadFileFl) )
- mode[0]='r';
- else
- if( cmIsFlag(flags,kWriteFileFl) )
- mode[0]='w';
- else
- if( cmIsFlag(flags,kAppendFileFl) )
- mode[0]='a';
- else
- cmErrMsg(&err,kInvalidFlagFileRC,"File open flags must contain 'kReadFileFl','kWriteFileFl', or 'kAppendFileFl'.");
-
- if( cmIsFlag(flags,kUpdateFileFl) )
- mode[1]='+';
-
- if( fn == NULL )
- return cmErrMsg(&err,kObjAllocFailFileRC,"File object allocation failed due to empty file name.");
-
- unsigned byteCnt = sizeof(cmFile_t) + strlen(fn) + 1;
-
- if((p = (cmFile_t*)cmMemMallocZ(byteCnt)) == NULL )
- return cmErrMsg(&err,kObjAllocFailFileRC,"File object allocation failed for file '%s'.",cmStringNullGuard(fn));
-
- cmErrClone(&p->err,&err);
-
- p->fnStr = (cmChar_t*)(p+1);
- strcpy(p->fnStr,fn);
-
-
- errno = 0;
- if((p->fp = fopen(fn,mode)) == NULL )
- {
- cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed");
- cmMemFree(p);
- return rc;
- }
-
- hp->h = p;
-
- return kOkFileRC;
- }
-
- cmFileRC_t cmFileClose( cmFileH_t* hp )
- {
- if( cmFileIsValid(*hp) == false )
- return kOkFileRC;
-
- cmFile_t* p = _cmFileHandleToPtr(*hp);
-
- errno = 0;
- if( p->fp != NULL )
- if( fclose(p->fp) != 0 )
- return _cmFileError(p,kCloseFailFileRC,errno,"File close failed");
-
- cmMemFree(p);
- hp->h = NULL;
-
- return kOkFileRC;
- }
-
- bool cmFileIsValid( cmFileH_t h )
- { return h.h != NULL; }
-
- cmFileRC_t cmFileRead( cmFileH_t h, void* buf, unsigned bufByteCnt )
- {
- cmFile_t* p = _cmFileHandleToPtr(h);
-
- errno = 0;
- if( fread(buf,bufByteCnt,1,p->fp) != 1 )
- return _cmFileError(p,kReadFailFileRC,errno,"File read failed");
-
- return kOkFileRC;
- }
-
- cmFileRC_t cmFileWrite( cmFileH_t h, const void* buf, unsigned bufByteCnt )
- {
- cmFile_t* p = _cmFileHandleToPtr(h);
-
- errno = 0;
- if( fwrite(buf,bufByteCnt,1,p->fp) != 1 )
- return _cmFileError(p,kWriteFailFileRC,errno,"File write failed");
-
- return kOkFileRC;
- }
-
- cmFileRC_t cmFileSeek( cmFileH_t h, enum cmFileSeekFlags_t flags, int offsByteCnt )
- {
- cmFile_t* p = _cmFileHandleToPtr(h);
- unsigned fileflags = 0;
-
- if( cmIsFlag(flags,kBeginFileFl) )
- fileflags = SEEK_SET;
- else
- if( cmIsFlag(flags,kCurFileFl) )
- fileflags = SEEK_CUR;
- else
- if( cmIsFlag(flags,kEndFileFl) )
- fileflags = SEEK_END;
- else
- return cmErrMsg(&p->err,kInvalidFlagFileRC,"Invalid file seek flag on '%s'.",p->fnStr);
-
- errno = 0;
- if( fseek(p->fp,offsByteCnt,fileflags) != 0 )
- return _cmFileError(p,kSeekFailFileRC,errno,"File seek failed");
-
- return kOkFileRC;
- }
-
- cmFileRC_t cmFileTell( cmFileH_t h, long* offsPtr )
- {
- assert( offsPtr != NULL );
- *offsPtr = -1;
- cmFile_t* p = _cmFileHandleToPtr(h);
- errno = 0;
- if((*offsPtr = ftell(p->fp)) == -1)
- return _cmFileError(p,kTellFailFileRC,errno,"File tell failed");
- return kOkFileRC;
- }
-
-
- bool cmFileEof( cmFileH_t h )
- { return feof( _cmFileHandleToPtr(h)->fp ) != 0; }
-
-
- unsigned cmFileByteCount( cmFileH_t h )
- {
- struct stat sr;
- int f;
- cmFile_t* p = _cmFileHandleToPtr(h);
- const cmChar_t errMsg[] = "File byte count request failed.";
-
- errno = 0;
-
- if((f = fileno(p->fp)) == -1)
- {
- _cmFileError(p,kHandleInvalidFileRC,errno,errMsg);
- return 0;
- }
-
- if(fstat(f,&sr) == -1)
- {
- _cmFileError(p,kStatFailFileRC,errno,errMsg);
- return 0;
- }
-
- return sr.st_size;
- }
-
- const cmChar_t* cmFileName( cmFileH_t h )
- {
- cmFile_t* p = _cmFileHandleToPtr(h);
- return p->fnStr;
- }
-
- cmFileRC_t cmFileFnWrite( const cmChar_t* fn, cmRpt_t* rpt, const void* buf, unsigned bufByteCnt )
- {
- cmFileH_t h = cmFileNullHandle;
- cmFileRC_t rc;
-
- if((rc = cmFileOpen(&h,fn,kWriteFileFl,rpt)) != kOkFileRC )
- goto errLabel;
-
- rc = cmFileWrite(h,buf,bufByteCnt);
-
- errLabel:
- cmFileClose(&h);
-
- return rc;
- }
-
- cmChar_t* _cmFileToBuf( cmFileH_t h, unsigned nn, unsigned* bufByteCntPtr )
- {
- errno = 0;
-
- unsigned n = cmFileByteCount(h);
- cmChar_t* buf = NULL;
- cmFile_t* p = _cmFileHandleToPtr(h);
-
-
- // if the file size calculation is ok
- if( errno != 0 )
- {
- _cmFileError(p,kBufAllocFailFileRC,errno,"Invalid file buffer length.");
- goto errLabel;
- }
-
- // allocate the read target buffer
- if((buf = cmMemAlloc(cmChar_t,n+nn)) == NULL)
- {
- _cmFileError(p,kBufAllocFailFileRC,0,"Read buffer allocation failed.");
- goto errLabel;
- }
-
- // read the file
- if( cmFileRead(h,buf,n) != kOkFileRC )
- goto errLabel;
-
- // zero memory after the file data
- memset(buf+n,0,nn);
-
- if( bufByteCntPtr != NULL )
- *bufByteCntPtr = n;
-
- return buf;
-
- errLabel:
- if( bufByteCntPtr != NULL )
- *bufByteCntPtr = 0;
-
- cmMemFree(buf);
-
- return NULL;
-
- }
-
- cmChar_t* _cmFileFnToBuf( const cmChar_t* fn, cmRpt_t* rpt, unsigned nn, unsigned* bufByteCntPtr )
- {
- cmFileH_t h = cmFileNullHandle;
- cmChar_t* buf = NULL;
-
- if( cmFileOpen(&h,fn,kReadFileFl | kBinaryFileFl,rpt) != kOkFileRC )
- goto errLabel;
-
- buf = _cmFileToBuf(h,nn,bufByteCntPtr);
-
- errLabel:
- cmFileClose(&h);
-
- return buf;
- }
-
- cmChar_t* cmFileToBuf( cmFileH_t h, unsigned* bufByteCntPtr )
- { return _cmFileToBuf(h,0,bufByteCntPtr); }
-
- cmChar_t* cmFileFnToBuf( const cmChar_t* fn, cmRpt_t* rpt, unsigned* bufByteCntPtr )
- { return _cmFileFnToBuf(fn,rpt,0,bufByteCntPtr); }
-
- cmChar_t* cmFileToStr( cmFileH_t h, unsigned* bufByteCntPtr )
- { return _cmFileToBuf(h,1,bufByteCntPtr); }
-
- cmChar_t* cmFileFnToStr( const cmChar_t* fn, cmRpt_t* rpt, unsigned* bufByteCntPtr )
- { return _cmFileFnToBuf(fn,rpt,1,bufByteCntPtr); }
-
- cmFileRC_t cmFileLineCount( cmFileH_t h, unsigned* lineCntPtr )
- {
- cmFileRC_t rc = kOkFileRC;
- cmFile_t* p = _cmFileHandleToPtr(h);
- unsigned lineCnt = 0;
- long offs;
- int c;
-
-
- assert( lineCntPtr != NULL );
- *lineCntPtr = 0;
-
- if((rc = cmFileTell(h,&offs)) != kOkFileRC )
- return rc;
-
- errno = 0;
-
- while(1)
- {
- c = fgetc(p->fp);
-
- if( c == EOF )
- {
- if( errno )
- rc =_cmFileError(p,kReadFailFileRC,errno,"File read char failed");
- else
- ++lineCnt; // add one in case the last line isn't terminated with a '\n'.
-
- break;
- }
-
- // if an end-of-line was encountered
- if( c == '\n' )
- ++lineCnt;
-
- }
-
- if((rc = cmFileSeek(h,kBeginFileFl,offs)) != kOkFileRC )
- return rc;
-
- *lineCntPtr = lineCnt;
-
- return rc;
- }
-
- cmFileRC_t _cmFileGetLine( cmFile_t* p, cmChar_t* buf, unsigned* bufByteCntPtr )
- {
- // fgets() reads up to n-1 bytes into buf[]
- if( fgets(buf,*bufByteCntPtr,p->fp) == NULL )
- {
- // an read error or EOF condition occurred
- *bufByteCntPtr = 0;
-
- if( !feof(p->fp ) )
- return _cmFileError(p,kReadFailFileRC,errno,"File read line failed");
-
- return kReadFailFileRC;
- }
-
- return kOkFileRC;
- }
-
- cmFileRC_t cmFileGetLine( cmFileH_t h, cmChar_t* buf, unsigned* bufByteCntPtr )
- {
- assert( bufByteCntPtr != NULL );
- cmFile_t* p = _cmFileHandleToPtr(h);
- unsigned tn = 128;
- cmChar_t t[ tn ];
- unsigned on = *bufByteCntPtr;
- long offs;
- cmFileRC_t rc;
-
- // store the current file offset
- if((rc = cmFileTell(h,&offs)) != kOkFileRC )
- return rc;
-
- // if no buffer was given then use t[]
- if( buf == NULL || *bufByteCntPtr == 0 )
- {
- *bufByteCntPtr = tn;
- buf = t;
- }
-
- // fill the buffer from the current line
- if((rc = _cmFileGetLine(p,buf,bufByteCntPtr)) != kOkFileRC )
- return rc;
-
- // get length of the string in the buffer
- // (this is one less than the count of bytes written to the buffer)
- unsigned n = strlen(buf);
-
- // if the provided buffer was large enough to read the entire string
- if( on > n+1 )
- {
- //*bufByteCntPtr = n+1;
- return kOkFileRC;
- }
-
- //
- // the provided buffer was not large enough
- //
-
- // m tracks the length of the string
- unsigned m = n;
-
- while( n+1 == *bufByteCntPtr )
- {
- // fill the buffer from the current line
- if((rc = _cmFileGetLine(p,buf,bufByteCntPtr)) != kOkFileRC )
- return rc;
-
- n = strlen(buf);
- m += n;
- }
-
- // restore the original file offset
- if((rc = cmFileSeek(h,kBeginFileFl,offs)) != kOkFileRC )
- return rc;
-
- // add 1 for /0, 1 for /n and 1 to detect buf-too-short
- *bufByteCntPtr = m+3;
-
- return kBufTooSmallFileRC;
-
- }
-
- cmFileRC_t cmFileGetLineAuto( cmFileH_t h, cmChar_t** bufPtrPtr, unsigned* bufByteCntPtr )
- {
- cmFileRC_t rc = kOkFileRC;
- bool fl = true;
- cmChar_t* buf = *bufPtrPtr;
-
- *bufPtrPtr = NULL;
-
- while(fl)
- {
- fl = false;
-
- switch( rc = cmFileGetLine(h,buf,bufByteCntPtr) )
- {
- case kOkFileRC:
- {
- *bufPtrPtr = buf;
- }
- break;
-
- case kBufTooSmallFileRC:
- buf = cmMemResizeZ(cmChar_t,buf,*bufByteCntPtr);
- fl = true;
- break;
-
- default:
- cmMemFree(buf);
- break;
- }
- }
-
-
-
- return rc;
- }
-
- cmFileRC_t cmFileReadChar( cmFileH_t h, char* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadUChar( cmFileH_t h, unsigned char* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadShort( cmFileH_t h, short* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadUShort( cmFileH_t h, unsigned short* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadLong( cmFileH_t h, long* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadULong( cmFileH_t h, unsigned long* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadInt( cmFileH_t h, int* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadUInt( cmFileH_t h, unsigned int* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadFloat( cmFileH_t h, float* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadDouble( cmFileH_t h, double* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileReadBool( cmFileH_t h, bool* buf, unsigned cnt )
- { return cmFileRead(h,buf,sizeof(buf[0])*cnt); }
-
-
-
- cmFileRC_t cmFileWriteChar( cmFileH_t h, const char* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteUChar( cmFileH_t h, const unsigned char* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteShort( cmFileH_t h, const short* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteUShort( cmFileH_t h, const unsigned short* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteLong( cmFileH_t h, const long* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteULong( cmFileH_t h, const unsigned long* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteInt( cmFileH_t h, const int* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteUInt( cmFileH_t h, const unsigned int* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteFloat( cmFileH_t h, const float* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteDouble( cmFileH_t h, const double* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
- cmFileRC_t cmFileWriteBool( cmFileH_t h, const bool* buf, unsigned cnt )
- { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
-
-
-
-
- cmFileRC_t cmFilePrint( cmFileH_t h, const cmChar_t* text )
- {
- cmFile_t* p = _cmFileHandleToPtr(h);
-
- errno = 0;
- if( fputs(text,p->fp) < 0 )
- return _cmFileError(p,kPrintFailFileRC,errno,"File print failed");
-
- return kOkFileRC;
- }
-
-
- cmFileRC_t cmFileVPrintf( cmFileH_t h, const cmChar_t* fmt, va_list vl )
- {
- cmFile_t* p = _cmFileHandleToPtr(h);
-
- if( vfprintf(p->fp,fmt,vl) < 0 )
- return _cmFileError(p,kPrintFailFileRC,errno,"File print failed");
-
- return kOkFileRC;
- }
-
- cmFileRC_t cmFilePrintf( cmFileH_t h, const cmChar_t* fmt, ... )
- {
- va_list vl;
- va_start(vl,fmt);
- cmFileRC_t rc = cmFileVPrintf(h,fmt,vl);
- va_end(vl);
- return rc;
- }
-
|