![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsReadTextFile.hpp 00003 ******************************************************************************/ 00021 /****************************************************************************** 00022 SUKITTI PUNAK (02/17/2006) 00023 UPDATE (08/26/2009) 00024 ******************************************************************************/ 00025 #ifndef TAPs_TEXT_FILE_HPP 00026 #define TAPs_TEXT_FILE_HPP 00027 00028 #include "../Core/TAPsStdLib.hpp" 00029 #include "../Core/TAPsEnumList.hpp" 00030 00031 BEGIN_NAMESPACE_TAPs 00032 //============================================================================= 00033 class TextFile { 00034 //============================================================================= 00035 protected: 00036 std::vector< std::string > m_vstrLine; // vector of strings 00037 std::string m_strFileName; // file name 00038 Enum::OpenFile m_eOpenFileType; // type of open file 00039 std::ifstream m_inStream; // ifstream for input file 00040 std::ofstream m_outStream; // ofstream for output file 00041 //============================================================================= 00042 public: 00043 //------------------------------------------------------------------------- 00044 // Output Operator <<i 00045 friend std::ostream & operator<< ( std::ostream & output, 00046 TextFile const & obj ) 00047 { 00048 //output << typeid(*this).name() << "( "; 00049 output << "TextFile:"; 00050 switch ( obj.m_eOpenFileType ) { 00051 case Enum::READ: 00052 output << "(read)"; 00053 break; 00054 case Enum::WRITE: 00055 output << "(write)"; 00056 break; 00057 case Enum::READ_AND_WRITE: 00058 output << "(read & write)"; 00059 break; 00060 } 00061 output << " \"" << obj.m_strFileName << "\"\n"; 00062 for ( int i = 0; i < static_cast<int>(obj.m_vstrLine.size()); ++i ) { 00063 output << obj.m_vstrLine[i] << "\n"; 00064 } 00065 return output; 00066 } 00067 //------------------------------------------------------------------------- 00071 TextFile (); 00072 00074 TextFile ( 00075 std::string fileName, 00076 Enum::OpenFile type 00077 ); 00078 00080 ~TextFile (); 00081 //------------------------------------------------------------------------- 00083 std::string GetFileName () const { return m_strFileName; } 00084 00086 int GetNumOfLines () const { return static_cast<int>( m_vstrLine.size() ); } 00087 00089 std::string CopyLine ( int i ) 00090 { 00091 assert( 0 <= i && i < static_cast<int>( m_vstrLine.size() ) ); 00092 return m_vstrLine[i]; 00093 } 00094 00096 std::string const & GetLine ( int i ) const 00097 { 00098 assert( 0 <= i && i < static_cast<int>( m_vstrLine.size() ) ); 00099 return m_vstrLine[i]; 00100 } 00102 std::string & GetLine ( int i ) 00103 { 00104 assert( 0 <= i && i < static_cast<int>( m_vstrLine.size() ) ); 00105 return m_vstrLine[i]; 00106 } 00107 00108 //------------------------------------------------------------------------- 00109 // Operations 00110 00112 std::vector< std::string > const & GetContents() const { return m_vstrLine; } 00114 std::vector< std::string > & GetContents() { return m_vstrLine; } 00115 00117 bool ChangeMode ( 00118 Enum::OpenFile type 00119 ); 00120 00122 bool Read (); 00123 00125 bool Read ( std::string newFileName ); 00126 00128 bool Read ( char fileName[] ) { Read( std::string( fileName ) ); } 00129 00131 bool Write (); 00132 00134 bool Write ( std::string newFileName ); 00135 00137 bool Write ( char fileName[] ) { Write( std::string( fileName ) ); } 00138 00140 bool Write ( std::vector< std::string > const & content ); 00141 00143 bool CopyToFile ( std::string fileName ); 00144 00146 bool CopyToFile ( char fileName[] ) { CopyToFile( std::string( fileName ) ); } 00147 00149 bool CopyFrom ( TextFile const & src ); 00150 00152 bool Close (); 00153 00154 protected: 00155 00157 bool ReadFile (); 00158 00160 bool WriteFile (); 00161 //------------------------------------------------------------------------- 00162 }; // END CLASS TextFile 00163 //============================================================================= 00164 END_NAMESPACE_TAPs 00165 //----------------------------------------------------------------------------- 00166 // Include definition if TAPs_USE_EXPORT is not defined 00167 #if !defined( TAPs_USE_EXPORT ) 00168 #include "TAPsTextFile.cpp" 00169 #endif 00170 //----------------------------------------------------------------------------- 00171 #endif 00172 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00173 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----