Flow123d  release_2.2.0-914-gf1a3a4f
Macros | Functions
xio.cc File Reference

I/O functions with filename storing, able to track current line in opened file. All standard stdio functions working with files (not stdin, stdout, stderr) should be replaced by their equivalents from XIO library. More...

#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <errno.h>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <iterator>
#include "xio.h"

Go to the source code of this file.

Macros

#define XIO_WARN(f)   LogOut() << "File pointer '" << (f) << "' not in xfiles_map. Opened with regular fopen() or already closed?" << std::endl;
 
#define XIO_PRINT_INFO(f)   printf( "XIO: In function '%s', %s\n", __func__, xio_getfulldescription( f ) )
 
#define XIO_DEBUG(f)   do { if ( Xio::get_instance()->get_verbosity() > 0 ) XIO_PRINT_INFO(f); } while (0)
 

Functions

static XFILExio_getfptr (FILE *f)
 Internal XIO locator. More...
 
char * xio_getfname (FILE *f)
 Get file name from pointer to FILE structure. More...
 
char * xio_getfmode (FILE *f)
 Get file mode from file stream. More...
 
int xio_getlinesread (FILE *f)
 Get number of lines that were completely read from file since fopen() or rewind() More...
 
char * xio_getfulldescription (FILE *f)
 Get pointer to string with full file description. More...
 
FILE * xfopen (const std::string &fname, const char *mode)
 
FILE * xfopen (const char *fname, const char *mode)
 fopen() with error handling and filename store More...
 
int xfflush (FILE *f)
 Flush file stream. More...
 
int xfclose (FILE *stream)
 FCLOSE WITH ERROR HANDLING. More...
 
FILE * xfreopen (const char *filename, const char *mode, FILE *stream)
 Reopen stream with different file or mode. More...
 
int xfprintf (FILE *out, const char *fmt,...)
 FPRINTF WITH ERROR HANDLING. More...
 
int xfscanf (FILE *in, const char *fmt,...)
 FSCANF WITH ERROR HANDLING. More...
 
int xgetc (FILE *f)
 getc() with error handling and line count More...
 
int xfgetc (FILE *f)
 fgetc() with error handling and line count More...
 
int xungetc (int c, FILE *f)
 ungetc() with error handling and line count More...
 
int xrename (const char *oldname, const char *newname)
 Changes the name of the file or directory specified by oldname to newname. More...
 
size_t xfread (void *ptr, size_t size, size_t count, FILE *stream)
 Read block of data from stream, handle errors. More...
 
size_t xfwrite (const void *ptr, size_t size, size_t count, FILE *stream)
 Write block of data to stream, handle errors. More...
 
char * xfgets (char *s, int n, FILE *in)
 FGETS WITH ERROR HANDLING and line count. More...
 
void xrewind (FILE *f)
 Rewind file, handle line count. More...
 
int xfeof (FILE *f)
 Check END OF FILE. More...
 

Variables

static XFILE xstdin = {&std::string("stdin")[0], &std::string("r")[0], 0}
 basic definitions More...
 
static XFILE xstdout = {&std::string("stdout")[0], &std::string("w")[0], 0}
 
static XFILE xstderr = {&std::string("stderr")[0], &std::string("w")[0], 0}
 

Detailed Description

I/O functions with filename storing, able to track current line in opened file. All standard stdio functions working with files (not stdin, stdout, stderr) should be replaced by their equivalents from XIO library.

 * Copyright (C) 2015 Technical University of Liberec. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Author
Jiri Jenicek
Todo:
Better error handling ( perror()? strerror()? )

Definition in file xio.cc.

Macro Definition Documentation

#define XIO_DEBUG (   f)    do { if ( Xio::get_instance()->get_verbosity() > 0 ) XIO_PRINT_INFO(f); } while (0)

Definition at line 49 of file xio.cc.

#define XIO_PRINT_INFO (   f)    printf( "XIO: In function '%s', %s\n", __func__, xio_getfulldescription( f ) )

Definition at line 48 of file xio.cc.

#define XIO_WARN (   f)    LogOut() << "File pointer '" << (f) << "' not in xfiles_map. Opened with regular fopen() or already closed?" << std::endl;

Definition at line 47 of file xio.cc.

Function Documentation

int xfclose ( FILE *  stream)

FCLOSE WITH ERROR HANDLING.

Close file (function)

Parameters
[in,out]streampointer to FILE structure
Returns
same as ISO C fclose()

Definition at line 292 of file xio.cc.

Here is the caller graph for this function:

int xfeof ( FILE *  f)

Check END OF FILE.

Error-handling.

Parameters
[in]fpointer to FILE structure
Returns
same as ISO C feof()

Definition at line 628 of file xio.cc.

int xfflush ( FILE *  f)

Flush file stream.

Flush stream (function)

Parameters
[in,out]fpointer to FILE structure
Returns
same as ISO C fflush()

Definition at line 270 of file xio.cc.

int xfgetc ( FILE *  f)

fgetc() with error handling and line count

Get character from stream (function)

Parameters
[in,out]fpointer to FILE structure
Returns
same as ISO C fgetc()

Definition at line 431 of file xio.cc.

Here is the caller graph for this function:

char* xfgets ( char *  s,
int  n,
FILE *  in 
)

FGETS WITH ERROR HANDLING and line count.

