| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// qcpp.cpp |
| 3 |
// Interpreter for ntermediate code |
| 4 |
// $Date: 2003/02/20 00:24:16 $ |
| 5 |
// $Revision: 1.3 $ |
| 6 |
//---------------------------------------------------------------------------- |
| 7 |
#include <iostream> |
| 8 |
#include <iomanip> |
| 9 |
#include <fstream> |
| 10 |
#include <cstdlib> |
| 11 |
#include <vector> |
| 12 |
#include <string> |
| 13 |
|
| 14 |
#include "QCompilerCode.h" |
| 15 |
|
| 16 |
typedef unsigned int Appoptions; |
| 17 |
|
| 18 |
const char * const app_name = "qcpp"; |
| 19 |
const char * const app_version = "1.0"; |
| 20 |
const char * const app_extension = ".cpp"; |
| 21 |
|
| 22 |
const Appoptions opt_version = 1; |
| 23 |
const Appoptions opt_help = 1 << 1; |
| 24 |
const Appoptions opt_verbose = 1 << 2; |
| 25 |
const Appoptions opt_output = 1 << 3; |
| 26 |
const Appoptions opt_input = 1 << 4; |
| 27 |
|
| 28 |
//---------------------------------------------------------------------------- |
| 29 |
// Function prototypes |
| 30 |
//---------------------------------------------------------------------------- |
| 31 |
inline bool isSet(const Appoptions &o1, const Appoptions &o2) { |
| 32 |
return (o2 == (o1 & o2)); |
| 33 |
} |
| 34 |
|
| 35 |
Appoptions checkOption(const char * inOptions); |
| 36 |
void forceQuit(void); |
| 37 |
void printUsage(void); |
| 38 |
void printVersion(void); |
| 39 |
void errorAndQuit(const char * const inMessage); |
| 40 |
std::string setOutputFilename(const std::string &inString, |
| 41 |
const char * const inExt = app_extension); |
| 42 |
|
| 43 |
//---------------------------------------------------------------------------- |
| 44 |
/** |
| 45 |
* Main |
| 46 |
*/ |
| 47 |
int main(int argc, char **argv) { |
| 48 |
Appoptions parameters_ready = 0; |
| 49 |
QCompilerCode *qCCode = NULL; |
| 50 |
|
| 51 |
std::string ofilename = ""; |
| 52 |
std::string ifilename = ""; |
| 53 |
std::vector<std::string> args; |
| 54 |
|
| 55 |
if (argc <= 1) { |
| 56 |
printVersion(); |
| 57 |
printUsage(); |
| 58 |
std::exit(0); |
| 59 |
} |
| 60 |
|
| 61 |
for (int i = 0; i < argc; i++) { |
| 62 |
std::string tmpstr = argv[i]; |
| 63 |
args.push_back(tmpstr); |
| 64 |
} |
| 65 |
|
| 66 |
unsigned int vec_size = args.size(); |
| 67 |
unsigned int arg_at = 1; |
| 68 |
|
| 69 |
while (arg_at < vec_size) { |
| 70 |
if ('-' == args[arg_at][0] && args[arg_at].length() > 1) { |
| 71 |
Appoptions tmpoptions = checkOption(args[arg_at].c_str()); |
| 72 |
|
| 73 |
//_____ HELP _____ |
| 74 |
if (isSet(tmpoptions, opt_help)) { |
| 75 |
if (isSet(parameters_ready, opt_help)) { |
| 76 |
errorAndQuit("-h option can be used only once."); |
| 77 |
} else { |
| 78 |
parameters_ready |= opt_help; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
//_____ VERBOSE _____ |
| 83 |
if (isSet(tmpoptions, opt_verbose)) { |
| 84 |
if (isSet(parameters_ready, opt_verbose)) { |
| 85 |
errorAndQuit("-v option can be used only once."); |
| 86 |
} else { |
| 87 |
parameters_ready |= opt_verbose; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
//_____ VERSION _____ |
| 92 |
if (isSet(tmpoptions, opt_version)) { |
| 93 |
if (isSet(parameters_ready, opt_version)) { |
| 94 |
errorAndQuit("-V option can be used only once."); |
| 95 |
} else { |
| 96 |
parameters_ready |= opt_version; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
//_____ OUTPUT _____ |
| 101 |
if (isSet(tmpoptions, opt_output)) { |
| 102 |
if (isSet(parameters_ready, opt_output)) { |
| 103 |
errorAndQuit("-o option can be used only once."); |
| 104 |
} |
| 105 |
|
| 106 |
if (arg_at < vec_size - 1) { |
| 107 |
ofilename = args[++arg_at]; |
| 108 |
parameters_ready |= opt_output; |
| 109 |
} else { |
| 110 |
errorAndQuit("too few parameters."); |
| 111 |
} |
| 112 |
} |
| 113 |
arg_at++; |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
if (isSet(parameters_ready, opt_input)) { |
| 118 |
errorAndQuit("too many input file."); |
| 119 |
} else { |
| 120 |
ifilename = args[arg_at]; |
| 121 |
parameters_ready |= opt_input; |
| 122 |
} |
| 123 |
arg_at++; |
| 124 |
} |
| 125 |
|
| 126 |
//_____ VERSION _____ |
| 127 |
if (isSet(parameters_ready, opt_version)) printVersion(); |
| 128 |
|
| 129 |
//_____ HELP ________ |
| 130 |
if (isSet(parameters_ready, opt_help)) printUsage(); |
| 131 |
|
| 132 |
//_____ OUTPUT ______ |
| 133 |
if (isSet(parameters_ready, opt_input)) { |
| 134 |
if (false == isSet(parameters_ready, opt_output)) { |
| 135 |
ofilename = "none"; |
| 136 |
} |
| 137 |
|
| 138 |
std::ifstream ifs(ifilename.c_str(), std::ios::in); |
| 139 |
if (!ifs) { |
| 140 |
std::cerr << "Cannot open the input file.\n"; |
| 141 |
forceQuit(); |
| 142 |
} |
| 143 |
|
| 144 |
if (isSet(parameters_ready, opt_verbose)) { |
| 145 |
std::cerr << "Input file: " << ifilename << "\n" |
| 146 |
<< "Output file: " << ofilename << "\n"; |
| 147 |
} |
| 148 |
|
| 149 |
try { |
| 150 |
qCCode = new QCompilerCode(ifs, setOutputFilename(ifilename, ".qdt").c_str()); |
| 151 |
} catch (const std::bad_alloc ex) { |
| 152 |
std::cerr << "Failed in memory allocation.\n"; |
| 153 |
forceQuit(); |
| 154 |
} |
| 155 |
|
| 156 |
if (true == qCCode->Compile()) { |
| 157 |
if (isSet(parameters_ready, opt_output)) { |
| 158 |
std::ofstream ofs(ofilename.c_str(), std::ios::out); |
| 159 |
qCCode->SaveToStream(ofs); |
| 160 |
ofs.close(); |
| 161 |
} else { |
| 162 |
qCCode->SaveToStream(std::cout); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
delete qCCode; |
| 167 |
|
| 168 |
ifs.close(); |
| 169 |
|
| 170 |
if (isSet(parameters_ready, opt_verbose)) { |
| 171 |
std::cerr << "qcpp: done." << std::endl; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
//---------------------------------------------------------------------------- |
| 179 |
/** |
| 180 |
* Check options |
| 181 |
*/ |
| 182 |
Appoptions checkOption(const char * inOptions) { |
| 183 |
Appoptions retVal = 0; |
| 184 |
inOptions++; |
| 185 |
while ('\0' != *inOptions) { |
| 186 |
switch (*inOptions) { |
| 187 |
case 'h': |
| 188 |
retVal |= opt_help; |
| 189 |
break; |
| 190 |
case 'o': |
| 191 |
retVal |= opt_output; |
| 192 |
break; |
| 193 |
case 'v': |
| 194 |
retVal |= opt_verbose; |
| 195 |
break; |
| 196 |
case 'V': |
| 197 |
retVal |= opt_version; |
| 198 |
break; |
| 199 |
default: |
| 200 |
errorAndQuit("there are some invalid options."); |
| 201 |
break; |
| 202 |
} |
| 203 |
inOptions++; |
| 204 |
} |
| 205 |
return retVal; |
| 206 |
} |
| 207 |
|
| 208 |
//---------------------------------------------------------------------------- |
| 209 |
/** |
| 210 |
* Force quit |
| 211 |
*/ |
| 212 |
void forceQuit(void) { |
| 213 |
std::cerr << "Quit forced.\n"; |
| 214 |
std::exit(1); |
| 215 |
} |
| 216 |
|
| 217 |
//---------------------------------------------------------------------------- |
| 218 |
/** |
| 219 |
* Print usages |
| 220 |
*/ |
| 221 |
void printUsage(void) { |
| 222 |
std::cout << "Usage: " << app_name |
| 223 |
<< " [-hvV] [-o output_file] input_file.mcd\n\n" |
| 224 |
<< " -h show this help\n" |
| 225 |
<< " -v verbose mode\n" |
| 226 |
<< " -V show version\n\n" |
| 227 |
<< std::flush; |
| 228 |
} |
| 229 |
|
| 230 |
//---------------------------------------------------------------------------- |
| 231 |
/** |
| 232 |
* Print version |
| 233 |
*/ |
| 234 |
void printVersion(void) { |
| 235 |
std::cout << "\nCopyright (C) 2002-2003 QCAD project\n" |
| 236 |
<< app_name << " version " << app_version << "\n\n" |
| 237 |
<< std::flush; |
| 238 |
} |
| 239 |
|
| 240 |
//---------------------------------------------------------------------------- |
| 241 |
/** |
| 242 |
* Show message and quit |
| 243 |
*/ |
| 244 |
void errorAndQuit(const char * const inMessage) { |
| 245 |
std::cerr << "Error: " << inMessage << "\n\n"; |
| 246 |
printUsage(); |
| 247 |
forceQuit(); |
| 248 |
} |
| 249 |
|
| 250 |
//---------------------------------------------------------------------------- |
| 251 |
/** |
| 252 |
* Set output filename |
| 253 |
*/ |
| 254 |
std::string setOutputFilename(const std::string &inString, |
| 255 |
const char * const inExt) { |
| 256 |
std::string retString = ""; |
| 257 |
|
| 258 |
unsigned int posdot = inString.rfind(".", inString.length() - 1); |
| 259 |
|
| 260 |
if (posdot == std::string::npos) { |
| 261 |
retString = inString; |
| 262 |
} else { |
| 263 |
retString.assign(inString, 0, posdot); |
| 264 |
} |
| 265 |
|
| 266 |
retString += inExt; |
| 267 |
|
| 268 |
return retString; |
| 269 |
} |
| 270 |
|