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

Generated on Sun Sep 14 18:52:25 2008 for IT++ by Doxygen 1.5.6