![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsTextFile.cpp 00003 00004 TextFile is a class for reading/writing/modifying a text file. 00005 00006 SUKITTI PUNAK (02/17/2006) 00007 UPDATE (08/26/2009) 00008 ******************************************************************************/ 00009 #include "TAPsTextFile.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 BEGIN_NAMESPACE_TAPs 00015 //============================================================================= 00016 // Constructor(s) / Destructor 00017 //----------------------------------------------------------------------------- 00018 TextFile::TextFile () : m_eOpenFileType( Enum::READ ) 00019 {} 00020 //----------------------------------------------------------------------------- 00021 TextFile::TextFile ( std::string fileName, Enum::OpenFile type ) 00022 : m_strFileName( fileName ), m_eOpenFileType( type ) 00023 { 00024 switch ( m_eOpenFileType ) { 00025 case Enum::READ: 00026 m_inStream.open( m_strFileName.c_str(), std::ios::in ); 00027 if ( !m_inStream.is_open() ) { 00028 std::cerr << m_strFileName 00029 << " could not be opened for reading!" 00030 << std::endl; 00031 } 00032 break; 00033 case Enum::WRITE: 00034 m_outStream.open( m_strFileName.c_str(), std::ios::out ); 00035 if ( !m_outStream.is_open() ) { 00036 std::cerr << m_strFileName 00037 << " could not be opened for writing!" 00038 << std::endl; 00039 } 00040 break; 00041 case Enum::READ_AND_WRITE: 00042 std::cerr << "Not Implemented Yet!\n"; 00043 assert( false ); 00044 break; 00045 default: 00046 assert( false ); 00047 } 00048 } 00049 //----------------------------------------------------------------------------- 00050 TextFile::~TextFile () 00051 { 00052 Close(); 00053 } 00054 //----------------------------------------------------------------------------- 00055 //============================================================================= 00056 // Operations 00057 //----------------------------------------------------------------------------- 00058 bool TextFile::ChangeMode ( Enum::OpenFile type ) 00059 { 00060 // Check the supported modes 00061 if ( type != Enum::READ && type != Enum::WRITE ) { 00062 std::cerr << "Mode " << type << " for a TextFile object is not supported!"; 00063 return false; 00064 } 00065 00066 // Close the open file 00067 Close(); 00068 00069 // Change the mode and reopen the associated file 00070 m_eOpenFileType = type; 00071 switch ( m_eOpenFileType ) { 00072 case Enum::READ: 00073 m_inStream.open( m_strFileName.c_str(), std::ios::in ); 00074 if ( !m_inStream.is_open() ) { 00075 std::cerr << m_strFileName 00076 << " could not be opened for reading!" 00077 << std::endl; 00078 } 00079 return true; 00080 break; 00081 case Enum::WRITE: 00082 m_outStream.open( m_strFileName.c_str(), std::ios::out ); 00083 if ( !m_outStream.is_open() ) { 00084 std::cerr << m_strFileName 00085 << " could not be opened for writing!" 00086 << std::endl; 00087 } 00088 return true; 00089 break; 00090 case Enum::READ_AND_WRITE: 00091 std::cerr << "Not Implemented Yet!\n"; 00092 assert( false ); 00093 break; 00094 default: 00095 assert( false ); 00096 } 00097 return false; 00098 } 00099 00100 bool TextFile::Read () 00101 { 00102 if ( !m_inStream.is_open() ) return false; 00103 return ReadFile(); 00104 } 00105 //----------------------------------------------------------------------------- 00106 bool TextFile::Read ( std::string newFileName ) 00107 { 00108 // Close the open file 00109 Close(); 00110 00111 // Set the new file 00112 m_strFileName = newFileName; 00113 switch ( m_eOpenFileType ) { 00114 case Enum::READ: 00115 m_inStream.open( m_strFileName.c_str(), std::ios::in ); 00116 if ( !m_inStream.is_open() ) { 00117 std::cerr << m_strFileName 00118 << " could not be opened for reading!" 00119 << std::endl; 00120 } 00121 break; 00122 case Enum::WRITE: 00123 m_outStream.open( m_strFileName.c_str(), std::ios::out ); 00124 if ( !m_outStream.is_open() ) { 00125 std::cerr << m_strFileName 00126 << " could not be opened for writing!" 00127 << std::endl; 00128 } 00129 std::cerr << "The mode is not for reading!" << std::endl; 00130 return false; 00131 break; 00132 case Enum::READ_AND_WRITE: 00133 std::cerr << "Not Implemented Yet!\n"; 00134 assert( false ); 00135 break; 00136 default: 00137 assert( false ); 00138 } 00139 00140 // Read the file content into the buffer 00141 if ( !m_inStream.is_open() ) return false; 00142 return ReadFile(); 00143 } 00144 //----------------------------------------------------------------------------- 00145 bool TextFile::Write () 00146 { 00147 if ( !m_inStream.is_open() ) return false; 00148 return WriteFile(); 00149 } 00150 //----------------------------------------------------------------------------- 00151 bool TextFile::Write ( std::string newFileName ) 00152 { 00153 // Close the open file 00154 Close(); 00155 00156 // Set the new file 00157 m_strFileName = newFileName; 00158 switch ( m_eOpenFileType ) { 00159 case Enum::READ: 00160 m_inStream.open( m_strFileName.c_str(), std::ios::in ); 00161 if ( !m_inStream.is_open() ) { 00162 std::cerr << m_strFileName 00163 << " could not be opened for reading!" 00164 << std::endl; 00165 } 00166 std::cerr << "The mode is not for writing!" << std::endl; 00167 return false; 00168 break; 00169 case Enum::WRITE: 00170 m_outStream.open( m_strFileName.c_str(), std::ios::out ); 00171 if ( !m_outStream.is_open() ) { 00172 std::cerr << m_strFileName 00173 << " could not be opened for writing!" 00174 << std::endl; 00175 } 00176 break; 00177 case Enum::READ_AND_WRITE: 00178 std::cerr << "Not Implemented Yet!\n"; 00179 assert( false ); 00180 break; 00181 default: 00182 assert( false ); 00183 } 00184 00185 // Write from the buffer into the file 00186 if ( !m_inStream.is_open() ) return false; 00187 return WriteFile(); 00188 } 00189 //----------------------------------------------------------------------------- 00190 bool TextFile::Write ( std::vector< std::string > const & content ) 00191 { 00192 if ( !m_outStream.is_open() ) return false; 00193 m_vstrLine = content; 00194 return WriteFile(); 00195 } 00196 //----------------------------------------------------------------------------- 00197 bool TextFile::CopyToFile ( std::string fileName ) 00198 { 00199 if ( !m_outStream.is_open() ) return false; 00200 00201 // Open the write file 00202 std::ofstream out; 00203 out.open( fileName.c_str(), std::ios::out ); 00204 if ( !out.is_open() ) { 00205 std::cerr << "Couldn't open the file (\"" << fileName << "\")!" << std::endl; 00206 return false; 00207 } 00208 00209 // Write the content to the write file 00210 std::vector< std::string >::const_iterator pos = m_vstrLine.begin(); 00211 while ( pos != m_vstrLine.end() ) { 00212 out.write( (*pos).c_str(), static_cast<int>( (*pos).size() ) ); 00213 out << std::endl; 00214 ++pos; 00215 } 00216 00217 // Close the write file 00218 out.close(); 00219 00220 return true; 00221 } 00222 //----------------------------------------------------------------------------- 00223 bool TextFile::CopyFrom ( TextFile const & src ) 00224 { 00225 if ( !m_outStream.is_open() ) return false; 00226 this->m_vstrLine = src.m_vstrLine; 00227 return WriteFile(); 00228 } 00229 //----------------------------------------------------------------------------- 00230 bool TextFile::Close () 00231 { 00232 if ( m_inStream.is_open() ) m_inStream.close(); 00233 if ( m_outStream.is_open() ) m_outStream.close(); 00234 return true; 00235 } 00236 //----------------------------------------------------------------------------- 00237 //============================================================================= 00238 // START: PROTECTED PART 00239 //----------------------------------------------------------------------------- 00240 bool TextFile::ReadFile () 00241 { 00242 m_inStream.seekg( 0 ); // beginning of the file 00243 m_vstrLine.clear(); // clear read lines 00244 char *cStr = new char[ 256 ]; 00245 while ( m_inStream && !m_inStream.eof() ) { 00246 m_inStream.getline( cStr, 256 ); // read a line with 256 chars limit 00247 assert( m_inStream.gcount() <= 256 ); 00248 m_vstrLine.push_back( std::string( cStr ) ); 00249 } 00250 delete [] cStr; 00251 return true; 00252 } 00253 //----------------------------------------------------------------------------- 00254 bool TextFile::WriteFile () 00255 { 00256 std::vector< std::string >::const_iterator pos = m_vstrLine.begin(); 00257 while ( pos != m_vstrLine.end() ) { 00258 m_outStream.write( (*pos).c_str(), static_cast<int>( (*pos).size() ) ); 00259 m_outStream << std::endl; 00260 ++pos; 00261 } 00262 return true; 00263 } 00264 //----------------------------------------------------------------------------- 00265 // END: PROTECTED PART 00266 //============================================================================= 00267 END_NAMESPACE_TAPs 00268 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00269 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----