00001 00030 #include <itpp/base/binfile.h> 00031 #include <itpp/base/math/misc.h> 00032 #include <cstring> 00033 00034 00035 using std::ofstream; 00036 using std::ifstream; 00037 using std::fstream; 00038 using std::ios; 00039 00040 00041 namespace itpp { 00042 00044 template<typename T1, typename T2> inline 00045 void read_endian(T1& st, T2& data, bool switch_endian = false) 00046 { 00047 int bytes = sizeof(T2); 00048 char *c = reinterpret_cast<char *>(&data); 00049 if (!switch_endian) 00050 st.read(c, bytes); 00051 else 00052 for (int i = bytes - 1; i >= 0; i--) 00053 st.get(c[i]); 00054 } 00055 00057 template<typename T1, typename T2> inline 00058 void write_endian(T1& st, T2 data, bool switch_endian = false) 00059 { 00060 int bytes = sizeof(T2); 00061 char *c = reinterpret_cast<char *>(&data); 00062 if (!switch_endian) 00063 st.write(c, bytes); 00064 else 00065 for (int i = bytes - 1; i >= 0; i--) 00066 st.put(c[i]); 00067 } 00068 00069 // ---------------------------------------------------------------------- 00070 00071 bool exist(const std::string& name) 00072 { 00073 bool file_exists = false; 00074 ifstream file(name.c_str(), ios::in); 00075 if (file.is_open()) { 00076 file_exists = true; 00077 } 00078 file.close(); 00079 return file_exists; 00080 } 00081 00082 // ---------------------------------------------------------------------- 00083 // bfstream_base 00084 // ---------------------------------------------------------------------- 00085 00086 bfstream_base::bfstream_base(endian e): 00087 switch_endianity(false), 00088 native_endianity(check_big_endianness() ? b_endian : l_endian) 00089 { 00090 if (native_endianity != e) 00091 switch_endianity = true; 00092 } 00093 00094 // ---------------------------------------------------------------------- 00095 // bofstream 00096 // ---------------------------------------------------------------------- 00097 00098 bofstream::bofstream(const std::string& name, endian e) : 00099 bfstream_base(e), ofstream(name.c_str(), ios::out | ios::binary) {} 00100 00101 bofstream::bofstream() : bfstream_base(), ofstream() {} 00102 00103 void bofstream::open(const std::string& name, endian e) 00104 { 00105 if (native_endianity != e) 00106 switch_endianity = true; 00107 else 00108 switch_endianity = false; 00109 ofstream::open(name.c_str(), ios::out | ios::binary); 00110 } 00111 00112 bofstream& bofstream::operator<<(char a) 00113 { 00114 put(a); 00115 return *this; 00116 } 00117 00118 bofstream& bofstream::operator<<(unsigned char a) 00119 { 00120 put(static_cast<char>(a)); 00121 return *this; 00122 } 00123 00124 bofstream& bofstream::operator<<(int16_t a) 00125 { 00126 write_endian<bofstream, int16_t>(*this, a, switch_endianity); 00127 return *this; 00128 } 00129 00130 bofstream& bofstream::operator<<(uint16_t a) 00131 { 00132 write_endian<bofstream, uint16_t>(*this, a, switch_endianity); 00133 return *this; 00134 } 00135 00136 bofstream& bofstream::operator<<(int32_t a) 00137 { 00138 write_endian<bofstream, int32_t>(*this, a, switch_endianity); 00139 return *this; 00140 } 00141 00142 bofstream& bofstream::operator<<(uint32_t a) 00143 { 00144 write_endian<bofstream, uint32_t>(*this, a, switch_endianity); 00145 return *this; 00146 } 00147 00148 bofstream& bofstream::operator<<(int64_t a) 00149 { 00150 write_endian<bofstream, int64_t>(*this, a, switch_endianity); 00151 return *this; 00152 } 00153 00154 bofstream& bofstream::operator<<(uint64_t a) 00155 { 00156 write_endian<bofstream, uint64_t>(*this, a, switch_endianity); 00157 return *this; 00158 } 00159 00160 bofstream& bofstream::operator<<(float a) 00161 { 00162 write_endian<bofstream, float>(*this, a, switch_endianity); 00163 return *this; 00164 } 00165 00166 bofstream& bofstream::operator<<(double a) 00167 { 00168 write_endian<bofstream, double>(*this, a, switch_endianity); 00169 return *this; 00170 } 00171 00172 bofstream& bofstream::operator<<(const char *a) 00173 { 00174 write(a, strlen(a)+1); 00175 return *this; 00176 } 00177 00178 bofstream& bofstream::operator<<(const std::string& a) 00179 { 00180 write(a.c_str(), a.size()+1); 00181 return *this; 00182 } 00183 00184 // ---------------------------------------------------------------------- 00185 // bifstream 00186 // ---------------------------------------------------------------------- 00187 00188 bifstream::bifstream(const std::string& name, endian e) : 00189 bfstream_base(e), ifstream(name.c_str(), ios::in | ios::binary ) {} 00190 00191 bifstream::bifstream() : bfstream_base(), ifstream() {} 00192 00193 void bifstream::open(const std::string& name, endian e) 00194 { 00195 if (native_endianity != e) 00196 switch_endianity = true; 00197 else 00198 switch_endianity = false; 00199 ifstream::open(name.c_str(),ios::in | ios::binary ); 00200 } 00201 00202 int bifstream::length() // in bytes 00203 { 00204 std::streampos pos1, len; 00205 pos1 = tellg(); 00206 seekg(0, ios::end); 00207 len = tellg(); 00208 seekg(pos1); 00209 return len; 00210 } 00211 00212 bifstream& bifstream::operator>>(char& a) 00213 { 00214 get(a); 00215 return *this; 00216 } 00217 00218 bifstream& bifstream::operator>>(unsigned char& a) 00219 { 00220 char tmp; 00221 get(tmp); 00222 a = tmp; 00223 return *this; 00224 } 00225 00226 bifstream& bifstream::operator>>(int16_t& a) 00227 { 00228 read_endian<bifstream, int16_t>(*this, a, switch_endianity); 00229 return *this; 00230 } 00231 00232 bifstream& bifstream::operator>>(uint16_t& a) 00233 { 00234 read_endian<bifstream, uint16_t>(*this, a, switch_endianity); 00235 return *this; 00236 } 00237 00238 bifstream& bifstream::operator>>(int32_t& a) 00239 { 00240 read_endian<bifstream, int32_t>(*this, a, switch_endianity); 00241 return *this; 00242 } 00243 00244 bifstream& bifstream::operator>>(uint32_t& a) 00245 { 00246 read_endian<bifstream, uint32_t>(*this, a, switch_endianity); 00247 return *this; 00248 } 00249 00250 bifstream& bifstream::operator>>(int64_t& a) 00251 { 00252 read_endian<bifstream, int64_t>(*this, a, switch_endianity); 00253 return *this; 00254 } 00255 00256 bifstream& bifstream::operator>>(uint64_t& a) 00257 { 00258 read_endian<bifstream, uint64_t>(*this, a, switch_endianity); 00259 return *this; 00260 } 00261 00262 bifstream& bifstream::operator>>(float& a) 00263 { 00264 read_endian<bifstream, float>(*this, a, switch_endianity); 00265 return *this; 00266 } 00267 00268 bifstream& bifstream::operator>>(double& a) 00269 { 00270 read_endian<bifstream, double>(*this, a, switch_endianity); 00271 return *this; 00272 } 00273 00274 bifstream& bifstream::operator>>(char *a) 00275 { 00276 getline(a, '\0'); 00277 return *this; 00278 } 00279 00280 bifstream& bifstream::operator>>(std::string& a) 00281 { 00282 std::getline(*this, a, '\0'); 00283 return *this; 00284 } 00285 00286 // ---------------------------------------------------------------------- 00287 // bfstream 00288 // ---------------------------------------------------------------------- 00289 00290 bfstream::bfstream(const std::string& name, endian e) : 00291 bfstream_base(e), fstream(name.c_str(), ios::in | ios::out | ios::binary) 00292 {} 00293 00294 bfstream::bfstream() : bfstream_base(), fstream() {} 00295 00296 void bfstream::open(const std::string& name, bool trnc, endian e) 00297 { 00298 if (native_endianity != e) 00299 switch_endianity = true; 00300 else 00301 switch_endianity = false; 00302 00303 if (trnc) 00304 fstream::open(name.c_str(), ios::in | ios::out | ios::binary 00305 | ios::trunc); 00306 else 00307 fstream::open(name.c_str(), ios::in | ios::out | ios::binary); 00308 } 00309 00310 void bfstream::open_readonly(const std::string& name, endian e) 00311 { 00312 if (native_endianity != e) 00313 switch_endianity = true; 00314 else 00315 switch_endianity = false; 00316 fstream::open(name.c_str(), ios::in | ios::binary); 00317 } 00318 00319 int bfstream::length() // in bytes 00320 { 00321 std::streampos pos1, len; 00322 pos1 = tellg(); 00323 seekg(0, ios::end); 00324 len = tellg(); 00325 seekg(pos1); 00326 return len; 00327 } 00328 00329 bfstream& bfstream::operator<<(char a) 00330 { 00331 put(a); 00332 return *this; 00333 } 00334 00335 bfstream& bfstream::operator<<(unsigned char a) 00336 { 00337 put(static_cast<char>(a)); 00338 return *this; 00339 } 00340 00341 bfstream& bfstream::operator<<(int16_t a) 00342 { 00343 write_endian<bfstream, int16_t>(*this, a, switch_endianity); 00344 return *this; 00345 } 00346 00347 bfstream& bfstream::operator<<(uint16_t a) 00348 { 00349 write_endian<bfstream, uint16_t>(*this, a, switch_endianity); 00350 return *this; 00351 } 00352 00353 bfstream& bfstream::operator<<(int32_t a) 00354 { 00355 write_endian<bfstream, int32_t>(*this, a, switch_endianity); 00356 return *this; 00357 } 00358 00359 bfstream& bfstream::operator<<(uint32_t a) 00360 { 00361 write_endian<bfstream, uint32_t>(*this, a, switch_endianity); 00362 return *this; 00363 } 00364 00365 bfstream& bfstream::operator<<(int64_t a) 00366 { 00367 write_endian<bfstream, int64_t>(*this, a, switch_endianity); 00368 return *this; 00369 } 00370 00371 bfstream& bfstream::operator<<(uint64_t a) 00372 { 00373 write_endian<bfstream, uint64_t>(*this, a, switch_endianity); 00374 return *this; 00375 } 00376 00377 bfstream& bfstream::operator<<(float a) 00378 { 00379 write_endian<bfstream, float>(*this, a, switch_endianity); 00380 return *this; 00381 } 00382 00383 bfstream& bfstream::operator<<(double a) 00384 { 00385 write_endian<bfstream, double>(*this, a, switch_endianity); 00386 return *this; 00387 } 00388 00389 bfstream& bfstream::operator<<(const char *a) 00390 { 00391 write(a, strlen(a)+1); 00392 return *this; 00393 } 00394 00395 bfstream& bfstream::operator<<(const std::string& a) 00396 { 00397 write(a.c_str(), a.size()+1); 00398 return *this; 00399 } 00400 00401 00402 bfstream& bfstream::operator>>(char& a) 00403 { 00404 get(a); 00405 return *this; 00406 } 00407 00408 bfstream& bfstream::operator>>(unsigned char& a) 00409 { 00410 char tmp; 00411 get(tmp); 00412 a = tmp; 00413 return *this; 00414 } 00415 00416 bfstream& bfstream::operator>>(int16_t& a) 00417 { 00418 read_endian<bfstream, int16_t>(*this, a, switch_endianity); 00419 return *this; 00420 } 00421 00422 bfstream& bfstream::operator>>(uint16_t& a) 00423 { 00424 read_endian<bfstream, uint16_t>(*this, a, switch_endianity); 00425 return *this; 00426 } 00427 00428 bfstream& bfstream::operator>>(int32_t& a) 00429 { 00430 read_endian<bfstream, int32_t>(*this, a, switch_endianity); 00431 return *this; 00432 } 00433 00434 bfstream& bfstream::operator>>(uint32_t& a) 00435 { 00436 read_endian<bfstream, uint32_t>(*this, a, switch_endianity); 00437 return *this; 00438 } 00439 00440 bfstream& bfstream::operator>>(int64_t& a) 00441 { 00442 read_endian<bfstream, int64_t>(*this, a, switch_endianity); 00443 return *this; 00444 } 00445 00446 bfstream& bfstream::operator>>(uint64_t& a) 00447 { 00448 read_endian<bfstream, uint64_t>(*this, a, switch_endianity); 00449 return *this; 00450 } 00451 00452 bfstream& bfstream::operator>>(float& a) 00453 { 00454 read_endian<bfstream, float>(*this, a, switch_endianity); 00455 return *this; 00456 } 00457 00458 bfstream& bfstream::operator>>(double& a) 00459 { 00460 read_endian<bfstream, double>(*this, a, switch_endianity); 00461 return *this; 00462 } 00463 00464 bfstream& bfstream::operator>>(char *a) 00465 { 00466 getline(a, '\0'); 00467 return *this; 00468 } 00469 00470 bfstream& bfstream::operator>>(std::string& a) 00471 { 00472 std::getline(*this, a, '\0'); 00473 return *this; 00474 } 00475 00476 } // namespace itpp
Generated on Sat Apr 19 10:59:21 2008 for IT++ by Doxygen 1.5.5