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 00037 class BinaryFile 00038 { 00039 public: 00040 00044 BinaryFile(void); 00045 00049 ~BinaryFile(void); 00050 00057 BinaryFile(const char *filename, const FILE_MODES mode); 00058 00065 BinaryFile(const string &filename, const FILE_MODES mode); 00066 00072 void OpenForReading(const char *filename); 00073 00079 inline void OpenForReading(const string &filename) 00080 { 00081 OpenForReading(filename.c_str()); 00082 } 00083 00089 void OpenForWriting(const char *filename); 00090 00096 inline void OpenForWriting(const string &filename) 00097 { 00098 OpenForWriting(filename.c_str()); 00099 } 00100 00106 void OpenForAppending(const char *filename); 00107 00113 inline void OpenForAppending(const string &filename) 00114 { 00115 OpenForAppending(filename.c_str()); 00116 } 00117 00124 inline bool Eof(); 00125 00130 void Close(); 00131 00136 inline void DiscardNextByte(){ 00137 DiscardBytes(1); 00138 } 00139 00145 void DiscardBytes(int count); 00146 00151 unsigned int BytesRead(); 00152 00157 BinaryFile& operator<< (char v); 00158 00163 BinaryFile& operator<< (int v); 00164 00169 BinaryFile& operator<< (float v); 00170 00175 BinaryFile& operator<< (double v); 00176 00181 BinaryFile& operator>>(char &v); 00182 00187 BinaryFile& operator>>(int &v); 00188 00193 BinaryFile& operator>>(float &v); 00194 00199 BinaryFile& operator>>(double &v); 00200 00201 protected: 00202 00209 void Init(const char *filename, const FILE_MODES mode); 00210 00214 void setEndianness(); 00215 00221 void hton_f(float v, char buf[8]) const; 00222 00228 void hton_d(double d, char buf[8]) const; 00229 00235 float ntoh_f(char buf[8]) const; 00236 00242 double ntoh_d(char buf[8]) const; 00243 00248 inline bool isLittleEndian() const 00249 { 00250 return (m_is_little_endian == 1 ? true : false); 00251 } 00252 00253 protected: 00255 FILE_MODES m_mode; // opening mode 00257 fstream m_f; // fstream 00259 char m_aux[8]; // auxiliar buffer 00260 00262 int m_is_little_endian; // 1: little endian, 0: big endian, -1: not set 00263 00264 }; 00265 00266 } 00267 00268 #endif