Flow123d  jenkins-Flow123d-windows-release-multijob-285
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
84  XFILEMAP &get_xfile_map();
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
98  XFILEMAP xfiles_map_;
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
size_t xfread(void *ptr, size_t size, size_t count, FILE *stream)
Direct input/output.
Definition: xio.cc:535
void set_verbosity(int verb)
Enable/Disable XIO debug output for EACH XIO function call.
Definition: xio.cc:98
int xfeof(FILE *f)
Error-handling.
Definition: xio.cc:645
FILE * xfopen(const char *fname, const char *mode)
File access.
Definition: xio.cc:258
static Xio * get_instance()
return instance
Definition: xio.cc:83
FILE * xfreopen(const char *filename, const char *mode, FILE *stream)
Reopen stream with different file or mode.
Definition: xio.cc:348
struct xfile XFILE
XFILE structure holds additional info to generic FILE.
int xgetc(FILE *f)
Get character from stream (function)
Definition: xio.cc:431
char * xio_getfulldescription(FILE *f)
Get pointer to string with full file description.
Definition: xio.cc:189
XFILEMAP & get_xfile_map()
Get XIO mapping instance.
Definition: xio.cc:108
int xfgetc(FILE *f)
Get character from stream (function)
Definition: xio.cc:448
void xrewind(FILE *f)
File positioning.
Definition: xio.cc:619
char * xfgets(char *s, int n, FILE *in)
Character input/output.
Definition: xio.cc:580
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
int xungetc(int c, FILE *f)
Unget character from stream (function)
Definition: xio.cc:482
int xfprintf(FILE *out, const char *fmt,...)
Formatted input/output.
Definition: xio.cc:395
Global macros to enhance readability and debugging, general constants.
XFILE structure holds additional info to generic FILE.
Definition: xio.h:55
int xfclose(FILE *stream)
Close file (function)
Definition: xio.cc:309
char * xio_getfname(FILE *f)
XIO library extensions.
Definition: xio.cc:120
int xfscanf(FILE *in, const char *fmt,...)
FSCANF WITH ERROR HANDLING.
Definition: xio.cc:412
char * xio_getfmode(FILE *f)
Get file mode from file stream.
Definition: xio.cc:143
int lineno
last read line (only for text files)
Definition: xio.h:58
int xio_getlinesread(FILE *f)
Get number of read lines from stream.
Definition: xio.cc:166
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 xfflush(FILE *f)
Flush stream (function)
Definition: xio.cc:287
int get_verbosity()
Get current XIO debug verbosity level.
Definition: xio.cc:103
static void init()
initialize XIO library
Definition: xio.cc:89
XFILEMAP xfiles_map_
mapping instance
Definition: xio.h:98
Xio()
Definition: xio.cc:75