Flow123d  release_3.0.0-885-g06276d1
xio.h
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file xio.h
15  * @brief I/O functions with filename storing, able to track current line in opened file. All standard
16  * stdio functions working with files (not stdin, stdout, stderr) should be replaced
17  * by their equivalents from XIO library.
18  * @author Jiri Jenicek
19  */
20 
21 /*
22  *
23  * Commented lines contains functions of stdio library that are not used in Flow123d.
24  * Their equivalents are currently not implemented in XIO library.
25  *
26  */
27 
28 #ifndef XIO_H_
29 #define XIO_H_
30 
31 #include "global_defs.h"
32 #include "system/system.hh"
33 
34 #include <map>
35 
36 // Size of buffer for text input lines.
37 #define LINE_SIZE 65536
38 
39 //! @brief XFILE structure holds additional info to generic FILE
40 /// @{
41 typedef struct xfile {
42  char * filename; ///< file name in the time of opening
43  char * mode; ///< opening mode
44  int lineno; ///< last read line (only for text files)
45 } XFILE;
46 //! @}
47 
48 
49 /**
50  * Base class of XIO library.
51  *
52  * The class implements a singleton pattern.
53  * Stores data of file mapping and debug output for XIO function.
54  */
55 class Xio {
56 public:
57  /// mapping of ptr to regular file structure to extended structure
59 
60  /// return instance
61  static Xio *get_instance();
62  /// initialize XIO library
63  static void init();
64 
65  /// Enable/Disable XIO debug output for EACH XIO function call
66  void set_verbosity(int verb);
67  /// Get current XIO debug verbosity level
68  int get_verbosity();
69  /// Get XIO mapping instance
70  XFILEMAP &get_xfile_map();
71 
72 private:
73  // Singleton instance
74  static Xio *instance;
75 
76  /**
77  * Constructor.
78  *
79  * Initialize XIO library.
80  */
81  Xio();
82 
83  /// mapping instance
84  XFILEMAP xfiles_map_;
85  /// internal XIO debug: print info at each XIO function
87 
88 };
89 
90 //! @brief XIO library extensions
91 /// @{
92 char * xio_getfname( FILE * f ); ///< Get file name from file stream
93 char * xio_getfmode( FILE * f ); ///< Get file mode from file stream
94 int xio_getlinesread( FILE * f ); ///< Get number of read lines from stream
95 char * xio_getfulldescription( FILE * f ); ///< Get pointer to string with full file description
96 //! @}
97 
98 //! @brief File access
99 /// @{
100 FILE * xfopen( const char * fname, const char * mode ); ///< Open file (function)
101 FILE * xfopen( const std::string &fname, const char * mode );
102 int xfclose( FILE * stream ); ///< Close file (function)
103 int xfflush( FILE * f ); ///< Flush stream (function)
104 FILE * xfreopen( const char * filename, const char * mode, FILE * stream );
105 //! @}
106 
107 //! @brief Formatted input/output
108 /// @{
109 int xfprintf( FILE * out, const char * fmt, ... ); ///< Write formatted output to stream (function)
110 int xfscanf( FILE * in, const char * fmt, ... ); ///< Read formatted data from stream (function)
111 //! @}
112 
113 //! @brief Character input/output
114 /// @{
115 char * xfgets( char *s, int n, FILE *in ); ///< Get string from stream (function)
116 int xfgetc( FILE * f ); ///< Get character from stream (function)
117 int xgetc( FILE * f ); ///< Get character from stream (function)
118 int xungetc( int c, FILE * f ); ///< Unget character from stream (function)
119 
120 //! @}
121 
122 //! @brief Direct input/output
123 /// @{
124 size_t xfread( void * ptr, size_t size, size_t count, FILE * stream ); ///< Read block of data from stream (function)
125 size_t xfwrite( const void * ptr, size_t size, size_t count, FILE * stream ); ///< Write block of data to stream (function)
126 //! @}
127 
128 //! @brief File positioning
129 /// @{
130 void xrewind( FILE * f ); ///< Set position indicator to the beginning (function)
131 //! @}
132 
133 //! @brief Error-handling
134 /// @{
135 int xfeof ( FILE * f ); ///< Check End-of-File indicator (function)
136 //! @}
137 
138 #endif /* XIO_H_ */
int verbosity_
internal XIO debug: print info at each XIO function
Definition: xio.h:86
static Xio * instance
Definition: xio.h:74
char * mode
opening mode
Definition: xio.h:43
Definition: xio.h:55
size_t xfread(void *ptr, size_t size, size_t count, FILE *stream)
Direct input/output.
Definition: xio.cc:518
int xfeof(FILE *f)
Error-handling.
Definition: xio.cc:628
FILE * xfopen(const char *fname, const char *mode)
File access.
Definition: xio.cc:241
FILE * xfreopen(const char *filename, const char *mode, FILE *stream)
Reopen stream with different file or mode.
Definition: xio.cc:331
struct xfile XFILE
XFILE structure holds additional info to generic FILE.
int xgetc(FILE *f)
Get character from stream (function)
Definition: xio.cc:414
char * xio_getfulldescription(FILE *f)
Get pointer to string with full file description.
Definition: xio.cc:172
int xfgetc(FILE *f)
Get character from stream (function)
Definition: xio.cc:431
void xrewind(FILE *f)
File positioning.
Definition: xio.cc:602
char * xfgets(char *s, int n, FILE *in)
Character input/output.
Definition: xio.cc:563
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:541
int xungetc(int c, FILE *f)
Unget character from stream (function)
Definition: xio.cc:465
int xfprintf(FILE *out, const char *fmt,...)
Formatted input/output.
Definition: xio.cc:378
Global macros to enhance readability and debugging, general constants.
XFILE structure holds additional info to generic FILE.
Definition: xio.h:41
int xfclose(FILE *stream)
Close file (function)
Definition: xio.cc:292
char * xio_getfname(FILE *f)
XIO library extensions.
Definition: xio.cc:103
int xfscanf(FILE *in, const char *fmt,...)
FSCANF WITH ERROR HANDLING.
Definition: xio.cc:395
char * xio_getfmode(FILE *f)
Get file mode from file stream.
Definition: xio.cc:126
int lineno
last read line (only for text files)
Definition: xio.h:44
int xio_getlinesread(FILE *f)
Get number of read lines from stream.
Definition: xio.cc:149
Definition: format.cc:82
map< FILE *, XFILE * > XFILEMAP
mapping of ptr to regular file structure to extended structure
Definition: xio.h:58
char * filename
file name in the time of opening
Definition: xio.h:42
int xfflush(FILE *f)
Flush stream (function)
Definition: xio.cc:270
XFILEMAP xfiles_map_
mapping instance
Definition: xio.h:84