IT++ Logo

binfile.cpp

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

Generated on Sun Jul 26 08:36:47 2009 for IT++ by Doxygen 1.5.9