1:
2:
3: /* Cfour (C++ Common Classes Collection)
4: * Copyright (C) 2001, (C) 2002 Jeffrey Bakker
5: *
6: * This program is free software; you can redistribute it and/or modify
7: * it under the terms of the GNU General Public License as published by
8: * the Free Software Foundation; either version 2 of the License, or
9: * (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19: *
20: *
21: * Author: Jeffrey Bakker
22: * Filename: cffile.h
23: * File version: 1.2.3
24: *
25: * CHANGELOG ==================================================================
26: * ============================================================================
27: *
28: * Created on: August 21st, 2001
29: *
30: *
31: * Modified on: September 2nd, 2001
32: *
33: * - Added a new read(), rline(), and write() methods for reading
34: * and writing to and from a file.
35: *
36: * Modified on: November 7th, 2001
37: *
38: * - Added a new isIredir(), and isOredir(), toggleImode(), and
39: * toggleOmode() methods and booleans data iredir & oredir for
40: * I/O redirection.
41: *
42: * Modified on: November 8th, 2001
43: *
44: * - Overloaded the insertion and extraction operators.
45: * - The read() and write() methods are now template methods.
46: *
47: * Modified on: December 30th, 2001
48: *
49: * - Added void setIFptr(long), void setOFptr (long),
50: * long getIFptr(), and long getOFptr() methods for
51: * get/setting the location of the in/out file pointers.
52: * - Added CFfile::READ and CFfile::WRITE constants to send
53: * as file modes to the constructor and open method.
54: *
55: * Modified on: March 30th, 2002
56: *
57: * - Extraction and insertion operator now return CFfile&, for
58: * concatenating multiple stream commands into a single call
59: * to the stream object. eg. IO << "this" << that << 2 << 'y';
60: * - Added rfile() method, which reads the contents of the
61: * currently open input file into a string.
62: * - Defined ENDL and DOSENDL as "\n" and "\r\n".
63: *
64: * Modified on: March 31st, 2002
65: *
66: * - Added an overloaded openW() method, which takes a bool
67: * parameter for overwriting or aborting on existing files.
68: * - Redone read() and write() using the conditional operator
69: * rather than using if/else.
70: * - Replaced read/write mode constants with #defines, and
71: * added more modes.
72: * - All templated methods are now implemented in the header
73: * file to fix unresolved symbols when linking with the
74: * microsoft compiler.
75: *
76: * Modified on: April 12th, 2002
77: *
78: * - Improved and added more whitespace defines, and set the file
79: * open method defines to Hex format.
80: * - The read() and write() methods now use positive logic.
81: *
82: * Modified on: April 30th, 2002
83: *
84: * - Added members bool oopen, iopen, bool isOopen(), and
85: * bool isIopen() to check if the file streams are in use.
86: *
87: * ============================================================================
88: * ============================================================================
89: *
90: * Remark:
91: *
92: * This is the definition of the CFfile class. The CFfile class contains file
93: * I/O methods which automates file checking while opening I/O streams. You
94: * can now safely open and close files, error free.
95: *
96: * The extraction and insertion operators have been overloaded, in an attempt
97: * to turn this class into a stream class which wraps the fstream and the
98: * iostream classes nicely and conveniently together.
99: *
100: * The class has also been designed to easily switch between file I/O to
101: * STDIN/STDOUT, enabling the ability to write programs which provide the
102: * "piping" functionality from the command line.
103: *
104: * For specific details on the methods, see the documentation at the top of
105: * the file "cffile.cpp".
106: *
107: * ============================================================================
108: * _______. ..
109: */
110:
111:
112: #ifndef _C4_FILE_H
113: #define _C4_FILE_H
114:
115: #define C4_FILE_VER "1.2.3"
116:
117:
118: // whitespace defines ----------------------------------------------------------
119: #if defined(WIN32)
120: #define ENDL "\r\n"
121: #elif defined(UNIX)
122: #define ENDL "\n"
123: #else // other OS
124: #define ENDL "\n"
125: #endif // defined(WIN32/UNIX)
126:
127: #define NIXENDL "\n"
128: #define DOSENDL "\r\n"
129: #define BIG_TAB "\t\t"
130: #define SPACE_4 " "
131: #define SPACE_8 " "
132:
133: // file open modes ----------
134: #define MODE_READ 0x72
135: #define MODE_WRITE 0x77
136: #define PROMPT_OVERWRITE 0x77
137: #define FORCE_OVERWRITE 0x66
138: #define NEVER_OVERWRITE 0x6B
139: //--------------------------..
140:
141: #include <iostream>
142: #include <fstream>
143: #include <string>
144: using namespace std;
145:
146:
147: class CFfile {
148:
149: public:
150: // (Con/De)structors ---------------------------------------------------------
151: CFfile();
152: CFfile(string filename, char io);
153: CFfile(string filein, string fileout);
154: ~CFfile();
155: // Initialize bool data members ----------------------------------------------
156: void init_switches();
157: //----------------------------------------------------------------------------
158: // Operator overloading ------------------------------------------------------
159: template <class T> CFfile& operator<<(T var) {write(var);return *this;}
160: template <class T> CFfile& operator>>(T& var) {read (var);return *this;}
161: //----------------------------------------------------------------------------
162: // File open methods ---------------------------------------------------------
163: bool exists(string fname); // check if a file exists
164: bool openR(string INfile); // opens file for reading
165: bool openW(string OUTfile); // opens file for writing
166: bool openW(string fn, bool ow); // open, specify overwrite
167: bool open(string fn, char io); // open, specify mode
168:
169: bool isIopen() {return iopen;} // is infile open?
170: bool isOopen() {return oopen;} // is outfile open?
171: //----------------------------------------------------------------------------
172: // File pointer location methods ---------------------------------------------
173: void setIFptr(long location); // set the infile pointer location
174: void setOFptr(long location); // set the outfile pointer location
175: long getIFptr(); // get the infile pointer location
176: long getOFptr(); // get the outfile pointer location
177: //----------------------------------------------------------------------------
178: // I/O redirection methods ---------------------------------------------------
179: bool isIredir() const; // returns the input mode
180: bool isOredir() const; // returns the output mode
181: void toggleImode(); // toggles input mode (FILE or STDIN)
182: void toggleOmode(); // toggles output mode (FILE or STDIN)
183: //----------------------------------------------------------------------------
184: // Read/Write methods --------------------------------------------------------
185: template <class T> void read(T &buffer) {(iredir? cin:ifile) >> buffer;}
186: template <class T> void write(T buffer) {(oredir? cout:ofile) << buffer;}
187: // Special purpose read methods ----------------------------------------------
188: void rline(string &buffer); // read a line to the string
189: void rfile(string &buffer); // read a file to the string
190: void backup(string fname, string bname); // copy a file
191: // File close methods --------------------------------------------------------
192: void closeR(); // close the input file
193: void closeW(); // close the output file
194: void close(); // close all
195: //----------------------------------------------------------------------------
196: // These are public, so you can still use them raw ---------------------------
197: ifstream ifile; // input file stream
198: ofstream ofile; // output file stream
199: //----------------------------------------------------------------------------
200: protected:
201: // I/O redirection switches --------------------------------------------------
202: bool iredir;
203: bool oredir;
204: bool iopen;
205: bool oopen;
206: };
207:
208: #endif // _C4_FILE_H
209:
210:
| w | e | b | c | p | p |
|
| |||||