/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/fileio.hpp Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/fileio.hpp Source File
Ratpac-two
fileio.hpp
1 #ifndef FILEIO_HPP
2 #define FILEIO_HPP
3 /*------------------------------------------------------------------------------
4 
5  Author: Andy Rushton
6  Copyright: (c) Andy Rushton, 2004
7  License: BSD License, see ../docs/license.html
8 
9  TextIO classes customised to use C stdio FILE*
10 
11 ------------------------------------------------------------------------------*/
12 #include <stddef.h>
13 #include <stdio.h>
14 
15 #include <cstring>
16 
17 #include "os_fixes.hpp"
18 #include "textio.hpp"
20 // Output Files
21 
22 class oftext : public otext {
23  public:
24  static size_t preferred_buffer;
25 
26  // create an uninitialised device which acts like /dev/null
27  oftext(void);
28  // attach an already-opened file to this device
29  // this will not be closed when the device is destroyed
30  // Note: on Windoze, already-open files such as stdout/err are treated as Unix mode because Windoze does eoln
31  // conversion
32  oftext(FILE* handle, bool line_buffer = false);
33  // open the file and attach it to this device
34  // this will be closed automatically when the device is destroyed
35  oftext(const char* filename, size_t bufsize = preferred_buffer, open_t mode = overwrite, bool line_buffer = false);
36  oftext(const std::string& filename, size_t bufsize = preferred_buffer, open_t mode = overwrite,
37  bool line_buffer = false);
38 
39  // similar to the constructors - these destroy the previous device contents (closing if appropriate)
40  // then perform the open/attach as above
41  void open(FILE* handle, bool line_buffer = false);
42  void open(const char* filename, size_t bufsize = preferred_buffer, open_t mode = overwrite, bool line_buffer = false);
43  void open(const std::string& filename, size_t bufsize = preferred_buffer, open_t mode = overwrite,
44  bool line_buffer = false);
45 
46  // get at the internal handle
47  // note that the handle and this device are guaranteed to be synchronised!
48  operator FILE*(void);
49 };
50 
52 // Input Files
53 
54 class iftext : public itext {
55  public:
56  static size_t preferred_buffer;
57 
58  // create an uninitialised device which acts like /dev/null
59  iftext(void);
60  // attach an already-opened file to this device
61  // this will not be closed when the device is destroyed
62  iftext(FILE* handle);
63  // open the file and attach it to this device
64  // this will be closed automatically when the device is destroyed
65  iftext(const char* filename, size_t bufsize = preferred_buffer);
66  iftext(const std::string& filename, size_t bufsize = preferred_buffer);
67 
68  // similar to the constructors - these destroy the previous device contents (closing if appropriate)
69  // then perform the open/attach as above
70  void open(FILE* handle);
71  void open(const char* filename, size_t bufsize = preferred_buffer);
72  void open(const std::string& filename, size_t bufsize = preferred_buffer);
73 
74  // get at the internal handle
75  // note that the handle and this device are guaranteed to be synchronised!
76  operator FILE*(void);
77 };
78 
80 // Internal buffers
81 
82 class ofbuff : public obuff {
83  friend class oftext;
84 
85  public:
86  ofbuff(FILE* handle, bool line_buffer);
87  ofbuff(const char* filename, size_t bufsize, otext::open_t mode, bool line_buffer);
88  ofbuff(const std::string& filename, size_t bufsize, otext::open_t mode, bool line_buffer);
89  virtual ~ofbuff(void);
90 
91  void open(const std::string& fname, size_t bufsize, otext::open_t mode, bool line_buffer);
92  virtual void flush(void);
93  virtual unsigned put(unsigned char);
94  virtual std::string error_string(void) const;
95 
96  private:
97  bool m_managed;
98  FILE* m_handle;
99 
100  // make this class uncopyable
101  ofbuff(const ofbuff&);
102  ofbuff& operator=(const ofbuff&);
103 };
104 
105 class ifbuff : public ibuff {
106  friend class iftext;
107 
108  public:
109  ifbuff(FILE*);
110  ifbuff(const char*, size_t bufsize = iftext::preferred_buffer);
111  ifbuff(const std::string&, size_t bufsize = iftext::preferred_buffer);
112  virtual ~ifbuff(void);
113 
114  void open(const std::string& filename, size_t bufsize);
115  virtual int peek(void);
116  virtual int get(void);
117  virtual std::string error_string(void) const;
118 
119  private:
120  bool m_managed;
121  FILE* m_handle;
122 
123  // make this class uncopyable
124  ifbuff(const ifbuff&);
125  ifbuff& operator=(const ifbuff&);
126 };
127 
129 // Standard Files
131 
132 extern iftext fin;
133 extern oftext fout;
134 extern oftext ferr;
135 extern oftext fnull;
136 
138 
139 #endif
Definition: textio.hpp:502
Definition: fileio.hpp:105
Definition: fileio.hpp:54
Definition: textio.hpp:242
Definition: textio.hpp:399
Definition: fileio.hpp:82
Definition: fileio.hpp:22
Definition: textio.hpp:37