LineFile.cpp
Go to the documentation of this file.
00001 /*      
00002  * File: LineFile.cpp
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 #include "LineFile.h"
00025 #include "DException.h"
00026 #include "FileModes.h"
00027 #include <vector>
00028 #include <string>
00029 using namespace std;
00030 
00031 using namespace DUtils;
00032 
00033 LineFile::LineFile(void): m_next_line("")
00034 {
00035 }
00036 
00037 LineFile::~LineFile(void)
00038 {
00039         Close();
00040 }
00041 
00042 LineFile::LineFile(const char *filename, const FILE_MODES mode)
00043 {
00044         Init(filename, mode);
00045 }
00046 
00047 LineFile::LineFile(const string &filename, const FILE_MODES mode)
00048 {
00049         Init(filename.c_str(), mode);
00050 }
00051 
00052 void LineFile::Init(const char *filename, const FILE_MODES mode)
00053 {
00054         m_next_line = "";
00055         
00056         if(mode & READ){
00057                 OpenForReading(filename);
00058         }else if((mode & WRITE) && (mode & APPEND)){
00059                 OpenForAppending(filename);
00060         }else if(mode & WRITE){
00061                 OpenForWriting(filename);
00062         }else{
00063                 throw DException("Wrong access mode");
00064         }
00065 }
00066 
00067 void LineFile::OpenForReading(const char *filename)
00068 {
00069         m_f.open(filename, ios::in);
00070         if(!m_f.is_open()){
00071                 throw DException(string("Cannot open ") + filename + " for reading");
00072         }else{
00073                 m_mode = READ;
00074         }
00075 }
00076 
00077 void LineFile::OpenForWriting(const char *filename)
00078 {
00079         m_f.open(filename, ios::out);
00080         if(!m_f.is_open()){
00081                 throw DException(string("Cannot open ") + filename + " for writing");
00082         }else{
00083                 m_mode = WRITE;
00084         }
00085 }
00086 
00087 void LineFile::OpenForAppending(const char *filename)
00088 {
00089         m_f.open(filename, ios::out | ios::app);
00090         if(!m_f.is_open()){
00091                 throw DException(string("Cannot open ") + filename + " for writing");
00092         }else{
00093                 m_mode = DUtils::FILE_MODES(WRITE | APPEND);
00094         }
00095 }
00096 
00097 void LineFile::Close()
00098 {
00099         if(m_f.is_open()) m_f.close();
00100 }
00101 
00102 bool LineFile::Eof()
00103 {
00104         if(!m_f.is_open()) return true;
00105 
00106         if(m_mode & READ){
00107                 if(m_f.eof())
00108                         return true;
00109                 else if(!m_next_line.empty())
00110                         return false;
00111                 else{
00112                         getline(m_f, m_next_line);
00113                         return m_f.eof();
00114                 }
00115         }else
00116                 throw DException("Wrong access mode");
00117 
00118 }
00119 
00120 LineFile& LineFile::operator<< (const char *s)
00121 {
00122         if(!m_f.is_open()) throw DException("File is not open");
00123 
00124         if(m_mode & WRITE)
00125                 m_f << s << endl;
00126         else
00127                 throw DException("Wrong access mode");
00128         
00129         return *this;
00130 }
00131 
00132 LineFile& LineFile::operator>> (string &s)
00133 {
00134         if(!m_f.is_open()) throw DException("File is not open");
00135 
00136         if(m_mode & READ){
00137                 if(m_f.eof()){
00138                         s.clear();
00139                 }else if(!m_next_line.empty()){
00140                         s = m_next_line;
00141                         m_next_line.clear();
00142                 }else{
00143                         getline(m_f, s);
00144                         if(m_f.eof()){
00145                                 s.clear();
00146                         }
00147                 }
00148                 
00149         }else
00150                 throw DException("Wrong access mode");
00151         
00152         return *this;
00153 }
00154 
00155 void LineFile::Dump(const vector<string> &v)
00156 {
00157         if(!m_f.is_open()) throw DException("File is not open");
00158                 
00159         if(m_mode & WRITE){
00160                 vector<string>::const_iterator it;
00161                 for(it = v.begin(); it != v.end(); it++){
00162                         m_f << *it << endl;
00163                 }
00164         }else
00165                 throw DException("Wrong access mode");
00166 }
00167 
00168 void LineFile::DiscardLine()
00169 {
00170         string nul;
00171         *this >> nul;
00172 }
00173 
00174 


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