- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/src/frost_utils_file.cpp

Go to the documentation of this file.

    00001 /* ###################################### */
    00002 /* ###     Frost Engine, by Kalith    ### */
    00003 /* ###################################### */
    00004 /*                                        */
    00005
    00006 #include "frost_utils_file.h"
    00007
    00008 using namespace std;
    00009
    00010 namespace Frost
    00011 {
    00012     const s_str File::CLASS_NAME = "File";
    00013
    00014     File::File( const s_str& sName, const FileType& mType, const s_bool& bBinary )
    00015     {
    00016         sName_ = sName;
    00017         sTabStr_ = "    ";
    00018
    00019         switch (mType)
    00020         {
    00021             case IO :
    00022             {
    00023                 if (bBinary)
    00024                     mFile_.open(sName.c_str(), ios_base::in | ios_base::out | ios::binary);
    00025                 else
    00026                     mFile_.open(sName.c_str(), ios_base::in | ios_base::out);
    00027                 break;
    00028             }
    00029             case I :
    00030             {
    00031                 if (bBinary)
    00032                     mFile_.open(sName.c_str(), ios::in | ios::binary);
    00033                 else
    00034                     mFile_.open(sName.c_str(), ios::in);
    00035                 break;
    00036             }
    00037             case O :
    00038             {
    00039                 if (bBinary)
    00040                     mFile_.open(sName.c_str(), ios::out | ios::binary);
    00041                 else
    00042                     mFile_.open(sName.c_str(), ios::out);
    00043                 break;
    00044             }
    00045         }
    00046     }
    00047
    00048     File::~File()
    00049     {
    00050         Close();
    00051     }
    00052
    00053     void File::Close()
    00054     {
    00055         mFile_.close();
    00056     }
    00057
    00058     s_bool File::IsOpen()
    00059     {
    00060         return mFile_.is_open();
    00061     }
    00062
    00063     s_bool File::IsValid()
    00064     {
    00065         return (mFile_.is_open() && !mFile_.eof());
    00066     }
    00067
    00068     s_uint File::GetLength()
    00069     {
    00070         uint uiOldPos = mFile_.tellg();
    00071
    00072         mFile_.seekg(0, ios::end);
    00073         s_uint uiLength = (uint)mFile_.tellg();
    00074         mFile_.seekg(uiOldPos);
    00075
    00076         return uiLength;
    00077     }
    00078
    00079     const s_str File::GetName() const
    00080     {
    00081         return sName_;
    00082     }
    00083
    00084     s_str File::GetLine()
    00085     {
    00086         s_str sTemp;
    00087         getline(mFile_, sTemp.Get());
    00088         #ifdef FROST_LINUX
    00089             sTemp.Replace("\r", "");
    00090         #endif
    00091         return sTemp;
    00092     }
    00093
    00094     s_uint File::GetReadPos()
    00095     {
    00096         return (uint)mFile_.tellg();
    00097     }
    00098
    00099     void File::SetReadPos( const s_uint& uiPos )
    00100     {
    00101         mFile_.seekg(uiPos.Get());
    00102     }
    00103
    00104     s_uint File::GetWritePos()
    00105     {
    00106         return (uint)mFile_.tellp();
    00107     }
    00108
    00109     void File::SetWritePos( const s_uint& uiPos )
    00110     {
    00111         mFile_.seekp(uiPos.Get());
    00112     }
    00113
    00114     string_element File::Get()
    00115     {
    00116         string_element cTemp;
    00117         mFile_.get(cTemp);
    00118         return cTemp;
    00119     }
    00120
    00121     void File::Read( s_str& sBuffer, const s_uint& uiSize )
    00122     {
    00123         string_element* sTemp = new string_element[uiSize.Get()];
    00124         mFile_.read(sTemp, uiSize.Get());
    00125         sBuffer = sTemp;
    00126         delete sTemp;
    00127     }
    00128
    00129     void File::Read( string_element* sBuffer, const s_uint& uiSize )
    00130     {
    00131         mFile_.read(sBuffer, uiSize.Get());
    00132     }
    00133
    00134     void File::Read( void* pBuffer, const s_uint& uiSize )
    00135     {
    00136         mFile_.read((char*)pBuffer, uiSize.Get());
    00137     }
    00138
    00139     void File::Write( const string_element* sBuffer, const s_uint& uiSize )
    00140     {
    00141         mFile_.write(sBuffer, uiSize.Get());
    00142     }
    00143
    00144     void File::Write( const void* sBuffer, const s_uint& uiSize )
    00145     {
    00146         mFile_.write((const char*)sBuffer, uiSize.Get());
    00147     }
    00148
    00149     void File::Write( const string_element& cChar )
    00150     {
    00151         mFile_.put(cChar);
    00152     }
    00153
    00154     void File::Write( const s_str& sLine )
    00155     {
    00156         mFile_.write(sLine.c_str(), sLine.GetSize().Get());
    00157     }
    00158
    00159     void File::WriteLine( const s_str& sLine )
    00160     {
    00161         if (uiTabCount_ == 0)
    00162         {
    00163             mFile_.write(sLine.c_str(), sLine.GetSize().Get());
    00164             mFile_.put('\n');
    00165         }
    00166         else
    00167         {
    00168             s_str sTab = s_str(sTabStr_, uiTabCount_);
    00169             s_str sNewLine = sTab+sLine;
    00170             sNewLine.Replace("\n", "\n"+sTab);
    00171
    00172             mFile_.write(sNewLine.c_str(), sNewLine.GetSize().Get());
    00173             mFile_.put('\n');
    00174         }
    00175     }
    00176
    00177     void File::Flush()
    00178     {
    00179         mFile_.flush();
    00180     }
    00181
    00182     s_bool File::Exists( const s_str& sFileName )
    00183     {
    00184         if (sFileName.IsEmpty())
    00185             return false;
    00186
    00187         fstream mFile;
    00188         mFile.open(sFileName.c_str(), ios::in);
    00189         return mFile.is_open();
    00190     }
    00191
    00192     void File::SetTabCharacter(const s_str& sTabStr)
    00193     {
    00194         sTabStr_ = sTabStr;
    00195     }
    00196
    00197     const s_str& File::GetTabCharacter() const
    00198     {
    00199         return sTabStr_;
    00200     }
    00201
    00202     void File::AddTab()
    00203     {
    00204         ++uiTabCount_;
    00205     }
    00206
    00207     void File::RemoveTab()
    00208     {
    00209         if (uiTabCount_ != 0)
    00210             --uiTabCount_;
    00211     }
    00212
    00213     void File::RemoveAllTabs()
    00214     {
    00215         uiTabCount_ = 0;
    00216     }
    00217 }