IT++ Logo Newcom Logo

binfile.cpp

Go to the documentation of this file.
00001 
00032 #ifndef _MSC_VER
00033 #  include <itpp/config.h>
00034 #else
00035 #  include <itpp/config_msvc.h>
00036 #endif
00037 
00038 #ifdef HAVE_SYS_STAT_H
00039 #  include <sys/stat.h>
00040 #endif
00041 
00042 #include <itpp/base/binfile.h>
00043 #include <itpp/base/machdep.h>
00044 
00045 
00046 using std::string;
00047 using std::ofstream;
00048 using std::ifstream;
00049 using std::fstream;
00050 using std::ios;
00051 
00052 namespace itpp { 
00053 
00054   bool exist(const std::string &name)
00055   {
00056 #ifdef HAVE_SYS_STAT_H
00057     struct stat st;
00058     return stat(name.c_str(), &st) == 0;
00059 #else
00060     // since <sys/stat.h> is not available, assume that file exists
00061     return true;
00062 #endif
00063   }
00064 
00065   bfstream_base::bfstream_base(endian e)
00066   {
00067     endianity = e;
00068     native_endianity = (big_endian((short)0x1234) == (short)0x1234)
00069       ? b_endian : l_endian;
00070   }
00071 
00072   //-----------------------------------------------------------------------
00073   //    bofstream
00074   //-----------------------------------------------------------------------
00075 
00076   bofstream::bofstream(const std::string &name, endian e)
00077     : bfstream_base(e), ofstream(name.c_str(), ios::out | ios::binary)
00078   {
00079   }
00080 
00081   bofstream::bofstream()
00082     : bfstream_base(), ofstream()
00083   {
00084   }
00085 
00086   void bofstream::open(const std::string &name, endian e)
00087   {
00088     endianity = e;
00089     ofstream::open(name.c_str(), ios::out | ios::binary);
00090   }
00091 
00092   bofstream& bofstream::operator <<(char a)
00093   {
00094     put(a);
00095     return *this;
00096   }
00097 
00098   bofstream& bofstream::operator <<(const class bin &a)
00099   {
00100     put(a.value());
00101     return *this;
00102   }
00103 
00104   bofstream& bofstream::operator <<(short a)
00105   {
00106     char *c=reinterpret_cast<char *>(&a);
00107 
00108     if (endianity == native_endianity) {
00109       put(c[0]);
00110       put(c[1]);
00111     } else {
00112       put(c[1]);
00113       put(c[0]);
00114     }
00115     return *this;
00116   }
00117 
00118   bofstream& bofstream::operator <<(unsigned short a)
00119   {
00120     char *c=reinterpret_cast<char *>(&a);
00121 
00122     if (endianity == native_endianity)
00123       write(c, 2);
00124     else {
00125       put(c[1]);
00126       put(c[0]);
00127     }
00128     return *this;
00129   }
00130 
00131   bofstream& bofstream::operator <<(float a)
00132   {
00133     char *c=reinterpret_cast<char *>(&a);
00134 
00135     if (endianity == native_endianity)
00136       write(c, 4);
00137     else {
00138       put(c[3]);
00139       put(c[2]);
00140       put(c[1]);
00141       put(c[0]);
00142     }
00143     return *this;
00144   }
00145 
00146   bofstream& bofstream::operator <<(double a)
00147   {
00148     char *c=reinterpret_cast<char *>(&a);
00149 
00150     if (endianity == native_endianity)
00151       write(c, 8);
00152     else {
00153       put(c[7]);
00154       put(c[6]);
00155       put(c[5]);
00156       put(c[4]);
00157       put(c[3]);
00158       put(c[2]);
00159       put(c[1]);
00160       put(c[0]);
00161     }
00162     return *this;
00163   }
00164 
00165 //   bofstream& bofstream::operator <<(long double a)
00166 //   {
00167 //     char *c=reinterpret_cast<char *>(&a);
00168 
00169 //     if (endianity == native_endianity)
00170 //       write(c, 10);
00171 //     else {
00172 //       put(c[9]);
00173 //       put(c[8]);
00174 //       put(c[7]);
00175 //       put(c[6]);
00176 //       put(c[5]);
00177 //       put(c[4]);
00178 //       put(c[3]);
00179 //       put(c[2]);
00180 //       put(c[1]);
00181 //       put(c[0]);
00182 //     }
00183 //     return *this;
00184 //   }
00185 
00186   bofstream& bofstream::operator <<(int a)
00187   {
00188     char *c=reinterpret_cast<char *>(&a);
00189 
00190     if (endianity == native_endianity)
00191       write(c, 4);
00192     else {
00193       put(c[3]);
00194       put(c[2]);
00195       put(c[1]);
00196       put(c[0]);
00197     }
00198     return *this;
00199   }
00200 
00201   bofstream& bofstream::operator <<(unsigned int a)
00202   {
00203     char *c=reinterpret_cast<char *>(&a);
00204 
00205     if (endianity == native_endianity)
00206       write(c, 4);
00207     else {
00208       put(c[3]);
00209       put(c[2]);
00210       put(c[1]);
00211       put(c[0]);
00212     }
00213     return *this;
00214   }
00215 
00216   bofstream& bofstream::operator <<(long int a)
00217   {
00218     char *c=reinterpret_cast<char *>(&a);
00219 
00220     if (endianity == native_endianity)
00221       write(c, 4);
00222     else {
00223       put(c[3]);
00224       put(c[2]);
00225       put(c[1]);
00226       put(c[0]);
00227     }
00228     return *this;
00229   }
00230 
00231   bofstream& bofstream::operator <<(unsigned long int a)
00232   {
00233     char *c=reinterpret_cast<char *>(&a);
00234 
00235     if (endianity == native_endianity)
00236       write(c, 4);
00237     else {
00238       put(c[3]);
00239       put(c[2]);
00240       put(c[1]);
00241       put(c[0]);
00242     }
00243     return *this;
00244   }
00245 
00246   bofstream& bofstream::operator<<(const char *a)
00247   {
00248     write(a, strlen(a)+1);
00249     return *this;
00250   }
00251 
00252   bofstream& bofstream::operator<<(const std::string &a)
00253   {
00254     write(a.c_str(), a.size()+1);
00255     return *this;
00256   }
00257 
00258   //-----------------------------------------------------------------------
00259   //    bifstream
00260   //-----------------------------------------------------------------------
00261 
00262   bifstream::bifstream(const std::string &name, endian e)
00263     : bfstream_base(e), ifstream(name.c_str(), ios::in | ios::binary )
00264   {
00265   }
00266 
00267   bifstream::bifstream()
00268     : bfstream_base(), ifstream()
00269   {
00270   }
00271 
00272   void bifstream::open(const std::string &name, endian e)
00273   {
00274     endianity = e;
00275     ifstream::open(name.c_str(),ios::in | ios::binary );
00276   }
00277 
00278   long bifstream::length()  //in bytes
00279   {
00280     std::streampos pos1,len;
00281     pos1=tellg();
00282     seekg(0,ios::end);
00283     len=tellg();
00284     seekg(pos1);
00285     return len;
00286   }
00287 
00288   bifstream& bifstream::operator >>(char &a)
00289   {
00290     get(a);
00291     return *this;
00292   }
00293 
00294   bifstream& bifstream::operator >>(class bin &a)
00295   {
00296     char temp;
00297     get(temp);
00298     a=temp;
00299     return *this;
00300   }
00301 
00302   bifstream& bifstream::operator >>(int &a)
00303   {
00304     char *c=reinterpret_cast<char *>(&a);
00305 
00306     if (endianity == native_endianity)
00307       read(c, 4);
00308     else {
00309       get(c[3]);
00310       get(c[2]);
00311       get(c[1]);
00312       get(c[0]);
00313     }
00314     return *this;
00315   }
00316 
00317   bifstream& bifstream::operator >>(unsigned int &a)
00318   {
00319     char *c=reinterpret_cast<char *>(&a);
00320 
00321     if (endianity == native_endianity)
00322       read(c, 4);
00323     else {
00324       get(c[3]);
00325       get(c[2]);
00326       get(c[1]);
00327       get(c[0]);
00328     }
00329     return *this;
00330   }
00331 
00332   bifstream& bifstream::operator >>(short int &a)
00333   {
00334     char *c=reinterpret_cast<char *>(&a);
00335 
00336     if (endianity == native_endianity)
00337       read(c, 2);
00338     else {
00339       get(c[1]);
00340       get(c[0]);
00341     }
00342     return *this;
00343   }
00344 
00345   bifstream& bifstream::operator >>(unsigned short int &a)
00346   {
00347     char *c=reinterpret_cast<char *>(&a);
00348 
00349     if (endianity == native_endianity)
00350       read(c, 2);
00351     else {
00352       get(c[1]);
00353       get(c[0]);
00354     }
00355     return *this;
00356   }
00357 
00358   bifstream& bifstream::operator >>(float &a)
00359   {
00360     char *c=reinterpret_cast<char *>(&a);
00361 
00362         if (endianity == native_endianity) {
00363       read(c, 4);
00364         } else {
00365       get(c[3]);
00366       get(c[2]);
00367       get(c[1]);
00368       get(c[0]);
00369     }
00370     return *this;
00371   }
00372 
00373   bifstream& bifstream::operator >>(double &a)
00374   {
00375     char *c=reinterpret_cast<char *>(&a);
00376 
00377     if (endianity == native_endianity)
00378       read(c, 8);
00379     else {
00380       get(c[7]);
00381       get(c[6]);
00382       get(c[5]);
00383       get(c[4]);
00384       get(c[3]);
00385       get(c[2]);
00386       get(c[1]);
00387       get(c[0]);
00388     }
00389     return *this;
00390   }
00391 
00392 //   bifstream& bifstream::operator >>(long double &a)
00393 //   {
00394 //     char *c=reinterpret_cast<char *>(&a);
00395 
00396 //     if (endianity == native_endianity)
00397 //       read(c, 10);
00398 //     else {
00399 //       get(c[9]);
00400 //       get(c[8]);
00401 //       get(c[7]);
00402 //       get(c[6]);
00403 //       get(c[5]);
00404 //       get(c[4]);
00405 //       get(c[3]);
00406 //       get(c[2]);
00407 //       get(c[1]);
00408 //       get(c[0]);
00409 //     }
00410 //     return *this;
00411 //   }
00412 
00413   bifstream& bifstream::operator >>(long int &a)
00414   {
00415     char *c=reinterpret_cast<char *>(&a);
00416 
00417     if (endianity == native_endianity)
00418       read(c, 4);
00419     else {
00420       get(c[3]);
00421       get(c[2]);
00422       get(c[1]);
00423       get(c[0]);
00424     }
00425     return *this;
00426   }
00427 
00428   bifstream& bifstream::operator >>(unsigned long int &a)
00429   {
00430     char *c=reinterpret_cast<char *>(&a);
00431 
00432     if (endianity == native_endianity)
00433       read(c, 4);
00434     else {
00435       get(c[3]);
00436       get(c[2]);
00437       get(c[1]);
00438       get(c[0]);
00439     }
00440     return *this;
00441   }
00442 
00443   bifstream& bifstream::operator>>(char *a)
00444   {
00445     getline(a, '\0');
00446     return *this;
00447   }
00448 
00449   bifstream& bifstream::operator>>(std::string &a)
00450   {
00451     std::getline(*this, a, '\0');
00452     return *this;
00453   }
00454 
00455   //-----------------------------------------------------------------------
00456   //    bfstream
00457   //-----------------------------------------------------------------------
00458 
00459   bfstream::bfstream(const std::string &name, endian e)
00460     : bfstream_base(e), fstream(name.c_str(), ios::in | ios::out | ios::binary)
00461   {
00462   }
00463 
00464   bfstream::bfstream()
00465     : bfstream_base(), fstream()
00466   {
00467   }
00468 
00469   void bfstream::open(const std::string &name, bool trnc, endian e) //CC fix trunc -> trnc
00470   {
00471     endianity = e;
00472 
00473     if (trnc)
00474       fstream::open(name.c_str(), ios::in | ios::out | ios::binary | ios::trunc);
00475     else
00476       fstream::open(name.c_str(), ios::in | ios::out | ios::binary);
00477 
00478 
00479   }
00480 
00481   void bfstream::open_readonly(const std::string &name, endian e)
00482   {
00483     endianity = e;
00484     fstream::open(name.c_str(), ios::in | ios::binary);
00485   }
00486 
00487   long bfstream::length()  //in bytes
00488   {
00489     std::streampos pos1,len;
00490     pos1=tellg();
00491     seekg(0,ios::end);
00492     len=tellg();
00493     seekg(pos1);
00494     return len;
00495   }
00496 
00497   bfstream& bfstream::operator <<(char a)
00498   {
00499     put(a);
00500     return *this;
00501   }
00502 
00503   bfstream& bfstream::operator <<(const class bin &a)
00504   {
00505     put(a.value());
00506     return *this;
00507   }
00508 
00509   bfstream& bfstream::operator <<(int a)
00510   {
00511     char *c=reinterpret_cast<char *>(&a);
00512 
00513     if (endianity == native_endianity)
00514       write(c, 4);
00515     else {
00516       put(c[3]);
00517       put(c[2]);
00518       put(c[1]);
00519       put(c[0]);
00520     }
00521     return *this;
00522   }
00523 
00524   bfstream& bfstream::operator <<(unsigned int a)
00525   {
00526     char *c=reinterpret_cast<char *>(&a);
00527 
00528     if (endianity == native_endianity)
00529       write(c, 4);
00530     else {
00531       put(c[3]);
00532       put(c[2]);
00533       put(c[1]);
00534       put(c[0]);
00535     }
00536     return *this;
00537   }
00538 
00539   bfstream& bfstream::operator <<(short a)
00540   {
00541     char *c=reinterpret_cast<char *>(&a);
00542 
00543     if (endianity == native_endianity) {
00544       put(c[0]);
00545       put(c[1]);
00546     } else {
00547       put(c[1]);
00548       put(c[0]);
00549     }
00550     return *this;
00551   }
00552 
00553   bfstream& bfstream::operator <<(unsigned short a)
00554   {
00555     char *c=reinterpret_cast<char *>(&a);
00556 
00557     if (endianity == native_endianity)
00558       write(c, 2);
00559     else {
00560       put(c[1]);
00561       put(c[0]);
00562     }
00563     return *this;
00564   }
00565 
00566   bfstream& bfstream::operator <<(float a)
00567   {
00568     char *c=reinterpret_cast<char *>(&a);
00569 
00570     if (endianity == native_endianity)
00571       write(c, 4);
00572     else {
00573       put(c[3]);
00574       put(c[2]);
00575       put(c[1]);
00576       put(c[0]);
00577     }
00578     return *this;
00579   }
00580 
00581   bfstream& bfstream::operator <<(double a)
00582   {
00583     char *c=reinterpret_cast<char *>(&a);
00584 
00585     if (endianity == native_endianity)
00586       write(c, 8);
00587     else {
00588       put(c[7]);
00589       put(c[6]);
00590       put(c[5]);
00591       put(c[4]);
00592       put(c[3]);
00593       put(c[2]);
00594       put(c[1]);
00595       put(c[0]);
00596     }
00597     return *this;
00598   }
00599 
00600 //   bfstream& bfstream::operator <<(long double a)
00601 //   {
00602 //     char *c=reinterpret_cast<char *>(&a);
00603 
00604 //     if (endianity == native_endianity)
00605 //       write(c, 10);
00606 //     else {
00607 //       put(c[9]);
00608 //       put(c[8]);
00609 //       put(c[7]);
00610 //       put(c[6]);
00611 //       put(c[5]);
00612 //       put(c[4]);
00613 //       put(c[3]);
00614 //       put(c[2]);
00615 //       put(c[1]);
00616 //       put(c[0]);
00617 //     }
00618 //     return *this;
00619 //   }
00620 
00621   bfstream& bfstream::operator <<(long int a)
00622   {
00623     char *c=reinterpret_cast<char *>(&a);
00624 
00625     if (endianity == native_endianity)
00626       write(c, 4);
00627     else {
00628       put(c[3]);
00629       put(c[2]);
00630       put(c[1]);
00631       put(c[0]);
00632     }
00633     return *this;
00634   }
00635 
00636   bfstream& bfstream::operator <<(unsigned long int a)
00637   {
00638     char *c=reinterpret_cast<char *>(&a);
00639 
00640     if (endianity == native_endianity)
00641       write(c, 4);
00642     else {
00643       put(c[3]);
00644       put(c[2]);
00645       put(c[1]);
00646       put(c[0]);
00647     }
00648     return *this;
00649   }
00650 
00651   bfstream& bfstream::operator<<(const char *a)
00652   {
00653     write(a, strlen(a)+1);
00654     return *this;
00655   }
00656 
00657   bfstream& bfstream::operator<<(const std::string &a)
00658   {
00659     write(a.c_str(), a.size()+1);
00660     return *this;
00661   }
00662 
00663   bfstream& bfstream::operator >>(char &a)
00664   {
00665     get(a);
00666     return *this;
00667   }
00668 
00669   bfstream& bfstream::operator >>(class bin &a)
00670   {
00671     char temp;
00672     get(temp);
00673     a=temp;
00674     return *this;
00675   }
00676 
00677   bfstream& bfstream::operator >>(int &a)
00678   {
00679     char *c=reinterpret_cast<char *>(&a);
00680 
00681     if (endianity == native_endianity)
00682       read(c, 4);
00683     else {
00684       get(c[3]);
00685       get(c[2]);
00686       get(c[1]);
00687       get(c[0]);
00688     }
00689     return *this;
00690   }
00691 
00692   bfstream& bfstream::operator >>(unsigned int &a)
00693   {
00694     char *c=reinterpret_cast<char *>(&a);
00695 
00696     if (endianity == native_endianity)
00697       read(c, 4);
00698     else {
00699       get(c[3]);
00700       get(c[2]);
00701       get(c[1]);
00702       get(c[0]);
00703     }
00704     return *this;
00705   }
00706 
00707   bfstream& bfstream::operator >>(short int &a)
00708   {
00709     char *c=reinterpret_cast<char *>(&a);
00710 
00711     if (endianity == native_endianity)
00712       read(c, 2);
00713     else {
00714       get(c[1]);
00715       get(c[0]);
00716     }
00717     return *this;
00718   }
00719 
00720   bfstream& bfstream::operator >>(unsigned short int &a)
00721   {
00722     char *c=reinterpret_cast<char *>(&a);
00723 
00724     if (endianity == native_endianity)
00725       read(c, 2);
00726     else {
00727       get(c[1]);
00728       get(c[0]);
00729     }
00730     return *this;
00731   }
00732 
00733   bfstream& bfstream::operator >>(float &a)
00734   {
00735     char *c=reinterpret_cast<char *>(&a);
00736 
00737     if (endianity == native_endianity)
00738       read(c, 4);
00739     else {
00740       get(c[3]);
00741       get(c[2]);
00742       get(c[1]);
00743       get(c[0]);
00744     }
00745     return *this;
00746   }
00747 
00748   bfstream& bfstream::operator >>(double &a)
00749   {
00750     char *c=reinterpret_cast<char *>(&a);
00751 
00752     if (endianity == native_endianity)
00753       read(c, 8);
00754     else {
00755       get(c[7]);
00756       get(c[6]);
00757       get(c[5]);
00758       get(c[4]);
00759       get(c[3]);
00760       get(c[2]);
00761       get(c[1]);
00762       get(c[0]);
00763     }
00764     return *this;
00765   }
00766 
00767 //   bfstream& bfstream::operator >>(long double &a)
00768 //   {
00769 //     char *c=reinterpret_cast<char *>(&a);
00770 
00771 //     if (endianity == native_endianity)
00772 //       read(c, 10);
00773 //     else {
00774 //       get(c[9]);
00775 //       get(c[8]);
00776 //       get(c[7]);
00777 //       get(c[6]);
00778 //       get(c[5]);
00779 //       get(c[4]);
00780 //       get(c[3]);
00781 //       get(c[2]);
00782 //       get(c[1]);
00783 //       get(c[0]);
00784 //     }
00785 //     return *this;
00786 //   }
00787 
00788   bfstream& bfstream::operator >>(long int &a)
00789   {
00790     char *c=reinterpret_cast<char *>(&a);
00791 
00792     if (endianity == native_endianity)
00793       read(c, 4);
00794     else {
00795       get(c[3]);
00796       get(c[2]);
00797       get(c[1]);
00798       get(c[0]);
00799     }
00800     return *this;
00801   }
00802 
00803   bfstream& bfstream::operator >>(unsigned long int &a)
00804   {
00805     char *c=reinterpret_cast<char *>(&a);
00806 
00807     if (endianity == native_endianity)
00808       read(c, 4);
00809     else {
00810       get(c[3]);
00811       get(c[2]);
00812       get(c[1]);
00813       get(c[0]);
00814     }
00815     return *this;
00816   }
00817 
00818   bfstream& bfstream::operator>>(char *a)
00819   {
00820     getline(a, '\0');
00821     return *this;
00822   }
00823 
00824   bfstream& bfstream::operator>>(std::string &a)
00825   {
00826     std::getline(*this, a, '\0');
00827     return *this;
00828   }
00829 
00830 } // namespace itpp
SourceForge Logo

Generated on Sat Aug 25 23:37:26 2007 for IT++ by Doxygen 1.5.2