changed "outfile" parameter to be a base name, added "classname" parameter

This commit is contained in:
Josh Holtrop 2010-04-28 17:27:36 -04:00
parent bd45bfbab2
commit 2b23657a0c
3 changed files with 25 additions and 11 deletions

View File

@ -15,6 +15,7 @@
using namespace std; using namespace std;
Parser::Parser() Parser::Parser()
: m_classname("Parser")
{ {
} }

View File

@ -23,10 +23,13 @@ class Parser
} }
void write(const std::string & fname); void write(const std::string & fname);
bool parseInputFile(char * buff, int size); bool parseInputFile(char * buff, int size);
void setClassName(const std::string & cn) { m_classname = cn; }
std::string getClassName() { return m_classname; }
protected: protected:
std::vector< refptr< TokenDefinition > > m_tokens; std::vector< refptr< TokenDefinition > > m_tokens;
std::vector< refptr< RuleDefinition > > m_rules; std::vector< refptr< RuleDefinition > > m_rules;
std::string m_classname;
}; };
#endif #endif

View File

@ -15,10 +15,12 @@ int main(int argc, char * argv[])
{ {
int longind = 1; int longind = 1;
int opt; int opt;
string output_fname; string outfile;
string classname = "Parser";
static struct option longopts[] = { static struct option longopts[] = {
/* name, has_arg, flag, val */ /* name, has_arg, flag, val */
{ "classname", required_argument, NULL, 'c' },
{ "outfile", required_argument, NULL, 'o' }, { "outfile", required_argument, NULL, 'o' },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
@ -27,12 +29,21 @@ int main(int argc, char * argv[])
{ {
switch (opt) switch (opt)
{ {
case 'c': /* classname */
classname = optarg;
break;
case 'o': /* outfile */ case 'o': /* outfile */
output_fname = optarg; outfile = optarg;
break; break;
} }
} }
if (optind >= argc)
{
cerr << "Usage: imbecile [options] <input-file>" << endl;
return 2;
}
string input_fname = argv[optind]; string input_fname = argv[optind];
ifstream ifs; ifstream ifs;
ifs.open(input_fname.c_str(), ios::binary); ifs.open(input_fname.c_str(), ios::binary);
@ -48,14 +59,13 @@ int main(int argc, char * argv[])
ifs.read(buff, size); ifs.read(buff, size);
ifs.close(); ifs.close();
if (output_fname == "") if (outfile == "")
output_fname = buildOutputFilename(input_fname); outfile = buildOutputFilename(input_fname);
Parser p; Parser p;
p.setClassName(classname);
p.parseInputFile(buff, size); p.parseInputFile(buff, size);
p.write(outfile);
p.write(output_fname);
delete[] buff; delete[] buff;
return 0; return 0;
@ -63,15 +73,15 @@ int main(int argc, char * argv[])
string buildOutputFilename(string & input_fname) string buildOutputFilename(string & input_fname)
{ {
string output_fname; string outfile;
size_t len = input_fname.length(); size_t len = input_fname.length();
if (len > 2 && input_fname.substr(len - 2) == ".I") if (len > 2 && input_fname.substr(len - 2) == ".I")
{ {
output_fname = input_fname.substr(0, len - 2) + ".cc"; outfile = input_fname.substr(0, len - 2);
} }
else else
{ {
output_fname = input_fname + ".cc"; outfile = input_fname;
} }
return output_fname; return outfile;
} }