Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

bkconvert.cc

Go to the documentation of this file.
00001 // convert.c
00002 // prog converts binary file formats in to format out.
00003 // formats: short, int, float, double (complex)
00004 // BK 28-Jan-00
00005 
00006 using namespace std;
00007 #include <iostream>
00008 #include <fstream>
00009 #include <cstdlib>
00010 #include <cstring>
00011 
00012 const int ONE27=127;
00013 struct sizeofel
00014   {
00015   int ifile;
00016   int ofile;
00017   };
00018 
00019 struct input
00020   {
00021   char ifilename[ONE27];
00022   char ofilename[ONE27];
00023   int iformat;
00024   int oformat;
00025   sizeofel bytesperelement;
00026   };
00027 
00028 
00029 /* ***** */
00030 void usage(char *programname)
00031   {
00032   cerr << "\n  Program: " << programname
00033        << "\n\tconvert binary data to other format.\n\n"
00034        << "  SYNOPSIS:\n\t" << programname
00035        << " inputfile outputfile informat oformat\n\n"
00036        << "\tinputfile: \tname of binary input file [INFILE]\n"
00037        << "\toutputfile: \tname of binary output file [OUTFILE]\n"
00038        << "\tifmt:      \tinput format [2]\n"
00039        << "\tofmt:      \toutput format [4]\n"
00040        << "\n  FORMATS:\n"
00041        << "\t1   \tsigned   (complex) char\n"
00042        << "\t101 \tunsigned (complex) char\n"
00043        << "\t2   \tsigned   (complex) short\n"
00044        << "\t102 \tunsigned (complex) short\n"
00045        << "\t3   \tsigned   (complex) int\n"
00046        << "\t103 \tunsigned (complex) int\n"
00047        << "\t4   \tsigned   (complex) float\n"
00048 //       << "\t104 \tunsigned (complex) float\n"
00049        << "\t5   \tsigned   (complex) double\n"
00050 //       << "\t105 \tunsigned (complex) double\n"
00051        << "\nNote: no rounding is performed when converting from e.g. float to int.\n"
00052        << endl;
00053   exit(-1);
00054   }
00055 
00056 /* ***** */
00057 void handleinput(int argc, char* argv[], input &options)
00058   {
00059   // default short to float
00060   options.iformat = 2;
00061   options.oformat = 4;
00062   strcpy(options.ifilename,"INFILE");
00063   strcpy(options.ofilename,"OUTFILE");
00064 
00065   // Handle command line arguments
00066   switch (argc)
00067     {
00068     case 5:                                     // ofmt
00069       options.oformat = atoi(argv[4]);
00070       //--- fall through --- //
00071     case 4:                                     // ifmt
00072       options.iformat = atoi(argv[3]);
00073       //--- fall through --- //
00074     case 3:                                     // of
00075       strcpy(options.ofilename,argv[2]);
00076       //--- fall through --- //
00077     case 2:                                     // if
00078       strcpy(options.ifilename,argv[1]);
00079       break;
00080 
00081     default:
00082       usage(argv[0]);
00083     } // switch handle arguments
00084 
00085 cerr << "TEST: "
00086 << options.ifilename << "; "
00087 << options.ofilename << "; "
00088 << options.iformat << "; "
00089 << options.oformat << "; "
00090 << endl;
00091 
00092   if      (options.iformat==1 || options.iformat==101)
00093     options.bytesperelement.ifile = sizeof(char);
00094   else if (options.iformat==2 || options.iformat==102)
00095     options.bytesperelement.ifile = sizeof(short);
00096   else if (options.iformat==3 || options.iformat==103)
00097     options.bytesperelement.ifile = sizeof(int);
00098   else if (options.iformat==4 || options.iformat==104)
00099     options.bytesperelement.ifile = sizeof(float);
00100   else if (options.iformat==5 || options.iformat==105)
00101     options.bytesperelement.ifile = sizeof(double);
00102   else
00103     usage(argv[0]);
00104   if      (options.oformat==1 || options.oformat==101)
00105     options.bytesperelement.ofile = sizeof(char);
00106   else if (options.oformat==2 || options.oformat==102)
00107     options.bytesperelement.ofile = sizeof(short);
00108   else if (options.oformat==3 || options.oformat==103)
00109     options.bytesperelement.ofile = sizeof(int);
00110   else if (options.oformat==4 || options.oformat==104)
00111     options.bytesperelement.ofile = sizeof(float);
00112   else if (options.oformat==5 || options.oformat==105)
00113     options.bytesperelement.ofile = sizeof(double);
00114   else
00115     usage(argv[0]);
00116 
00117   if (options.iformat == options.oformat)
00118     usage(argv[0]);
00119   if (options.bytesperelement.ofile < options.bytesperelement.ifile)
00120     cerr << "WARNING: writing to less bytes, no rounding of numbers.\n";
00121   } // handleinput
00122 
00123 //  /* *****
00124 //   * rcw: read, convert, write
00125 //   * template to read numelements of TypeA from opened ifile 
00126 //   * and to write them to opened ofile as TypeB
00127 //   * A and B are dummy function arguments, used to identificate correct template function.
00128 //  */
00129 //  template <class TypeA, class TypeB>
00130 //  void rcw(TypeA in, TypeB out, ifstream &ifile, ofstream &ofile, int numelements)
00131 //    {
00132 //    const int sizeofin  = sizeof(TypeA);
00133 //    const int sizeofout = sizeof(TypeB);
00134 //    for (int i=0; i<numelements; i++)
00135 //      {
00136 //      ifile.read((char*)&in,sizeofin);        // read
00137 //      out = TypeB(in);                        // convert
00138 //      ofile.write((char*)&out,sizeofout);     // write
00139 //      }
00140 //    }
00141 
00142 /* *****
00143  * writedata
00144  * template to write numelements of TypeA from array
00145  * to opened ofile as TypeB
00146  * A and B are dummy function arguments, used to identificate correct template function.
00147 */
00148 template <class TypeA>
00149 void writedata(TypeA *in, ofstream &ofile, int numelements, int oformat, int sizeofout)
00150   {
00151   int i;
00152   switch (oformat)
00153     {
00154     case 1:
00155       {
00156       char out;
00157       for (i=0; i<numelements; ++i)
00158         {
00159         out = char(in[i]);                      // convert
00160         ofile.write((char*)&out,sizeofout);     // write
00161         }
00162       break;
00163       }
00164     case 101:
00165       {
00166       unsigned char out;
00167       for (i=0; i<numelements; ++i)
00168         {
00169         out = char(in[i]);                      // convert
00170         ofile.write((char*)&out,sizeofout);     // write
00171         }
00172       break;
00173       }
00174     case 2:
00175       {
00176       short out;
00177       for (i=0; i<numelements; ++i)
00178         {
00179         out = char(in[i]);                      // convert
00180         ofile.write((char*)&out,sizeofout);     // write
00181         }
00182       break;
00183       }
00184     case 102:
00185       {
00186       unsigned short out;
00187       for (i=0; i<numelements; ++i)
00188         {
00189         out = char(in[i]);                      // convert
00190         ofile.write((char*)&out,sizeofout);     // write
00191         }
00192       break;
00193       }
00194     case 3:
00195       {
00196       int out;
00197       for (i=0; i<numelements; ++i)
00198         {
00199         out = char(in[i]);                      // convert
00200         ofile.write((char*)&out,sizeofout);     // write
00201         }
00202       break;
00203       }
00204     case 103:
00205       {
00206       unsigned int out;
00207       for (i=0; i<numelements; ++i)
00208         {
00209         out = char(in[i]);                      // convert
00210         ofile.write((char*)&out,sizeofout);     // write
00211         }
00212       break;
00213       }
00214     case 4:
00215       {
00216       float out;
00217       for (i=0; i<numelements; ++i)
00218         {
00219         out = char(in[i]);                      // convert
00220         ofile.write((char*)&out,sizeofout);     // write
00221         }
00222       break;
00223       }
00224 
00225 //      case 104:
00226 //        {
00227 //        unsigned float out;
00228 //        for (i=0; i<numelements; ++i)
00229 //          {
00230 //          out = char(in[i]);                  // convert
00231 //          ofile.write((char*)&out,sizeofout); // write
00232 //          }
00233 //        break;
00234 //        }
00235 
00236     case 5:
00237       {
00238       double out;
00239       for (i=0; i<numelements; ++i)
00240         {
00241         out = char(in[i]);                      // convert
00242         ofile.write((char*)&out,sizeofout);     // write
00243         }
00244       break;
00245       }
00246 
00247 //      case 105:
00248 //        {
00249 //        unsigned double out;
00250 //        for (i=0; i<numelements; ++i)
00251 //          {
00252 //          out = char(in[i]);                  // convert
00253 //          ofile.write((char*)&out,sizeofout); // write
00254 //          }
00255 //        break;
00256 //        }
00257 
00258     default:
00259         cerr << "impossible error with checked input.\n";
00260         exit(6);
00261     }
00262   }
00263 
00264 /* ***** */
00265 int main(int argc,char* argv[])
00266   {
00267   //char ident[] = "@(#)Doris software, kampes@geo.tudelft.nl";
00268   char ident[] = "@(#)bkconvert: Doris software, $Revision: 3.6 $, $Author: kampes $";
00269   cerr << ident << endl;
00270   input options;
00271   handleinput(argc,argv,options);
00272 
00273   //ifstream ifile(options.ifilename, ios::in | ios::binary | ios::nocreate);
00274   ifstream ifile(options.ifilename, ios::in | ios::binary);
00275   if (!ifile)
00276     {
00277     cerr << "input file: \"" << options.ifilename << "\" cannot be opened!\n";
00278     return 1;
00279     }
00280   ifile.seekg(0,ios::end);              // filepointer at end
00281   const int numbytes = ifile.tellg();
00282   if (numbytes%options.bytesperelement.ifile != 0)
00283     {
00284     cerr << "file size or format not ok!, exiting\n";
00285     return 4;
00286     }
00287   const int numelements = numbytes/options.bytesperelement.ifile;
00288   ifile.seekg(0,ios::beg);              // rewind
00289 
00290   //ofstream ofile(options.ofilename, ios::out | ios::binary | ios::noreplace);
00291   ofstream ofile(options.ofilename, ios::out | ios::binary);
00292   if (!ofile)
00293     {
00294     cerr << "output file: " << options.ofilename << " cannot be opened!\n"
00295          << "Should I try again (overwriting existing)? [y/n]\n";
00296     char dummychar;
00297     cin >> dummychar;
00298     if (dummychar=='y' || dummychar=='Y')
00299       ofstream ofile(options.ofilename, ios::out | ios::binary | ios::trunc);
00300     else
00301       return 2;
00302     }
00303 
00304   int buffersize = 512;         // number of elements in data buffer
00305   const int NUMBUFFERS   = numelements/buffersize;
00306   const int restelements = numelements%buffersize;
00307   const int EXTRABUF = (restelements!=0) ? 1 : 0;
00308 
00309 //    // input buffer arrays
00310 //    // should be done dynamically to save memory.
00311 //    char            data_schar[buffersize];
00312 //    unsigned char   data_uchar[buffersize];
00313 //    short           data_sshort[buffersize];
00314 //    unsigned short  data_ushort[buffersize];
00315 //    int             data_sint[buffersize];
00316 //    unsigned int    data_uint[buffersize];
00317 //    float           data_sfloat[buffersize];
00318 //    unsigned float  data_ufloat[buffersize];
00319 //    double          data_sdouble[buffersize];
00320 //    unsigned double data_udouble[buffersize];
00321 
00322   // input buffer arrays
00323   // should be done dynamically to save memory.
00324   char            data_schar[512];
00325   unsigned char   data_uchar[512];
00326   short           data_sshort[512];
00327   unsigned short  data_ushort[512];
00328   int             data_sint[512];
00329   unsigned int    data_uint[512];
00330   float           data_sfloat[512];
00331   double          data_sdouble[512];
00332 
00333   for (int i=1; i<=NUMBUFFERS+EXTRABUF; i++)
00334     {
00335     if (i==NUMBUFFERS+1)        // last smaller buffer
00336       buffersize=restelements;
00337 
00338     // read data in array
00339     switch (options.iformat)
00340       {
00341       case 1:
00342         ifile.read((char*)&data_schar[0],buffersize*options.bytesperelement.ifile);
00343         writedata(data_schar,
00344         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00345         break;
00346       case 101:
00347         ifile.read((char*)&data_uchar[0],buffersize*options.bytesperelement.ifile);
00348         writedata(data_uchar,
00349         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00350         break;
00351       case 2:
00352         ifile.read((char*)&data_sshort[0],buffersize*options.bytesperelement.ifile);
00353         writedata(data_sshort,
00354         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00355         break;
00356       case 102:
00357         ifile.read((char*)&data_ushort[0],buffersize*options.bytesperelement.ifile);
00358         writedata(data_ushort,
00359         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00360         break;
00361       case 3:
00362         ifile.read((char*)&data_sint[0],buffersize*options.bytesperelement.ifile);
00363         writedata(data_sint,
00364         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00365         break;
00366       case 103:
00367         ifile.read((char*)&data_uint[0],buffersize*options.bytesperelement.ifile);
00368         writedata(data_uint,
00369         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00370         break;
00371       case 4:
00372         ifile.read((char*)&data_sfloat[0],buffersize*options.bytesperelement.ifile);
00373         writedata(data_sfloat,
00374         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00375         break;
00376 
00377 //        case 104:
00378 //          ifile.read((char*)&data_ufloat[0],buffersize*options.bytesperelement.ifile);
00379 //          writedata(data_ufloat,
00380 //          ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00381 //          break;
00382 
00383       case 5:
00384         ifile.read((char*)&data_sdouble[0],buffersize*options.bytesperelement.ifile);
00385         writedata(data_sdouble,
00386         ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00387         break;
00388 
00389 //        case 105:
00390 //          ifile.read((char*)&data_udouble[0],buffersize*options.bytesperelement.ifile);
00391 //          writedata(data_udouble,
00392 //          ofile, buffersize, options.oformat, options.bytesperelement.ofile);
00393 //          break;
00394 
00395       default:
00396         cerr << "impossible error with checked input.\n";
00397         return 5;
00398       }
00399     }
00400   ifile.close();
00401   ofile.close();
00402 
00403   //rcw(gettypea(options.iformat), gettypeb(options.oformat),
00404   //    ifile, ofile, numelements);
00405 
00406   cout << "Normal termination.\n";
00407   cerr << "Output file: \"" << options.ofilename << "\"\n";
00408   return 0;
00409   }
00410 
00411 
00412 

Generated on Fri Apr 22 15:57:48 2005 for Doris by doxygen 1.3.6