57 static XFILE xstdin = {&std::string(
"stdin")[0], &std::string(
"r")[0], 0};
58 static XFILE xstdout = {&std::string(
"stdout")[0], &std::string(
"w")[0], 0};
59 static XFILE xstderr = {&std::string(
"stderr")[0], &std::string(
"w")[0], 0};
64 #define XIO_WARN(f) xprintf(Warn, "File pointer '%p' not in xfiles_map. Opened with regular fopen() or already closed?\n", (f) )
65 #define XIO_PRINT_INFO(f) printf( "XIO: In function '%s', %s\n", __func__, xio_getfulldescription( f ) )
66 #define XIO_DEBUG(f) do { if ( Xio::get_instance()->get_verbosity() > 0 ) XIO_PRINT_INFO(f); } while (0)
94 xprintf(
Warn,
"The XIO library already initialized.\n");
191 const char prn_format_long[] =
"FILE ptr %p: x->name '%s', x->mode '%s', x->line '%d'";
192 const char prn_format_short[] =
"FILE ptr %p: unknown FILE pointer";
193 static char * rs = NULL;
194 static int maxlen = 0;
201 rs = (
char *)
xmalloc( maxlen );
211 rs = (
char *)
xrealloc( rs, maxlen );
217 len = snprintf( rs, maxlen, prn_format_short, f );
221 rs = (
char *)
xrealloc( rs, maxlen );
222 snprintf( rs, maxlen, prn_format_short, f );
246 FILE *
xfopen(
const std::string& fname,
const char *mode )
248 const char *fnamec = fname.c_str();
249 return xfopen(fnamec,mode);
258 FILE *
xfopen(
const char *fname,
const char *mode )
263 ASSERT(!( (fname == NULL) || (mode == NULL) ),
"NULL pointer as argument of function xfopen()\n");
265 rc = fopen( fname, mode );
266 INPUT_CHECK( rc != NULL ,
"Cannot open file '%s' with permissions %s\n", fname, mode );
273 strcpy(xf->
mode, mode);
291 ASSERT(!(f == NULL),
"NULL as input argument\n");
314 ASSERT(!( stream == NULL ),
"NULL pointer as argument of function xfclose()\n");
318 rc = fclose( stream );
348 FILE *
xfreopen(
const char * filename,
const char * mode, FILE * stream )
353 ASSERT(!( (mode == NULL) || (stream == NULL)),
"Wrong arguments\n");
355 rc = freopen( filename, mode, stream );
357 INPUT_CHECK( rc != NULL ,
"Cannot reopen file %s with permissions %s\n", filename, mode );
364 if ( strlen(filename) > strlen(xf->
filename) )
369 if ( strlen(mode) > strlen(xf->
mode) )
371 strcpy(xf->
mode, mode);
382 strcpy(xf->
mode, mode);
400 ASSERT(!( (out == NULL) || (fmt == NULL) ),
"NULL pointer as argument of function xfprintf()\n");
401 va_start( argptr, fmt );
402 rc = vfprintf( out, fmt, argptr );
417 ASSERT(!( (in == NULL) || (fmt == NULL) ),
"NULL pointer as argument of function xfscanf()\n");
418 va_start( argptr , fmt );
419 rc = vfscanf( in, fmt, argptr );
435 ASSERT(!(f == NULL),
"NULL file\n");
453 ASSERT(!(f == NULL),
"NULL file\n");
487 ASSERT(!(f == NULL),
"NULL file\n");
495 if ( ( rc ==
'\n' ) && ( xf->
lineno > 0 ) )
515 int xrename (
const char * oldname,
const char * newname )
519 ASSERT(!(( oldname == NULL) || (newname == NULL)),
"NULL file name\n");
521 rc = rename( oldname, newname );
523 INPUT_CHECK( rc != 0,
"Failed when renaming '%s' to '%s'\n", oldname, newname );
535 size_t xfread(
void * ptr,
size_t size,
size_t count, FILE * stream )
539 ASSERT(!( (ptr == NULL) || ( stream == NULL) ),
"Incorrect arguments\n");
541 rc = fread( ptr, size, count, stream );
558 size_t xfwrite(
const void * ptr,
size_t size,
size_t count, FILE * stream )
562 ASSERT(!( (ptr == NULL) || (stream == NULL) ),
"Incorrect arguments\n");
564 rc = fwrite( ptr, size, count, stream );
585 ASSERT(!( (s == NULL) || (in == NULL) ),
"Incorrect arguments of function xfgets()\n");
586 rc = fgets( s, n, in );
623 ASSERT(!(f == NULL),
"NULL file argument in xrewind()\n");
int verbosity_
internal XIO debug: print info at each XIO function
void set_verbosity(int verb)
Enable/Disable XIO debug output for EACH XIO function call.
int xfflush(FILE *f)
Flush file stream.
void xrewind(FILE *f)
Rewind file, handle line count.
static Xio * get_instance()
return instance
int xrename(const char *oldname, const char *newname)
Changes the name of the file or directory specified by oldname to newname.
int xfclose(FILE *stream)
FCLOSE WITH ERROR HANDLING.
size_t xfwrite(const void *ptr, size_t size, size_t count, FILE *stream)
Write block of data to stream, handle errors.
char * xio_getfname(FILE *f)
Get file name from pointer to FILE structure.
int xgetc(FILE *f)
getc() with error handling and line count
int xfprintf(FILE *out, const char *fmt,...)
FPRINTF WITH ERROR HANDLING.
char * xfgets(char *s, int n, FILE *in)
FGETS WITH ERROR HANDLING and line count.
size_t xfread(void *ptr, size_t size, size_t count, FILE *stream)
Read block of data from stream, handle errors.
int xio_getlinesread(FILE *f)
Get number of lines that were completely read from file since fopen() or rewind() ...
XFILEMAP & get_xfile_map()
Get XIO mapping instance.
FILE * xfreopen(const char *filename, const char *mode, FILE *stream)
Reopen stream with different file or mode.
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
void * xrealloc(void *ptr, size_t size)
Reallocation of memory block with checking.
char * xio_getfmode(FILE *f)
Get file mode from file stream.
XFILE structure holds additional info to generic FILE.
char * xio_getfulldescription(FILE *f)
Get pointer to string with full file description.
static XFILE xstdin
basic definitions
void * xmalloc(size_t size)
Memory allocation with checking.
#define INPUT_CHECK(i,...)
Debugging macros.
int lineno
last read line (only for text files)
int xfgetc(FILE *f)
fgetc() with error handling and line count
char * filename
file name in the time of opening
int xfeof(FILE *f)
Check END OF FILE.
int get_verbosity()
Get current XIO debug verbosity level.
int xungetc(int c, FILE *f)
ungetc() with error handling and line count
int xfscanf(FILE *in, const char *fmt,...)
FSCANF WITH ERROR HANDLING.
static XFILE * xio_getfptr(FILE *f)
Internal XIO locator.
static void init()
initialize XIO library
XFILEMAP xfiles_map_
mapping instance
FILE * xfopen(const std::string &fname, const char *mode)