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 00037 class LineFile 00038 { 00039 public: 00043 LineFile(void); 00044 00048 ~LineFile(void); 00049 00056 LineFile(const char *filename, const FILE_MODES mode); 00057 00064 LineFile(const string &filename, const FILE_MODES mode); 00065 00071 void OpenForReading(const char *filename); 00072 00078 inline void OpenForReading(const string &filename) 00079 { 00080 OpenForReading(filename.c_str()); 00081 } 00082 00088 void OpenForWriting(const char *filename); 00089 00095 inline void OpenForWriting(const string &filename) 00096 { 00097 OpenForWriting(filename.c_str()); 00098 } 00099 00105 void OpenForAppending(const char *filename); 00106 00112 inline void OpenForAppending(const string &filename) 00113 { 00114 OpenForAppending(filename.c_str()); 00115 } 00116 00123 bool Eof(); 00124 00129 void Close(); 00130 00135 LineFile& operator<< (const char *s); 00136 00141 inline LineFile& operator<< (const string &s) 00142 { 00143 return this->operator <<(s.c_str()); 00144 } 00145 00151 LineFile& operator>> (string &s); 00152 00158 LineFile& operator>> (vector<string> &v); 00159 00165 void Dump(const vector<string> &v); 00166 00172 inline LineFile& operator<< (const vector<string> &v) 00173 { 00174 Dump(v); 00175 return *this; 00176 } 00177 00181 void DiscardLine(); 00182 00186 inline FILE_MODES GetOpenMode() const { return m_mode; } 00187 00188 protected: 00189 00196 void Init(const char *filename, const FILE_MODES mode); 00197 00198 protected: 00200 FILE_MODES m_mode; // opening mode 00202 fstream m_f; // fstream 00204 string m_next_line; // next line to read 00205 }; 00206 00207 } 00208 00209 #endif 00210