Flow123d
release_3.0.0-973-g92f55e826
|
Go to the documentation of this file.
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 xfprintf(FILE *out, const char *fmt,...)
FPRINTF 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.
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
char * xfgets(char *s, int n, FILE *in)
FGETS WITH ERROR HANDLING and line count.
int xio_getlinesread(FILE *f)
Get number of lines that were completely read from file since fopen() or rewind()
int xfscanf(FILE *in, const char *fmt,...)
FSCANF WITH ERROR HANDLING.
FILE * xfopen(const std::string &fname, const char *mode)
static XFILE * xio_getfptr(FILE *f)
Internal XIO locator.
int get_verbosity()
Get current XIO debug verbosity level.
static XFILE xstdin
basic definitions
static void init()
initialize XIO library
FILE * xfreopen(const char *filename, const char *mode, FILE *stream)
Reopen stream with different file or mode.
#define LogOut()
Macro defining 'log' record of log.
int xfflush(FILE *f)
Flush file stream.
void xrewind(FILE *f)
Rewind file, handle line count.
char * filename
file name in the time of opening
int verbosity_
internal XIO debug: print info at each XIO function
static Xio * get_instance()
return instance
char * xio_getfname(FILE *f)
Get file name from pointer to FILE structure.
XFILE structure holds additional info to generic FILE.
size_t xfread(void *ptr, size_t size, size_t count, FILE *stream)
Read block of data from stream, handle errors.
int lineno
last read line (only for text files)
int xfgetc(FILE *f)
fgetc() with error handling and line count
int xfeof(FILE *f)
Check END OF FILE.
char * xio_getfulldescription(FILE *f)
Get pointer to string with full file description.
int xungetc(int c, FILE *f)
ungetc() with error handling and line count
#define WarningOut()
Macro defining 'warning' record of log.
int xrename(const char *oldname, const char *newname)
Changes the name of the file or directory specified by oldname to newname.
char * xio_getfmode(FILE *f)
Get file mode from file stream.
#define INPUT_CHECK(i,...)
Debugging macros.
int xfclose(FILE *stream)
FCLOSE WITH ERROR HANDLING.
void set_verbosity(int verb)
Enable/Disable XIO debug output for EACH XIO function call.
XFILEMAP & get_xfile_map()
Get XIO mapping instance.
XFILEMAP xfiles_map_
mapping instance
int xgetc(FILE *f)
getc() with error handling and line count