- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_file.h

Go to the documentation of this file.

    00001 /* ###################################### */
    00002 /* ###     Frost Engine, by Kalith    ### */
    00003 /* ###################################### */
    00004 /*                                        */
    00005 /*                                        */
    00006
    00007 #ifndef FROST_UTILS_FILE_H
    00008 #define FROST_UTILS_FILE_H
    00009
    00010 #include "frost_utils_types.h"
    00011
    00012 #include <fstream>
    00013
    00014 typedef char string_element;
    00015 typedef std::string string_object;
    00016 typedef std::stringstream string_stream;
    00017 typedef std::fstream file_stream;
    00018
    00019 namespace Frost
    00020 {
    00021     /// A tool to input some data
    00022     /** Built upon std::fstream, this class offers
    00023     *   almost the exact same possibilities. The
    00024     *   main difference is that this class can use
    00025     *   Frost's data type.
    00026     */
    00027     class File
    00028     {
    00029     public :
    00030
    00031         enum FileType
    00032         {
    00033             IO, /// Input - Output
    00034             I,  /// Input
    00035             O   /// Output
    00036         };
    00037
    00038         /// Call that constructor to open a file.
    00039         /** \param sName   The file's name
    00040         *   \param mType   How to open this file, see FileType
    00041         *   \param bBinary 'false' if the file is plain text
    00042         */
    00043         File(const s_str& sName, const FileType& mType = IO, const s_bool& bBinary = false);
    00044
    00045         /// Destructor.
    00046         /** \note Automatically closes the file.
    00047         */
    00048         ~File();
    00049
    00050         /// Call that function when you're done reading.
    00051         void        Close();
    00052
    00053         /// Checks if the file has been found and opened.
    00054         /** \return 'true' if the file has been succesfuly opened
    00055         */
    00056         s_bool      IsOpen();
    00057
    00058         /// Checks if the file can still be read.
    00059         /** \return 'true' if the file has been successfuly opened and if it
    00060         *           has not yet reached its end.
    00061         */
    00062         s_bool      IsValid();
    00063
    00064         /// Returns this file's number of character.
    00065         /** \return This file's character count
    00066         */
    00067         s_uint      GetLength();
    00068
    00069         /// Returns this file's name.
    00070         /** \return This file's name
    00071         */
    00072         const s_str GetName() const;
    00073
    00074         /// Returns the next line.
    00075         /** \return The next line
    00076         */
    00077         s_str       GetLine();
    00078
    00079         /// Returns the reading position in the file.
    00080         /** \return The position of the currently read character
    00081         *   in the file
    00082         */
    00083         s_uint      GetReadPos();
    00084
    00085         /// Sets the reading position in the file.
    00086         /** \param uiPos The new position
    00087         */
    00088         void        SetReadPos(const s_uint& uiPos);
    00089
    00090         /// Returns the writing position in the file.
    00091         /** \return The position of the currently written character
    00092         *   in the file
    00093         */
    00094         s_uint      GetWritePos();
    00095
    00096         /// Sets the writing position in the file.
    00097         /** \param uiPos The new position
    00098         */
    00099         void        SetWritePos(const s_uint& uiPos);
    00100
    00101         /// Returns the next character.
    00102         /** \return The next character
    00103         */
    00104         string_element Get();
    00105
    00106         /// Extracts a block of unformated data.
    00107         /** \param[out] sBuffer A buffer string object
    00108         *   \param      uiSize  The number of character to extract
    00109         */
    00110         void        Read(s_str& sBuffer, const s_uint& uiSize);
    00111
    00112         /// Extracts a block of unformated data.
    00113         /** \param[out] sBuffer A buffer string object
    00114         *   \param      uiSize  The number of character to extract
    00115         */
    00116         void        Read(string_element* sBuffer, const s_uint& uiSize);
    00117
    00118         /// Extracts a block of unformated data.
    00119         /** \param[out] pBuffer A pointer to the object/array to fill
    00120         *   \param      uiSize  The number of character to extract
    00121         */
    00122         void        Read(void* pBuffer, const s_uint& uiSize);
    00123
    00124         /// Fills an object.
    00125         /** \param[out] mObject The object to fill
    00126         *   \note The object provided should only contain POD types and no pointers.
    00127         */
    00128         template<class T>
    00129         void        Read(T& mObject)
    00130         {
    00131             Read(&mObject, sizeof(T));
    00132         }
    00133
    00134         /// Writes down a char array into the file.
    00135         /** \param sBuffer The char array
    00136         *   \param uiSize  The number of char to write to the file
    00137         *   \note This function changes the writing position
    00138         */
    00139         void        Write(const string_element* sBuffer, const s_uint& uiSize);
    00140
    00141         /// Writes down some data into the file.
    00142         /** \param pBuffer The object/array to write
    00143         *   \param uiSize  The object's size
    00144         *   \note This function changes the writing position
    00145         */
    00146         void        Write(const void* pBuffer, const s_uint& uiSize);
    00147
    00148         /// Writes a table into the file.
    00149         /** \param pTable The table to write
    00150         *   \note This function changes the writing position
    00151         */
    00152         template<class T>
    00153         void        Write(const T* pTable, const s_uint& uiNum)
    00154         {
    00155             Write((void*)pTable, sizeof(T)*uiNum);
    00156         }
    00157
    00158         /// Writes an object into the file.
    00159         /** \param mObject The object to write
    00160         *   \note This function changes the writing position
    00161         */
    00162         template<class T>
    00163         void        Write(const T& mObject)
    00164         {
    00165             Write((void*)&mObject, sizeof(T));
    00166         }
    00167
    00168         /// Writes down a single char into the file.
    00169         /** \param cChar The char to write down
    00170         *   \note This function changes the writing position
    00171         */
    00172         void        Write(const string_element& cChar);
    00173
    00174         /// Writes the content of the provided string into the file.
    00175         /** \param sLine The string to write
    00176         */
    00177         void        Write(const s_str& sLine);
    00178
    00179         /// Writes the content of the provided string and go to the next line.
    00180         /** \param sLine The string to write
    00181         *   \note This function automatically adds the line jump.
    00182         */
    00183         void        WriteLine(const s_str& sLine);
    00184
    00185         /// Forces writing everything to the file.
    00186         void        Flush();
    00187
    00188         /// Sets the tabulation string.
    00189         /** \param sTabStr The tabulation string
    00190         *   \note Default is 4 spaces : "    "
    00191         */
    00192         void        SetTabCharacter(const s_str& sTabStr);
    00193
    00194         /// Returns the tabulation string.
    00195         /** \return The tabulation string
    00196         *   \note Default is 4 spaces : "    "
    00197         */
    00198         const s_str& GetTabCharacter() const;
    00199
    00200         /// Adds a tabulation string.
    00201         /** \note Calling this function increments the tab counter.<br>
    00202         *         When WriteLine() is called, the tabulation string is written
    00203         *         in front of each line, as many time as you've called this
    00204         *         function.
    00205         */
    00206         void        AddTab();
    00207
    00208         /// Removes a tabulation string.
    00209         /** \note Calling this function decrements the tab counter.<br>
    00210         *         See AddTab() for more infos.
    00211         */
    00212         void        RemoveTab();
    00213
    00214         /// Removes all tabulation strings.
    00215         /** \note Calling this function sets the tab counter to zero.<br>
    00216         *         See AddTab() for more infos.
    00217         */
    00218         void        RemoveAllTabs();
    00219
    00220         /// Checks if a file exists.
    00221         /** \param sFileName The name of the file
    00222         *   \return 'true' if the file exists, 'false' otherwise
    00223         */
    00224         static s_bool Exists(const s_str& sFileName);
    00225
    00226         static const s_str CLASS_NAME;
    00227
    00228     private :
    00229
    00230         /// The file stream object
    00231         file_stream mFile_;
    00232         /// The name of this file
    00233         s_str       sName_;
    00234         /// The tabbing string
    00235         s_str       sTabStr_;
    00236         /// The total tabbing
    00237         s_uint      uiTabCount_;
    00238     };
    00239 }
    00240
    00241 #endif