40 static XFILE xstdin = {&std::string(
"stdin")[0], &std::string(
"r")[0], 0};
41 static XFILE xstdout = {&std::string(
"stdout")[0], &std::string(
"w")[0], 0};
42 static XFILE xstderr = {&std::string(
"stderr")[0], &std::string(
"w")[0], 0};
47 #define XIO_WARN(f) LogOut() << "File pointer '" << (f) << "' not in xfiles_map. Opened with regular fopen() or already closed?" << std::endl; 48 #define XIO_PRINT_INFO(f) printf( "XIO: In function '%s', %s\n", __func__, xio_getfulldescription( f ) ) 49 #define XIO_DEBUG(f) do { if ( Xio::get_instance()->get_verbosity() > 0 ) XIO_PRINT_INFO(f); } while (0) 77 WarningOut() <<
"The XIO library already initialized." << std::endl;
174 const char prn_format_long[] =
"FILE ptr %p: x->name '%s', x->mode '%s', x->line '%d'";
175 const char prn_format_short[] =
"FILE ptr %p: unknown FILE pointer";
176 static char * rs = NULL;
177 static int maxlen = 0;
184 rs = (
char *)xmalloc( maxlen );
194 rs = (
char *)xrealloc( rs, maxlen );
200 len = snprintf( rs, maxlen, prn_format_short, f );
204 rs = (
char *)xrealloc( rs, maxlen );
205 snprintf( rs, maxlen, prn_format_short, f );
229 FILE *
xfopen(
const std::string& fname,
const char *mode )
231 const char *fnamec = fname.c_str();
232 return xfopen(fnamec,mode);
241 FILE *
xfopen(
const char *fname,
const char *mode )
246 OLD_ASSERT(!( (fname == NULL) || (mode == NULL) ),
"NULL pointer as argument of function xfopen()\n");
247 LogOut() <<
"Opening file: '" << fname <<
"'" << std::endl;
248 rc = fopen( fname, mode );
249 INPUT_CHECK( rc != NULL ,
"Cannot open file '%s' with permissions %s\n", fname, mode );
253 xf->
filename = (
char *)xmalloc(strlen(fname)+1);
255 xf->
mode = (
char *)xmalloc(strlen(mode)+1);
256 strcpy(xf->
mode, mode);
274 OLD_ASSERT(!(f == NULL),
"NULL as input argument\n");
297 OLD_ASSERT(!( stream == NULL ),
"NULL pointer as argument of function xfclose()\n");
301 rc = fclose( stream );
331 FILE *
xfreopen(
const char * filename,
const char * mode, FILE * stream )
336 OLD_ASSERT(!( (mode == NULL) || (stream == NULL)),
"Wrong arguments\n");
338 rc = freopen( filename, mode, stream );
340 INPUT_CHECK( rc != NULL ,
"Cannot reopen file %s with permissions %s\n", filename, mode );
347 if ( strlen(filename) > strlen(xf->
filename) )
352 if ( strlen(mode) > strlen(xf->
mode) )
353 xf->
mode = (
char *)xrealloc( xf->
mode, strlen(mode)+1 );
354 strcpy(xf->
mode, mode);
362 xf->
filename = (
char *)xmalloc(strlen(filename));
364 xf->
mode = (
char *)xmalloc(strlen(mode));
365 strcpy(xf->
mode, mode);
383 OLD_ASSERT(!( (out == NULL) || (fmt == NULL) ),
"NULL pointer as argument of function xfprintf()\n");
384 va_start( argptr, fmt );
385 rc = vfprintf( out, fmt, argptr );
400 OLD_ASSERT(!( (in == NULL) || (fmt == NULL) ),
"NULL pointer as argument of function xfscanf()\n");
401 va_start( argptr , fmt );
402 rc = vfscanf( in, fmt, argptr );
478 if ( ( rc ==
'\n' ) && ( xf->
lineno > 0 ) )
498 int xrename (
const char * oldname,
const char * newname )
502 OLD_ASSERT(!(( oldname == NULL) || (newname == NULL)),
"NULL file name\n");
504 rc = rename( oldname, newname );
506 INPUT_CHECK( rc != 0,
"Failed when renaming '%s' to '%s'\n", oldname, newname );
518 size_t xfread(
void * ptr,
size_t size,
size_t count, FILE * stream )
522 OLD_ASSERT(!( (ptr == NULL) || ( stream == NULL) ),
"Incorrect arguments\n");
524 rc = fread( ptr, size, count, stream );
541 size_t xfwrite(
const void * ptr,
size_t size,
size_t count, FILE * stream )
545 OLD_ASSERT(!( (ptr == NULL) || (stream == NULL) ),
"Incorrect arguments\n");
547 rc = fwrite( ptr, size, count, stream );
568 OLD_ASSERT(!( (s == NULL) || (in == NULL) ),
"Incorrect arguments of function xfgets()\n");
569 rc = fgets( s, n, in );
606 OLD_ASSERT(!(f == NULL),
"NULL file argument in xrewind()\n");
int verbosity_
internal XIO debug: print info at each XIO function
int xrename(const char *oldname, const char *newname)
Changes the name of the file or directory specified by oldname to newname.
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 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.
#define INPUT_CHECK(i,...)
Debugging macros.
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...
#define LogOut()
Macro defining 'log' record of log.
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
int lineno
last read line (only for text files)
#define WarningOut()
Macro defining 'warning' record of log.
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)