cstsql.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include <string>
00006 #include <vector>
00007 #include <sstream>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include "clssdf.h"
00011 
00012 using namespace std;
00013 
00014 void usage() {
00015   cerr << "cstsql [-def=definitionfile1 -def=definitionfile2 ... -def=definitionfileN] [-i=inputfile] -c=outputclass [-o=outputfile] [-a=appendfile] [-lower_case_mysql_names=false]" << endl;
00016   cerr << "      [-f=outputformat] [-help|-?|?]" << endl;
00017   cerr << "definitionsfileI is the name if the Ith file of definitions of parent classes. This is " << endl;
00018   cerr << "used in cases where parent classes occur outside the input file" << endl;
00019   cerr << "inputfile is the filename of a valid input file" << endl;
00020   cerr << "outputformat <- [init_static_attributes | " << endl;
00021   cerr << "                 exists                 | " << endl;
00022   cerr << "                 singleton_select       | " << endl;
00023   cerr << "                 insert                 | " << endl;
00024   cerr << "                 update                 | " << endl;
00025   cerr << "                 read_binary            | " << endl;
00026   cerr << "                 write_binary           | " << endl;
00027   cerr << "                 map_by_offset          | " << endl;
00028   cerr << "                 send_binary            | " << endl;
00029   cerr << "                 recv_binary            | " << endl;
00030 }
00031 
00032 const char *get_inputfile( int argc, const char *argv[], int *argmask) {
00033   int i;
00034   for (i=0;i<argc;i++) {
00035     if (memcmp( argv[i], "-i=", 3)==0) {
00036        argmask[i] = 1;
00037        return argv[i]+3;
00038     }
00039   }
00040   return NULL;
00041 }
00042 
00043 const char *get_deffile( int argc, const char *argv[], int *argmask) {
00044   int i;
00045   for (i=0;i<argc;i++) {
00046     if (argmask[i]==0 && memcmp( argv[i], "-def=", 3)==0) {
00047        argmask[i] = 1;
00048        return argv[i]+5;
00049     }
00050   }
00051   return NULL;
00052 }
00053 
00054 const char *get_help( int argc, const char *argv[], int *argmask) {
00055   int i;
00056   for (i=0;i<argc;i++) {
00057     if (memcmp( argv[i], "-h", 2)==0 || memcmp( argv[i], "-?", 2)==0 || argv[i][0]=='?') {
00058        argmask[i] = 1;
00059        return argv[i]+3;
00060     }
00061   }
00062   return NULL;
00063 }
00064 
00065 const char *get_outputfile( int argc, const char *argv[], int *argmask) {
00066   int i;
00067   for (i=0;i<argc;i++) {
00068     if (memcmp( argv[i], "-o=", 3)==0) {
00069        argmask[i] = 1;
00070        return argv[i]+3;
00071     }
00072   }
00073   return NULL;
00074 }
00075 
00076 const char *get_appendfile( int argc, const char *argv[], int *argmask) {
00077   int i;
00078   for (i=0;i<argc;i++) {
00079     if (memcmp( argv[i], "-a=", 3)==0) {
00080        argmask[i] = 1;
00081        return argv[i]+3;
00082     }
00083   }
00084   return NULL;
00085 }
00086 
00087 const char *get_outputformat( int argc, const char *argv[], int *argmask) {
00088   int i;
00089   for (i=0;i<argc;i++) {
00090     if (memcmp( argv[i], "-f=", 3)==0) {
00091        argmask[i] = 1;
00092        return argv[i]+3;
00093     }
00094   }
00095   return NULL;
00096 }
00097 
00098 const char *get_lower_case_mysql_names( int argc, const char *argv[], int *argmask) {
00099   int i;
00100   const char *tag = "-lower_case_mysql_names=";
00101   int ln = strlen( tag);
00102   for (i=0;i<argc;i++) {
00103     if (memcmp( argv[i], tag, ln)==0) {
00104        argmask[i] = 1;
00105        return argv[i]+ln;
00106     }
00107   }
00108   return "false";
00109 }
00110 
00111 const char *get_outputclass( int argc, const char *argv[], int *argmask) {
00112   int i;
00113   for (i=0;i<argc;i++) {
00114     if (memcmp( argv[i], "-c=", 3)==0) {
00115        argmask[i] = 1;
00116        return argv[i]+3;
00117     }
00118   }
00119   return NULL;
00120 }
00121 
00125 int main( int argc, const char *argv[]) {
00126   class_def::class_definition *_class_definition;
00127   const char *infile, *outputclass, *outfile, *appfile, *outputformat, *deffile, *lower_case_mysql_names;
00128   istream *in, *def;
00129   ostream *out;
00130   ifstream fin;
00131   ofstream fout;
00132   int argmask[10], i, k, errflag, class_found;
00133   vector<string> deffiles;
00134 
00135   argmask[0] = 1;
00136   for (i=1;i<10;i++) {
00137     argmask[i] = 0;
00138   }
00139 
00140   if (argc>10 || get_help( argc, argv, argmask)!=NULL) {
00141     usage();
00142     return 1;
00143   }
00144 
00145   infile       = get_inputfile(    argc, argv, argmask);
00146 
00147   deffile = get_deffile( argc, argv, argmask);
00148   while (deffile!=NULL) {
00149     deffiles.push_back( deffile);
00150     deffile = get_deffile( argc, argv, argmask);
00151   }
00152 
00153   outputformat           = get_outputformat( argc, argv, argmask);
00154   lower_case_mysql_names = get_lower_case_mysql_names( argc, argv, argmask);
00155   outputclass            = get_outputclass(  argc, argv, argmask);
00156   outfile                = get_outputfile(   argc, argv, argmask);
00157   appfile                = get_appendfile(   argc, argv, argmask);
00158 
00159   if (outfile!=NULL && appfile!=NULL) {
00160     cerr << "outputfile and appendfile are mutually exclusive" << endl;
00161     class_def::error();
00162   }
00163 
00164   for (errflag=0,i=1;i<argc;i++) {
00165     if (argmask[i]==0) {
00166       cerr << "error argument={" << argv[i] << "} was not recognized" << endl;
00167       errflag = 1;
00168     }
00169   }
00170   if (errflag) {
00171     return 2;
00172   }
00173 
00174   // read definition files to get definitions of parent classes
00175   for (i=k=0;k<deffiles.size();k++) {
00176     ifstream fdef( deffiles[k].c_str());
00177 
00178     if (!fdef.is_open()) {
00179       cerr << "can't open " << deffiles[i] << endl;
00180       return 3;
00181     }
00182     def = &fdef;
00183  
00184     while (*def) {
00185       try {
00186         _class_definition = new class_def::class_definition();
00187         *def >> *_class_definition;
00188         class_def::class_definition::_univ.push_back( _class_definition);
00189         i++;
00190       } catch (std::exception& xpt) {
00191         cerr << xpt.what();
00192         return 5;
00193       } catch (class_def::end_of_input) {
00194         break;
00195       } catch (class_def::comment cm) {
00196 
00197       } catch (...) {
00198         cerr << "unknown exception was thrown" << endl;
00199         break;
00200       }
00201     }
00202 
00203     fdef.close();
00204     def = NULL;
00205   }
00206 
00207   class_def::attribute::put_allow_custom_classes( 1);
00208 
00209   if (infile==NULL) {
00210     in = &cin;
00211   } else {
00212     fin.open( infile);
00213     if (!fin.is_open()) {
00214       cerr << "can't open " << infile << endl;
00215       return 3;
00216     }
00217     in = &fin;
00218   }
00219 
00220   if (outfile==NULL) {
00221     out = &cout;
00222   } else {
00223     fout.open( outfile, ios::binary);
00224     if (!fout.is_open()) {
00225       cerr << "can't open " << outfile << endl;
00226       return 4;
00227     }
00228     out = &fout;
00229   }
00230 
00231   if (appfile!=NULL) {
00232     fout.open( appfile, ios::app | ios::binary);
00233     if (!fout.is_open()) {
00234       cerr << "can't open " << outfile << endl;
00235       return 4;
00236     }
00237     out = &fout;
00238   }
00239 
00240   if (outputformat==NULL) {
00241     cerr << "output format must be specified" << endl;
00242     return 5;
00243   }
00244 
00245   if (outputclass==NULL) {
00246     cerr << "output class must be specified" << endl;
00247     return 5;
00248   }
00249 
00250   class_def::class_definition::set_output_format( outputformat);
00251 
00252   if (lower_case_mysql_names != NULL && strcmp( lower_case_mysql_names, "true")==0) {
00253     class_def::class_definition::set_lower_case_mysql_names( true);
00254   }
00255 
00256   class_found = 0;
00257   while (*in && class_found==0) {
00258     try {
00259       _class_definition = new class_def::class_definition();
00260       *in >> *_class_definition;
00261       class_def::class_definition::_univ.push_back( _class_definition);
00262       //i++;
00263     } catch (std::exception& xpt) {
00264       cerr << xpt.what();
00265       return 5;
00266     } catch (class_def::comment cm) {
00267       continue;
00268     } catch (class_def::end_of_input) {
00269       break;
00270     } catch (...) {
00271       cerr << "unknown exception was thrown" << endl;
00272       break;
00273     }
00274 
00275     if (strcmp( _class_definition->_name.c_str(), outputclass)==0) {
00276       try {
00277         *out << *_class_definition;
00278         class_found = 1;
00279       } catch (std::exception& xpt) {
00280         cerr << xpt.what();
00281         return 5;
00282       } catch (...) {
00283         cerr << "unknown exception was thrown" << endl;
00284         break;
00285       }
00286     }
00287   }
00288 
00289   if (class_found==0) {
00290     cerr << "class = " << outputclass << " was not found" << endl;
00291     class_def::error();
00292   }
00293 
00294   return 0;
00295 }

Generated on Tue Jul 14 12:22:31 2009 for cstpp by  doxygen 1.5.1