cmFile.h/c: Added ability to use stdin,stdout,stderr.

This commit is contained in:
kevin 2013-02-25 11:39:42 -08:00
parent 8f09079fe0
commit c9a0a1b2dc
2 changed files with 38 additions and 7 deletions

View File

@ -60,6 +60,27 @@ cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag
if( cmIsFlag(flags,kUpdateFileFl) ) if( cmIsFlag(flags,kUpdateFileFl) )
mode[1]='+'; mode[1]='+';
// handle requests to use stdin,stdout,stderr
FILE* sfp = NULL;
if( cmIsFlag(flags,kStdoutFileFl) )
{
sfp = stdout;
fn = "stdout";
}
else
if( cmIsFlag(flags,kStderrFileFl) )
{
sfp = stderr;
fn = "stderr";
}
else
if( cmIsFlag(flags,kStdinFileFl) )
{
sfp = stdin;
fn = "stdin";
}
if( fn == NULL ) if( fn == NULL )
return cmErrMsg(&err,kObjAllocFailFileRC,"File object allocation failed due to empty file name."); return cmErrMsg(&err,kObjAllocFailFileRC,"File object allocation failed due to empty file name.");
@ -73,13 +94,17 @@ cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag
p->fnStr = (cmChar_t*)(p+1); p->fnStr = (cmChar_t*)(p+1);
strcpy(p->fnStr,fn); strcpy(p->fnStr,fn);
if( sfp != NULL )
errno = 0; p->fp = sfp;
if((p->fp = fopen(fn,mode)) == NULL ) else
{ {
cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed"); errno = 0;
cmMemFree(p); if((p->fp = fopen(fn,mode)) == NULL )
return rc; {
cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed");
cmMemFree(p);
return rc;
}
} }
hp->h = p; hp->h = p;

View File

@ -42,7 +42,10 @@ extern "C" {
kWriteFileFl = 0x02, //< Create an empty file for writing kWriteFileFl = 0x02, //< Create an empty file for writing
kAppendFileFl = 0x04, //< Open a file for writing at the end of the file. kAppendFileFl = 0x04, //< Open a file for writing at the end of the file.
kUpdateFileFl = 0x08, //< Open a file for reading and writing. kUpdateFileFl = 0x08, //< Open a file for reading and writing.
kBinaryFileFl = 0x10 //< Open a file for binary (not text) input/output. kBinaryFileFl = 0x10, //< Open a file for binary (not text) input/output.
kStdoutFileFl = 0x20, //< Ignore fn use 'stdout'
kStderrFileFl = 0x40, //< Ignore fn use 'stderr'
kStdinFileFl = 0x80, //< Ignore fn use 'stdin'
}; };
// Open or create a file. // Open or create a file.
@ -51,6 +54,9 @@ extern "C" {
// be set to cmFileNullHandle prior to calling this function. If *hp is a valid handle // be set to cmFileNullHandle prior to calling this function. If *hp is a valid handle
// then it is automatically finalized by an internal call to cmFileClose() prior to // then it is automatically finalized by an internal call to cmFileClose() prior to
// being re-iniitalized. // being re-iniitalized.
//
// If kStdoutFileFl, kStderrFileFl or kStdinFileFl are set then
// file name argument 'fn' is ignored.
cmFileRC_t cmFileOpen( cmFileRC_t cmFileOpen(
cmFileH_t* hp, // Pointer to a client supplied cmFileHandle_t to recieve the handle for the new object. cmFileH_t* hp, // Pointer to a client supplied cmFileHandle_t to recieve the handle for the new object.
const cmChar_t* fn, // The name of the file to open or create. const cmChar_t* fn, // The name of the file to open or create.