00001
00002
00003
00004
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
00049 << "\t5 \tsigned (complex) double\n"
00050
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
00060 options.iformat = 2;
00061 options.oformat = 4;
00062 strcpy(options.ifilename,"INFILE");
00063 strcpy(options.ofilename,"OUTFILE");
00064
00065
00066 switch (argc)
00067 {
00068 case 5:
00069 options.oformat = atoi(argv[4]);
00070
00071 case 4:
00072 options.iformat = atoi(argv[3]);
00073
00074 case 3:
00075 strcpy(options.ofilename,argv[2]);
00076
00077 case 2:
00078 strcpy(options.ifilename,argv[1]);
00079 break;
00080
00081 default:
00082 usage(argv[0]);
00083 }
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 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
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]);
00160 ofile.write((char*)&out,sizeofout);
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]);
00170 ofile.write((char*)&out,sizeofout);
00171 }
00172 break;
00173 }
00174 case 2:
00175 {
00176 short out;
00177 for (i=0; i<numelements; ++i)
00178 {
00179 out = char(in[i]);
00180 ofile.write((char*)&out,sizeofout);
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]);
00190 ofile.write((char*)&out,sizeofout);
00191 }
00192 break;
00193 }
00194 case 3:
00195 {
00196 int out;
00197 for (i=0; i<numelements; ++i)
00198 {
00199 out = char(in[i]);
00200 ofile.write((char*)&out,sizeofout);
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]);
00210 ofile.write((char*)&out,sizeofout);
00211 }
00212 break;
00213 }
00214 case 4:
00215 {
00216 float out;
00217 for (i=0; i<numelements; ++i)
00218 {
00219 out = char(in[i]);
00220 ofile.write((char*)&out,sizeofout);
00221 }
00222 break;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 case 5:
00237 {
00238 double out;
00239 for (i=0; i<numelements; ++i)
00240 {
00241 out = char(in[i]);
00242 ofile.write((char*)&out,sizeofout);
00243 }
00244 break;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
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
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
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);
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);
00289
00290
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;
00305 const int NUMBUFFERS = numelements/buffersize;
00306 const int restelements = numelements%buffersize;
00307 const int EXTRABUF = (restelements!=0) ? 1 : 0;
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
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)
00336 buffersize=restelements;
00337
00338
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
00378
00379
00380
00381
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
00390
00391
00392
00393
00394
00395 default:
00396 cerr << "impossible error with checked input.\n";
00397 return 5;
00398 }
00399 }
00400 ifile.close();
00401 ofile.close();
00402
00403
00404
00405
00406 cout << "Normal termination.\n";
00407 cerr << "Output file: \"" << options.ofilename << "\"\n";
00408 return 0;
00409 }
00410
00411
00412