Character input/output.

Parameters
[out]sPointer to an array of chars where the string read is stored
[in]nMaximum number of characters to be read (including the final null-character)
[in,out]inPointer to a FILE
Returns
same as ISO C fgets()

Definition at line 563 of file xio.cc.

Here is the caller graph for this function:

FILE* xfopen ( const std::string &  fname,
const char *  mode 
)

Definition at line 229 of file xio.cc.

Here is the caller graph for this function:

FILE* xfopen ( const char *  fname,
const char *  mode 
)

fopen() with error handling and filename store

File access.

Parameters
[in]fnamefilename to open
[in]modeopening mode
Returns
same as ISO C fopen()

Definition at line 241 of file xio.cc.

int xfprintf ( FILE *  out,
const char *  fmt,
  ... 
)

FPRINTF WITH ERROR HANDLING.

Formatted input/output.

Definition at line 378 of file xio.cc.

Here is the caller graph for this function:

size_t xfread ( void *  ptr,
size_t  size,
size_t  count,
FILE *  stream 
)

Read block of data from stream, handle errors.

Direct input/output.

Parameters
[out]ptrPointer to a block of memory with a minimum size of (size*count) bytes.
[in]sizeSize in bytes of each element to be read.
[in]countNumber of elements, each one with a size of size bytes.
[in,out]streamPointer to a FILE object that specifies an input stream.
Returns
same as ISO C fread()

Definition at line 518 of file xio.cc.

FILE* xfreopen ( const char *  filename,
const char *  mode,
FILE *  stream 
)

Reopen stream with different file or mode.

Parameters
[in]filenameName of the file to be opened
[in]modeFile access mode
[in,out]streamPointer to a FILE object that identifies the stream to be reopened
Returns
Same as ISO C freopen()

Definition at line 331 of file xio.cc.

int xfscanf ( FILE *  in,
const char *  fmt,
  ... 
)

FSCANF WITH ERROR HANDLING.

Read formatted data from stream (function)

Definition at line 395 of file xio.cc.

size_t xfwrite ( const void *  ptr,
size_t  size,
size_t  count,
FILE *  stream 
)

Write block of data to stream, handle errors.

Parameters
[in]ptrPointer to a block of memory with a minimum size of (size*count) bytes.
[in]sizeSize in bytes of each element to be written.
[in]countNumber of elements, each one with a size of size bytes.
[in,out]streamPointer to a FILE object that specifies an output stream.
Returns
same as ISO C fwrite()

Definition at line 541 of file xio.cc.

int xgetc ( FILE *  f)

getc() with error handling and line count

Get character from stream (function)

Parameters
[in,out]fpointer to FILE structure
Returns
same as ISO C getc()

Definition at line 414 of file xio.cc.

char* xio_getfmode ( FILE *  f)

Get file mode from file stream.

Parameters
[in]fpointer to FILE structure
Returns
pointer to file opening mode if OK, NULL if file stream is not known

Definition at line 126 of file xio.cc.

char* xio_getfname ( FILE *  f)

Get file name from pointer to FILE structure.

XIO library extensions.

Parameters
[in]fpointer to FILE structure
Returns
pointer to file name if OK, NULL if file stream is not known

Definition at line 103 of file xio.cc.

Here is the caller graph for this function:

static XFILE * xio_getfptr ( FILE *  f)
static

Internal XIO locator.

Parameters
[in]fpointer to FILE structure
Returns
pointer to XFILE structure if OK, NULL if file stream is not known

Definition at line 217 of file xio.cc.

Here is the caller graph for this function:

char* xio_getfulldescription ( FILE *  f)

Get pointer to string with full file description.

Parameters
[in]fpointer to FILE structure
Returns
pointer to string with description, null terminated, no LF

Definition at line 172 of file xio.cc.

int xio_getlinesread ( FILE *  f)

Get number of lines that were completely read from file since fopen() or rewind()

Get number of read lines from stream.

Parameters
[in]fpointer to FILE structure
Returns
number of lines read if OK, -1 if file stream is not known

Definition at line 149 of file xio.cc.

int xrename ( const char *  oldname,
const char *  newname 
)

Changes the name of the file or directory specified by oldname to newname.

Parameters
[in]oldnamename of the file to be renamed and/or moved
[in]newnamenew name
Returns
same as ISO C rename()

Definition at line 498 of file xio.cc.

void xrewind ( FILE *  f)

Rewind file, handle line count.

File positioning.

Parameters
[in,out]fpointer to FILE structure

Definition at line 602 of file xio.cc.

int xungetc ( int  c,
FILE *  f 
)

ungetc() with error handling and line count

Unget character from stream (function)

Parameters
[in]ccharacter to push back
[in,out]fpointer to FILE structure with INPUT stream
Returns
same as ISO C ungetc()

Definition at line 465 of file xio.cc.

Variable Documentation

XFILE xstderr = {&std::string("stderr")[0], &std::string("w")[0], 0}
static

Definition at line 42 of file xio.cc.

XFILE xstdin = {&std::string("stdin")[0], &std::string("r")[0], 0}
static

basic definitions

Definition at line 40 of file xio.cc.

XFILE xstdout = {&std::string("stdout")[0], &std::string("w")[0], 0}
static

Definition at line 41 of file xio.cc.