LineFile.h
Go to the documentation of this file.
00001 /*      
00002  * File: LineFile.h
00003  * Project: DUtils library
00004  * Author: Dorian Galvez-Lopez
00005  * Date: October 6, 2009
00006  * Description: reads and writes text line files
00007  *
00008  * 
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  */
00023 
00024 #pragma once
00025 #ifndef __D_LINE_FILE__
00026 #define __D_LINE_FILE__
00027 
00028 #include "DException.h"
00029 #include "FileModes.h"
00030 #include <vector>
00031 #include <fstream>
00032 using namespace std;
00033 
00034 namespace DUtils {
00035 
00036 class LineFile
00037 {
00038 public:
00039         /* Creates a linefile with no file
00040          */
00041         LineFile(void);
00042 
00043         /* Closes any opened file
00044         */
00045         ~LineFile(void);
00046 
00047         /* Creates a linefile by opening a file
00048          * @param filename
00049          * @param mode: READ or WRITE
00050          * @throws DException if cannot open the file
00051          */
00052         LineFile(const char *filename, const FILE_MODES mode);
00053         LineFile(const string &filename, const FILE_MODES mode);
00054 
00055         /* Opens a file for reading. It closes any other opened file
00056          * @param filename
00057          * @throws DException if cannot create the file
00058          */
00059         void OpenForReading(const char *filename);
00060         inline void OpenForReading(const string &filename)
00061         {
00062                 OpenForReading(filename.c_str());
00063         }
00064 
00065         /* Opens a file for writing. It closes any other opened file
00066          * @param filename
00067          * @throws DException if cannot create the file
00068          */
00069         void OpenForWriting(const char *filename);
00070         inline void OpenForWriting(const string &filename)
00071         {
00072                 OpenForWriting(filename.c_str());
00073         }
00074 
00075         /* Opens a file for writing at the end. It closes any other opened file
00076          * @param filename
00077          * @throws DException if cannot open the file
00078          */
00079         void OpenForAppending(const char *filename);
00080         inline void OpenForAppending(const string &filename)
00081         {
00082                 OpenForAppending(filename.c_str());
00083         }
00084 
00085         /* Says whether the end of the file has been reached. It is not necessary
00086          * to read a last empty line to reach eof
00087          * @return true iif there is nothing else to read
00088          * @thows DException if wrong access mode
00089          */
00090         bool Eof();
00091 
00092         /* Closes any opened file. It is not necessary to call this function
00093          * explicitly
00094          */
00095         void Close();
00096 
00097         /* Writes a line
00098          * @thows DException if wrong access mode
00099          */
00100         LineFile& operator<< (const char *s);
00101         inline LineFile& operator<< (const string &s)
00102         {
00103                 return this->operator <<(s.c_str()); 
00104         }
00105 
00106         /* Reads a line
00107          * @param s[]: buffer to store the string. Memory must be allocated
00108          * @param s: string to write on
00109          * @thows DException if wrong access mode
00110          */
00111         LineFile& operator>> (string &s);
00112 
00113         /* Writes several lines at a time
00114          * @v: vector of line strings
00115          * @throws DException if wrong access mode
00116          */
00117         void Dump(const vector<string> &v);
00118         inline LineFile& operator<< (const vector<string> &v)
00119         {
00120                 Dump(v);
00121                 return *this;
00122         }
00123 
00124         /* In reading mode, reads and throws away the next line
00125          */
00126         void DiscardLine();
00127 
00128 protected:
00129 
00136         void Init(const char *filename, const FILE_MODES mode);
00137 
00138 protected:
00139         FILE_MODES m_mode;              // opening mode
00140         fstream m_f;                    // fstream
00141         string m_next_line;     // next line to read
00142 };
00143 
00144 }
00145 
00146 #endif
00147 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:39