BinaryFile.h
Go to the documentation of this file.
00001 /*      
00002  * File: BinaryFile.h
00003  * Project: DUtils library
00004  * Author: Dorian Galvez-Lopez
00005  * Date: April 2010
00006  * Description: reads and writes binary files in network byte order.
00007  *    Manages endianness and data size automatically.
00008  *
00009  * 
00010  * This program is free software: you can redistribute it and/or modify
00011  * it under the terms of the GNU Lesser General Public License as published by
00012  * the Free Software Foundation, either version 3 of the License, or
00013  * any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public License
00021  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022  *
00023  */
00024 
00025 #pragma once
00026 #ifndef __D_BINARY_FILE__
00027 #define __D_BINARY_FILE__
00028 
00029 #include "DException.h"
00030 #include "FileModes.h"
00031 #include <fstream>
00032 using namespace std;
00033 
00034 namespace DUtils {
00035 
00036 class BinaryFile
00037 {
00038 public:
00039         
00040         /* Creates a binary file with no file
00041          */
00042         BinaryFile(void);
00043 
00044         /* Closes any opened file
00045         */
00046         ~BinaryFile(void);
00047 
00048         /* Creates a binary file by opening a file
00049          * @param filename
00050          * @param mode: READ or WRITE
00051          * @throws DException if cannot open the file
00052          */
00053         BinaryFile(const char *filename, const FILE_MODES mode);
00054         BinaryFile(const string &filename, const FILE_MODES mode);
00055 
00056         /* Opens a file for reading. It closes any other opened file
00057          * @param filename
00058          * @throws DException if cannot open the file
00059          */
00060         void OpenForReading(const char *filename);
00061         inline void OpenForReading(const string &filename)
00062         {
00063                 OpenForReading(filename.c_str());
00064         }
00065 
00066         /* Opens a file for writing. It closes any other opened file
00067          * @param filename
00068          * @throws DException if cannot create the file
00069          */
00070         void OpenForWriting(const char *filename);
00071         inline void OpenForWriting(const string &filename)
00072         {
00073                 OpenForWriting(filename.c_str());
00074         }
00075 
00076         /* Opens a file for writing at the end. It closes any other opened file
00077          * @param filename
00078          * @throws DException if cannot open the file
00079          */
00080         void OpenForAppending(const char *filename);
00081         inline void OpenForAppending(const string &filename)
00082         {
00083                 OpenForAppending(filename.c_str());
00084         }
00085 
00086         /* Says whether the end of the file has been reached. Requires to
00087          * read the end of the file to return true
00088          * @return true iif the end of the file has been already read
00089          * @throws DException if wrong access mode
00090          */
00091         inline bool Eof();
00092 
00093         /* Closes any opened file. It is not necessary to call this function
00094          * explicitly
00095          */
00096         void Close();
00097 
00102         inline void DiscardNextByte(){
00103                 DiscardBytes(1);
00104         }
00105 
00111         void DiscardBytes(int count);
00112 
00117         unsigned int BytesRead();
00118 
00119         /* Writes a byte char
00120          * @throws DException if wrong access mode
00121          */
00122         BinaryFile& operator<< (char v);
00123 
00124         /* Writes a 4 byte integer value
00125          * @throws DException if wrong access mode
00126          */
00127         BinaryFile& operator<< (int v);
00128 
00129         /* Writes a 4 byte float value
00130          * @throws DException if wrong access mode
00131          */
00132         BinaryFile& operator<< (float v);
00133 
00134         /* Writes a 8 byte float value
00135          * @throws DException if wrong access mode
00136          */
00137         BinaryFile& operator<< (double v);
00138 
00139         /* Reads a byte char
00140          * @throws DException if wrong access mode
00141          */
00142         BinaryFile& operator>>(char &v);
00143 
00144         /* Reads a 4 byte integer value
00145          * @throws DException if wrong access mode
00146          */
00147         BinaryFile& operator>>(int &v);
00148 
00149         /* Reads a 4 byte float value
00150          * @throws DException if wrong access mode
00151          */
00152         BinaryFile& operator>>(float &v);
00153 
00154         /* Reads a 8 byte float value
00155          * @throws DException if wrong access mode
00156          */
00157         BinaryFile& operator>>(double &v);
00158 
00159 protected:
00160 
00167         void Init(const char *filename, const FILE_MODES mode);
00168 
00172         void setEndianness();
00173 
00179         void hton_f(float v, char buf[8]) const;
00180 
00186         void hton_d(double d, char buf[8]) const;
00187 
00193         float ntoh_f(char buf[8]) const;
00194 
00200         double ntoh_d(char buf[8]) const;
00201 
00206         inline bool isLittleEndian() const
00207         {
00208                 return (m_is_little_endian == 1 ? true : false);
00209         }
00210 
00211 protected:
00212         FILE_MODES m_mode;              // opening mode
00213         fstream m_f;                    // fstream
00214         char m_aux[8];  // auxiliar buffer
00215 
00216         // current machine endianness
00217         int m_is_little_endian; // 1: little endian, 0: big endian, -1: not set
00218 
00219 };
00220 
00221 }
00222 
00223 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:30:47