Flow123d  jenkins-Flow123d-linux-release-multijob-282
xio.h
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file xio.h
26  * @brief I/O functions with filename storing, able to track current line in opened file. All standard
27  * stdio functions working with files (not stdin, stdout, stderr) should be replaced
28  * by their equivalents from XIO library.
29  *
30  * @date 6.4.2010
31  * @author Jiri Jenicek
32  *
33  */
34 
35 /*
36  *
37  * Commented lines contains functions of stdio library that are not used in Flow123d.
38  * Their equivalents are currently not implemented in XIO library.
39  *
40  */
41 
42 #ifndef XIO_H_
43 #define XIO_H_
44 
45 #include "global_defs.h"
46 #include "system/system.hh"
47 
48 #include <map>
49 
50 // Size of buffer for text input lines.
51 #define LINE_SIZE 65536
52 
53 //! @brief XFILE structure holds additional info to generic FILE
54 /// @{
55 typedef struct xfile {
56  char * filename; ///< file name in the time of opening
57  char * mode; ///< opening mode
58  int lineno; ///< last read line (only for text files)
59 } XFILE;
60 //! @}
61 
62 
63 /**
64  * Base class of XIO library.
65  *
66  * The class implements a singleton pattern.
67  * Stores data of file mapping and debug output for XIO function.
68  */
69 class Xio {
70 public:
71  /// mapping of ptr to regular file structure to extended structure
73 
74  /// return instance
75  static Xio *get_instance();
76  /// initialize XIO library
77  static void init();
78 
79  /// Enable/Disable XIO debug output for EACH XIO function call
80  void set_verbosity(int verb);
81  /// Get current XIO debug verbosity level
82  int get_verbosity();
83  /// Get XIO mapping instance
85 
86 private:
87  // Singleton instance
88  static Xio *instance;
89 
90  /**
91  * Constructor.
92  *
93  * Initialize XIO library.
94  */
95  Xio();
96 
97  /// mapping instance
99  /// internal XIO debug: print info at each XIO function
101 
102 };
103 
104 //! @brief XIO library extensions
105 /// @{
106 char * xio_getfname( FILE * f ); ///< Get file name from file stream
107 char * xio_getfmode( FILE * f ); ///< Get file mode from file stream
108 int xio_getlinesread( FILE * f ); ///< Get number of read lines from stream
109 char * xio_getfulldescription( FILE * f ); ///< Get pointer to string with full file description
110 //! @}
111 
112 //! @brief File access
113 /// @{
114 FILE * xfopen( const char * fname, const char * mode ); ///< Open file (function)
115 FILE * xfopen( const std::string &fname, const char * mode );
116 int xfclose( FILE * stream ); ///< Close file (function)
117 int xfflush( FILE * f ); ///< Flush stream (function)
118 FILE * xfreopen( const char * filename, const char * mode, FILE * stream );
119 //! @}
120 
121 //! @brief Formatted input/output
122 /// @{
123 int xfprintf( FILE * out, const char * fmt, ... ); ///< Write formatted output to stream (function)
124 int xfscanf( FILE * in, const char * fmt, ... ); ///< Read formatted data from stream (function)
125 //! @}
126 
127 //! @brief Character input/output
128 /// @{
129 char * xfgets( char *s, int n, FILE *in ); ///< Get string from stream (function)
130 int xfgetc( FILE * f ); ///< Get character from stream (function)
131 int xgetc( FILE * f ); ///< Get character from stream (function)
132 int xungetc( int c, FILE * f ); ///< Unget character from stream (function)
133 
134 //! @}
135 
136 //! @brief Direct input/output
137 /// @{
138 size_t xfread( void * ptr, size_t size, size_t count, FILE * stream ); ///< Read block of data from stream (function)
139 size_t xfwrite( const void * ptr, size_t size, size_t count, FILE * stream ); ///< Write block of data to stream (function)
140 //! @}
141 
142 //! @brief File positioning
143 /// @{
144 void xrewind( FILE * f ); ///< Set position indicator to the beginning (function)
145 //! @}
146 
147 //! @brief Error-handling
148 /// @{
149 int xfeof ( FILE * f ); ///< Check End-of-File indicator (function)
150 //! @}
151 
152 #endif /* XIO_H_ */
int verbosity_
internal XIO debug: print info at each XIO function
Definition: xio.h:100
static Xio * instance
Definition: xio.h:88
char * mode
opening mode
Definition: xio.h:57
Definition: xio.h:69
void set_verbosity(int verb)
Enable/Disable XIO debug output for EACH XIO function call.
Definition: xio.cc:98
int xfflush(FILE *f)
Flush file stream.
Definition: xio.cc:287
void xrewind(FILE *f)
Rewind file, handle line count.
Definition: xio.cc:619
static Xio * get_instance()
return instance
Definition: xio.cc:83
struct xfile XFILE
XFILE structure holds additional info to generic FILE.
int xfclose(FILE *stream)
FCLOSE WITH ERROR HANDLING.
Definition: xio.cc:309
size_t xfwrite(const void *ptr, size_t size, size_t count, FILE *stream)
Write block of data to stream, handle errors.
Definition: xio.cc:558
char * xio_getfname(FILE *f)
Get file name from pointer to FILE structure.
Definition: xio.cc:120
int xgetc(FILE *f)
getc() with error handling and line count
Definition: xio.cc:431
int xfprintf(FILE *out, const char *fmt,...)
FPRINTF WITH ERROR HANDLING.
Definition: xio.cc:395
char * xfgets(char *s, int n, FILE *in)
FGETS WITH ERROR HANDLING and line count.
Definition: xio.cc:580
size_t xfread(void *ptr, size_t size, size_t count, FILE *stream)
Read block of data from stream, handle errors.
Definition: xio.cc:535
int xio_getlinesread(FILE *f)
Get number of lines that were completely read from file since fopen() or rewind() ...
Definition: xio.cc:166
XFILEMAP & get_xfile_map()
Get XIO mapping instance.
Definition: xio.cc:108
FILE * xfreopen(const char *filename, const char *mode, FILE *stream)
Reopen stream with different file or mode.
Definition: xio.cc:348
Global macros to enhance readability and debugging, general constants.
char * xio_getfmode(FILE *f)
Get file mode from file stream.
Definition: xio.cc:143
XFILE structure holds additional info to generic FILE.
Definition: xio.h:55
char * xio_getfulldescription(FILE *f)
Get pointer to string with full file description.
Definition: xio.cc:189
int lineno
last read line (only for text files)
Definition: xio.h:58
int xfgetc(FILE *f)
fgetc() with error handling and line count
Definition: xio.cc:448
map< FILE *, XFILE * > XFILEMAP
mapping of ptr to regular file structure to extended structure
Definition: xio.h:72
char * filename
file name in the time of opening
Definition: xio.h:56
int xfeof(FILE *f)
Check END OF FILE.
Definition: xio.cc:645
int get_verbosity()
Get current XIO debug verbosity level.
Definition: xio.cc:103
int xungetc(int c, FILE *f)
ungetc() with error handling and line count
Definition: xio.cc:482
int xfscanf(FILE *in, const char *fmt,...)
FSCANF WITH ERROR HANDLING.
Definition: xio.cc:412
static void init()
initialize XIO library
Definition: xio.cc:89
XFILEMAP xfiles_map_
mapping instance
Definition: xio.h:98
Xio()
Definition: xio.cc:75
FILE * xfopen(const std::string &fname, const char *mode)
Definition: xio.cc:246