1 //----------------------------------------------------------------------------
\r
3 // Interpreter for ntermediate code
\r
4 // $Date: 2003/02/20 00:24:16 $
\r
6 //----------------------------------------------------------------------------
\r
14 #include "QCompilerCode.h"
\r
16 typedef unsigned int Appoptions;
\r
18 const char * const app_name = "qcpp";
\r
19 const char * const app_version = "1.0";
\r
20 const char * const app_extension = ".cpp";
\r
22 const Appoptions opt_version = 1;
\r
23 const Appoptions opt_help = 1 << 1;
\r
24 const Appoptions opt_verbose = 1 << 2;
\r
25 const Appoptions opt_output = 1 << 3;
\r
26 const Appoptions opt_input = 1 << 4;
\r
28 //----------------------------------------------------------------------------
\r
29 // Function prototypes
\r
30 //----------------------------------------------------------------------------
\r
31 inline bool isSet(const Appoptions &o1, const Appoptions &o2) {
\r
32 return (o2 == (o1 & o2));
\r
35 Appoptions checkOption(const char * inOptions);
\r
36 void forceQuit(void);
\r
37 void printUsage(void);
\r
38 void printVersion(void);
\r
39 void errorAndQuit(const char * const inMessage);
\r
40 std::string setOutputFilename(const std::string &inString,
\r
41 const char * const inExt = app_extension);
\r
43 //----------------------------------------------------------------------------
\r
47 int main(int argc, char **argv) {
\r
48 Appoptions parameters_ready = 0;
\r
49 QCompilerCode *qCCode = NULL;
\r
51 std::string ofilename = "";
\r
52 std::string ifilename = "";
\r
53 std::vector<std::string> args;
\r
61 for (int i = 0; i < argc; i++) {
\r
62 std::string tmpstr = argv[i];
\r
63 args.push_back(tmpstr);
\r
66 unsigned int vec_size = args.size();
\r
67 unsigned int arg_at = 1;
\r
69 while (arg_at < vec_size) {
\r
70 if ('-' == args[arg_at][0] && args[arg_at].length() > 1) {
\r
71 Appoptions tmpoptions = checkOption(args[arg_at].c_str());
\r
74 if (isSet(tmpoptions, opt_help)) {
\r
75 if (isSet(parameters_ready, opt_help)) {
\r
76 errorAndQuit("-h option can be used only once.");
\r
78 parameters_ready |= opt_help;
\r
82 //_____ VERBOSE _____
\r
83 if (isSet(tmpoptions, opt_verbose)) {
\r
84 if (isSet(parameters_ready, opt_verbose)) {
\r
85 errorAndQuit("-v option can be used only once.");
\r
87 parameters_ready |= opt_verbose;
\r
91 //_____ VERSION _____
\r
92 if (isSet(tmpoptions, opt_version)) {
\r
93 if (isSet(parameters_ready, opt_version)) {
\r
94 errorAndQuit("-V option can be used only once.");
\r
96 parameters_ready |= opt_version;
\r
100 //_____ OUTPUT _____
\r
101 if (isSet(tmpoptions, opt_output)) {
\r
102 if (isSet(parameters_ready, opt_output)) {
\r
103 errorAndQuit("-o option can be used only once.");
\r
106 if (arg_at < vec_size - 1) {
\r
107 ofilename = args[++arg_at];
\r
108 parameters_ready |= opt_output;
\r
110 errorAndQuit("too few parameters.");
\r
117 if (isSet(parameters_ready, opt_input)) {
\r
118 errorAndQuit("too many input file.");
\r
120 ifilename = args[arg_at];
\r
121 parameters_ready |= opt_input;
\r
126 //_____ VERSION _____
\r
127 if (isSet(parameters_ready, opt_version)) printVersion();
\r
129 //_____ HELP ________
\r
130 if (isSet(parameters_ready, opt_help)) printUsage();
\r
132 //_____ OUTPUT ______
\r
133 if (isSet(parameters_ready, opt_input)) {
\r
134 if (false == isSet(parameters_ready, opt_output)) {
\r
135 ofilename = "none";
\r
138 std::ifstream ifs(ifilename.c_str(), std::ios::in);
\r
140 std::cerr << "Cannot open the input file.\n";
\r
144 if (isSet(parameters_ready, opt_verbose)) {
\r
145 std::cerr << "Input file: " << ifilename << "\n"
\r
146 << "Output file: " << ofilename << "\n";
\r
150 qCCode = new QCompilerCode(ifs, setOutputFilename(ifilename, ".qdt").c_str());
\r
151 } catch (const std::bad_alloc ex) {
\r
152 std::cerr << "Failed in memory allocation.\n";
\r
156 if (true == qCCode->Compile()) {
\r
157 if (isSet(parameters_ready, opt_output)) {
\r
158 std::ofstream ofs(ofilename.c_str(), std::ios::out);
\r
159 qCCode->SaveToStream(ofs);
\r
162 qCCode->SaveToStream(std::cout);
\r
170 if (isSet(parameters_ready, opt_verbose)) {
\r
171 std::cerr << "qcpp: done." << std::endl;
\r
178 //----------------------------------------------------------------------------
\r
182 Appoptions checkOption(const char * inOptions) {
\r
183 Appoptions retVal = 0;
\r
185 while ('\0' != *inOptions) {
\r
186 switch (*inOptions) {
\r
188 retVal |= opt_help;
\r
191 retVal |= opt_output;
\r
194 retVal |= opt_verbose;
\r
197 retVal |= opt_version;
\r
200 errorAndQuit("there are some invalid options.");
\r
208 //----------------------------------------------------------------------------
\r
212 void forceQuit(void) {
\r
213 std::cerr << "Quit forced.\n";
\r
217 //----------------------------------------------------------------------------
\r
221 void printUsage(void) {
\r
222 std::cout << "Usage: " << app_name
\r
223 << " [-hvV] [-o output_file] input_file.mcd\n\n"
\r
224 << " -h show this help\n"
\r
225 << " -v verbose mode\n"
\r
226 << " -V show version\n\n"
\r
230 //----------------------------------------------------------------------------
\r
234 void printVersion(void) {
\r
235 std::cout << "\nCopyright (C) 2002-2003 QCAD project\n"
\r
236 << app_name << " version " << app_version << "\n\n"
\r
240 //----------------------------------------------------------------------------
\r
242 * Show message and quit
\r
244 void errorAndQuit(const char * const inMessage) {
\r
245 std::cerr << "Error: " << inMessage << "\n\n";
\r
250 //----------------------------------------------------------------------------
\r
252 * Set output filename
\r
254 std::string setOutputFilename(const std::string &inString,
\r
255 const char * const inExt) {
\r
256 std::string retString = "";
\r
258 unsigned int posdot = inString.rfind(".", inString.length() - 1);
\r
260 if (posdot == std::string::npos) {
\r
261 retString = inString;
\r
263 retString.assign(inString, 0, posdot);
\r
266 retString += inExt;
\r