00001
00002
00003 #include <iostream>
00004 #include <fstream>
00005 #include <string>
00006 #include <vector>
00007 #include <sstream>
00008 #include <stdlib.h>
00009 #include "clssdf.h"
00010
00011 using namespace std;
00012 using namespace class_def;
00013
00014 void usage() {
00015 cerr << "cstpp [-def=definitionfile1 -def=definitionfile2 ... -def=definitionfileN] [-i=inputfile] [-o=outputfile] [-f=outputformat]" << endl;
00016 cerr << "[-help|-?|?] [-e] [-b] [-c] [-nocopy] [-no_check_attributes]" << 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 << "outputfile is the name of the file which will be overwritten" << endl;
00021 cerr << "outputformat <- [simple_output | header_output]" << endl;
00022 cerr << "-def-definitionfile this file contains the definition of parent classes. There maybe multiple files." << endl;
00023 cerr << "-e is causes the function error,to be called, rather than throwing a class error" << endl;
00024 cerr << "-domain_error causes std::domain_error to be thrown, rather than throwing a class error" << endl;
00025 cerr << "-b causes padded white spaces at the end of strings to be removed" << endl;
00026 cerr << "-c allows custom classes" << endl;
00027 cerr << "-nocopy removes copy constructors from all classes" << endl;
00028 cerr << "-no_check_attributes removes calls to the check_attributes function, and the static members _erno and _err_msg" << endl;
00029 }
00030
00034 int main( int argc, const char *argv[]) {
00035 class_def::class_definition *_class_definition;
00036 const char *infile, *outfile, *outputformat, *errorfunction, *backwhite, *domain_error, *deffile, *customclasses, *nocopy,
00037 *no_check_attributes;
00038 istream *in, *def;
00039 ostream *out;
00040 ifstream fin;
00041 ofstream fout;
00042 int argmask[10], i, errflag, j, k;
00043 vector<string> deffiles;
00044
00045 argmask[0] = 1;
00046 for (i=1;i<10;i++) {
00047 argmask[i] = 0;
00048 }
00049
00050 bool ask_help = find_tag( argc, argv, argmask, "-?")!=0 ||
00051 find_tag( argc, argv, argmask, "?")!=0;
00052 if (argc>10 || ask_help) {
00053 usage();
00054 return 1;
00055 }
00056
00057 infile = get_tag( argc, argv, argmask, "-i=", NULL);
00058
00059 deffile = get_tag( argc, argv, argmask, "-def=", NULL);
00060 while (deffile!=0) {
00061 deffiles.push_back( deffile);
00062 deffile = get_tag( argc, argv, argmask, "-def=", NULL);
00063 }
00064
00065 outfile = get_tag( argc, argv, argmask, "-o=", NULL);
00066
00067 outputformat = get_tag( argc, argv, argmask, "-f=", NULL);
00068
00069 errorfunction = find_tag( argc, argv, argmask, "-e");
00070
00071 domain_error = find_tag( argc, argv, argmask, "-domain_error");
00072
00073 customclasses = find_tag( argc, argv, argmask, "-c");
00074
00075 nocopy = find_tag( argc, argv, argmask, "-nocopy");
00076
00077 no_check_attributes = find_tag( argc, argv, argmask, "-no_check_attributes");
00078
00079 backwhite = find_tag( argc, argv, argmask, "-b");
00080
00081 if (errorfunction!=NULL) {
00082 class_def::class_definition::set_error_function( 1);
00083 }
00084
00085 if (domain_error!=NULL) {
00086 class_def::class_definition::set_domain_error( 1);
00087 }
00088
00089 if (customclasses!=NULL) {
00090 class_def::attribute::put_allow_custom_classes( 1);
00091 }
00092
00093 if (nocopy != NULL) {
00094 class_def::attribute::put_copy_constructor( 0);
00095 }
00096
00097 if (no_check_attributes != NULL) {
00098 class_def::attribute::put_check_attributes( 0);
00099 }
00100
00101 if (backwhite!=NULL) {
00102 class_def::class_definition::set_back_white( 1);
00103 }
00104
00105 for (errflag=0,i=1;i<argc;i++) {
00106 if (argmask[i]==0) {
00107 cerr << "error argument={" << argv[i] << "} was not recognized" << endl;
00108 errflag = 1;
00109 }
00110 }
00111 if (errflag) {
00112 return 2;
00113 }
00114
00115
00116 for (i=k=0;k<deffiles.size();k++) {
00117 ifstream fdef( deffiles[k].c_str());
00118
00119 if (!fdef.is_open()) {
00120 cerr << "can't open " << deffiles[i] << endl;
00121 return 3;
00122 }
00123 def = &fdef;
00124
00125 while (*def) {
00126 try {
00127 _class_definition = new class_def::class_definition();
00128 *def >> *_class_definition;
00129 class_def::class_definition::_univ.push_back( _class_definition);
00130 i++;
00131 } catch (std::exception& xpt ) {
00132 cerr << xpt.what();
00133 return 5;
00134 } catch (class_def::end_of_input) {
00135 break;
00136 } catch (class_def::comment cm) {
00137
00138 } catch (...) {
00139 cerr << "unknown exception was thrown" << endl;
00140 break;
00141 }
00142 }
00143
00144 fdef.close();
00145 def = NULL;
00146 }
00147
00148 if (infile==NULL) {
00149 in = &cin;
00150 } else {
00151 fin.open( infile);
00152 if (!fin.is_open()) {
00153 cerr << "can't open " << infile << endl;
00154 return 3;
00155 }
00156 in = &fin;
00157 }
00158
00159 if (outfile==NULL) {
00160 out = &cout;
00161 } else {
00162 fout.open( outfile, ios::binary);
00163 if (!fout.is_open()) {
00164 cerr << "can't open " << outfile << endl;
00165 return 4;
00166 }
00167 out = &fout;
00168 }
00169
00170 if (outputformat==NULL)
00171 outputformat = "header_output";
00172
00173 class_def::class_definition::set_output_format( outputformat);
00174
00175 j = 0;
00176 while (*in) {
00177 if (j==0 && domain_error != NULL) {
00178 j = 1;
00179 out->flush();
00180 *out << class_def::print_stringify() << endl;
00181 }
00182
00183 try {
00184 _class_definition = new class_def::class_definition();
00185 *in >> *_class_definition;
00186 class_def::class_definition::_univ.push_back( _class_definition);
00187
00188 *out << *(class_def::class_definition::_univ.back());
00189
00190
00191 } catch (std::exception& xpt) {
00192 cerr << xpt.what();
00193 return 5;
00194 } catch (class_def::end_of_input) {
00195 break;
00196 } catch (class_def::comment cm) {
00197 *out << cm._data << endl;
00198 } catch (...) {
00199 cerr << "unknown exception was thrown" << endl;
00200 break;
00201 }
00202 }
00203
00204 for (j=0;j<i;j++) {
00205 _class_definition = class_def::class_definition::_univ[j];
00206 delete _class_definition;
00207 }
00208
00209 return 0;
00210 }