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.cpp
23: * File version: 1.2.3
24: *
25:
26: * CHANGELOG =================================================================
27: * ===========================================================================
28: *
29: * Created on: August 21st, 2001
30: *
31: *
32: * Modified on: September 2nd, 2001
33: *
34: * - Added a new read(), rline(), and write() methods for reading
35: * and writing to and from a file.
36: *
37: * Modified on: November 7th, 2001
38: *
39: * - Added a new isIredir(), and isOredir(), toggleImode(), and
40: * toggleOmode() methods and most cout's were changed to cerr
41: * for I/O redirection.
42: *
43: * Modified on: November 8th, 2001
44: *
45: * - Added template operaor<< and operator>> overloading.
46: * - The read() and write() methods are now template methods.
47: *
48: * Modified on: December 28th, 2001
49: *
50: * - Constructor CFfile(string,char) now calls open(string,char).
51: * - Added 'r' and 'w' to the io mode switch statement in open().
52: * - Updated documentation
53: *
54: * Modified on: March 30th, 2002
55: *
56: * - Extraction and insertion operator now return CFfile&, for
57: * concatenating multiple stream commands into a single call
58: * to the stream object. eg. IO << "this" << that << 2 << 'y';
59: * - Added rfile() method, which reads the contents of the
60: * currently open input file into a string.
61: * - Added an overloaded openW() method, which takes a bool
62: * parameter for overwriting or aborting on existing files.
63: *
64: * Modified on: March 31st, 2002
65: *
66: * - All templated methods are now implemented in the header
67: * file to fix unresolved symbols when linking with the
68: * microsoft compiler.
69: *
70: * Modified on: April 30th, 2002
71: *
72: * - Add code in constructors and the openX() methods to interact
73: * with iopen and oopen, to verify open file streams.
74: *
75: * ===========================================================================
76: * ===========================================================================
77: *
78: * Remark:
79: *
80: * This is the implementation of the CFfile class. In here, you will find
81: * the inner workings of the CFfile class. The documentation below will show
82: * how to use the methods.
83: *
84: * ===========================================================================
85: * BEGIN METHOD DOCUMENTATION ================================================
86: * ===========================================================================
87: *
88: * template <class T> CFfile& operator<<():
89: *
90: * Description: overloaded operator allows output stream of many data types.
91: *
92: * ===========================================================================
93: *
94: * template <class T> CFfile& operator>>():
95: *
96: * Description: overloaded operator allows input stream of many data types.
97: *
98: * ===========================================================================
99: *
100: * CFfile():
101: *
102: * Description: default constructor.
103: *
104: * ===========================================================================
105: *
106: * CFfile(string filename, char io):
107: *
108: * Description: Overloaded constructor that takes a filename and anI/O mode.
109: * This constructor calls open(string,char).
110: *
111: * Input:
112: *
113: * Parameter 1
114: * - Name: filename
115: * - Type: string
116: * - Description: the name of the file to open
117: *
118: *
119: * Parameter 2
120: * - Name: io
121: * - Type: char
122: * - Description: the mode in which to open the file ('i/r' or 'o/w').
123: *
124: * ===========================================================================
125: *
126: * bool open(string filename, char io):
127: *
128: * Description: Depending on the mode selected, it passes the filename to
129: * openR(string) or openW(string).
130: *
131: * Input:
132: *
133: * Parameter 1
134: * - Name: filename
135: * - Type: string
136: * - Description: the name of the file to open
137: *
138: *
139: * Parameter 2
140: * - Name: io
141: * - Type: char
142: * - Description: the mode in which to open the file ('i/r' or 'o/w').
143: *
144: * Output:
145: * - returns true if file opened correctly, false otherwise.
146: *
147: * ===========================================================================
148: *
149: * bool openR(string filename):
150: *
151: * Description: This opens the specified file for reading and returns true,
152: * if the file exists. If not, it will return false.
153: *
154: * Input:
155: *
156: * Parameter 1
157: * - Name: filename
158: * - Type: string
159: * - Description: the name of the file to open
160: *
161: * Output:
162: * - returns true if file opened correctly, false otherwise.
163: *
164: * ===========================================================================
165: *
166: * bool openW(string filename):
167: *
168: * Description: This opens the specified file for writing. If the file
169: * already exists, it asks the user whether to overwrite.
170: *
171: * Input:
172: *
173: * Parameter 1
174: * - Name: filename
175: * - Type: string
176: * - Description: the name of the file to open
177: *
178: * Output:
179: * - returns true if file opened correctly, false otherwise.
180: *
181: * ===========================================================================
182: *
183: * void rline(T &buffer):
184: *
185: * Description: reads a whole line from the file into the buffer.
186: *
187: * Input:
188: *
189: * Parameter 1
190: * - Name: buffer
191: * - Type: address of string
192: * - Description: the string in which to store the data
193: *
194: * ===========================================================================
195: *
196: * void rfile(T &buffer):
197: *
198: * Description: reads the currently open input file into the buffer.
199: *
200: * Input:
201: *
202: * Parameter 1
203: * - Name: buffer
204: * - Type: address of string
205: * - Description: the string in which to store the data
206: *
207: * ===========================================================================
208: *
209: * template <class T> void read(T &buffer):
210: *
211: * Description: reads a string from the file into the buffer.
212: *
213: * Input:
214: *
215: * Parameter 1
216: * - Name: buffer
217: * - Type: address of any data type
218: * - Description: the string in which to store the data
219: *
220: * ===========================================================================
221: *
222: * template <class T> void write(T buffer):
223: *
224: * Description: writes from the string buffer to the file.
225: *
226: * Input:
227: *
228: * Parameter 1
229: * - Name: buffer
230: * - Type: any data type
231: * - Description: the string in which to write from
232: *
233: * ===========================================================================
234: *
235: * void backup(string fname, string bname):
236: *
237: * Description: creates a backup of a file.
238: *
239: *
240: * Input:
241: *
242: * Parameter 1
243: * - Name: fname
244: * - Type: string
245: * - Description: the name of the file to copy from
246: *
247: *
248: * Parameter 2
249: * - Name: bname
250: * - Type: string
251: * - Description: the name of the backup file
252: *
253: * ===========================================================================
254: *
255: * bool exists(string fname):
256: *
257: * Description: checks whether or not a file exists
258: *
259: * Input:
260: *
261: * Parameter 1
262: * - Name: fname
263: * - Type: string
264: * - Description: the name of the file to check
265: *
266: * Output:
267: * - returns true if file exists, false otherwise.
268: *
269: * ===========================================================================
270: *
271: * void closeR():
272: *
273: * Description: closes the input stream _if_ it is open.
274: *
275: * ===========================================================================
276: *
277: * void closeW():
278: *
279: * Description: closes the output stream _if_ it is open.
280: *
281: * ===========================================================================
282: *
283: * void close():
284: *
285: * Description: calls closeR() and closeW().
286: *
287: * ===========================================================================
288: * END METHOD DOCUMENTATION ==================================================
289: * _______. ..
290: */
291:
292:
293: #include "cffile.h"
294: #include <iostream>
295: #include <cctype>
296: using namespace std;
297:
298: // constructors ---------------------------------------------------------------
299: CFfile::CFfile() {
300: init_switches();
301: } //---------------------------------------------------------------------------
302: CFfile::CFfile(string filename, char io) {
303: init_switches();
304: open(filename,io);
305: } //---------------------------------------------------------------------------
306: CFfile::CFfile(string in, string out) {
307: init_switches();
308: openR(in);
309: openW(out);
310: }
311: // destructor -----------------------------------------------------------------
312: CFfile::~CFfile() {
313: close();
314: }
315: // bool member initialization -------------------------------------------------
316: void CFfile::init_switches() {
317: iredir = false;
318: oredir = false;
319: iopen = false;
320: oopen = false;
321: }
322: // open methods ---------------------------------------------------------------
323: bool CFfile::open(string filename, char io) {
324:
325: io = tolower(io);
326: switch (io) {
327: case 'i':
328: case 'r': return openR(filename);
329: case 'o':
330: case 'w': return openW(filename);
331: case 'f': return openW(filename,true);
332: case 'k': return openW(filename,false);
333: default : cerr << "\nInvalid I/O mode.\n"; return false;
334: }
335:
336: return true;
337: } // --------------------------------------------------------------------------
338: bool CFfile::openR(string INfile) {
339:
340: if (exists(INfile)) {
341: ifile.open(INfile.data());
342: iopen = true;
343: return true;
344: } else {
345: cerr << ENDL << INfile << " cannot be opened.\n";
346: return false;
347: }
348: } // --------------------------------------------------------------------------
349: bool CFfile::openW(string OUTfile) {
350:
351: if (exists(OUTfile)) {
352: char write;
353: do {
354: cerr << ENDL << OUTfile << " already exists. Overwrite? (Y/N) ";
355: cin >> write;
356: if (write == 'N' || write == 'n') {return false;}
357: } while (!(write == 'Y' || write == 'y'));
358: }
359: ofile.open(OUTfile.data());
360: oopen = true;
361: return true;
362: } // --------------------------------------------------------------------------
363: bool CFfile::openW(string OUTfile, bool overwrite) {
364:
365: if (exists(OUTfile)) {
366: if(!overwrite) {return false;}
367: }
368: ofile.open(OUTfile.data());
369: oopen = true;
370: return true;
371: }
372: // ----------------------------------------------------------------------------
373: // close methods --------------------------------------------------------------
374: void CFfile::closeR() { // safely close the stream
375: if(ifile){
376: ifile.close();
377: iopen = false;
378: }
379: }
380: void CFfile::closeW() { // safely close the stream
381: if(ofile){
382: ofile.close();
383: oopen = false;
384: }
385: }
386: void CFfile::close() { // safely close all streams
387: closeR();
388: closeW();
389: }
390: // ----------------------------------------------------------------------------
391: // checks if a file exists ----------------------------------------------------
392: bool CFfile::exists(string fname) {
393:
394: ifstream chk;
395: chk.open(fname.data());
396:
397: if(!chk) {
398: chk.close();
399: return false;
400: }
401: chk.close();
402: return true;
403: } //---------------------------------------------------------------------------
404: // check input/output modes for redirection -----------------------------------
405: bool CFfile::isIredir() const {return iredir;} // is Input redirected?
406: bool CFfile::isOredir() const {return oredir;} // is Output redirected?
407: // toggle input/output modes for redirection ----------------------------------
408: void CFfile::toggleImode() {iredir = !iredir;}
409: void CFfile::toggleOmode() {oredir = !oredir;}
410:
411: // return the location of the pointer in the file -----------------------------
412: long CFfile::getIFptr() {return ifile.tellg();}
413: long CFfile::getOFptr() {return ofile.tellp();}
414: // set the location of the ponter in the file ---------------------------------
415: void CFfile::setIFptr(long location) {ifile.seekg(location);}
416: void CFfile::setOFptr(long location) {ofile.seekp(location);}
417:
418: // readline to string ---------------------------------------------------------
419: void CFfile::rline(string &buffer) {
420: if(!iredir) {getline(ifile,buffer);}
421: else getline(cin,buffer);
422: }
423: // readfile to string ---------------------------------------------------------
424: void CFfile::rfile(string &buffer) {
425:
426: long save_pos = getIFptr(); // save current position in file
427: setIFptr(0); // set position to the start of the file
428:
429: string line;
430: while(ifile) {
431: rline(line);
432: buffer += line + ENDL;
433: }
434:
435: setIFptr(save_pos); // restore previous position in file
436: } //---------------------------------------------------------------------------
437: // backup a file --------------------------------------------------------------
438: void CFfile::backup(string fname, string bname) {
439:
440: ifstream bin;
441: ofstream bout;
442: string linebuf;
443:
444: bin.open(fname.data());
445: bout.open(bname.data());
446:
447: getline(bin,linebuf);
448: while (bin) {
449: bout << linebuf;
450: getline(bin,linebuf);
451: if (bin) bout << ENDL;
452: }
453: bin.close();
454: bout.close();
455: } // --------------------------------------------------------------------------
456: //-----------------------------------------------------------------------------
457:
458:
| w | e | b | c | p | p |
|
| |||||