00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <cctype>
00043 #include <algorithm>
00044
00045 #include "parse_util.hh"
00046 #include "prop_registry.hh"
00047 #include "writer.hh"
00048
00049
00050 string Writer::className2cc (const string &className)
00051 {
00052 string res;
00053
00054 for (int n = 0; n < className.size(); n++)
00055 if (isupper(className[n])) {
00056 if (n != 0) res += '_';
00057 res += tolower(className[n]);
00058 } else res += className[n];
00059
00060 if (registry.contains("cc_extension"))
00061 return (res += '.') += registry["cc_extension"];
00062 else
00063 return res += ".cc";
00064 }
00065
00066
00067 string Writer::className2hh (const string &className)
00068 {
00069 string res;
00070
00071 for (int n = 0; n < className.size(); n++)
00072 if (isupper(className[n])) {
00073 if (n != 0) res += '_';
00074 res += tolower(className[n]);
00075 } else res += className[n];
00076
00077 if (registry.contains("hh_extension"))
00078 return (res += '.') += registry["hh_extension"];
00079 else
00080 return res += ".hh";
00081 }
00082
00083 string Writer::className2macro (const string &className)
00084 {
00085 string res("__");
00086
00087 for (int n = 0; n < className.size(); n++)
00088 if (isupper(className[n])) {
00089 if (n != 0) res += '_';
00090 res += className[n];
00091 } else res += toupper(className[n]);
00092
00093 return res += "_HH__";
00094 }
00095
00096
00097 string Writer::fullPath (const string &fileName)
00098 {
00099 if (registry.contains("target_dir"))
00100 return ((string) registry["target_dir"]) + fileName;
00101 else return fileName;
00102 }
00103
00104
00105 void Writer::openStream (const string &fullPathName)
00106 {
00107 ofs.open(fullPathName.c_str());
00108 if (!ofs)
00109 die(string("Could not open file \"") + fullPathName + "\" for writing.");
00110
00111 _indent.resize(0);
00112 _line = 8;
00113 _file = fullPathName;
00114
00115 ofs << "/******************************************************************"
00116 << endl
00117 << " * File: " << fullPathName << endl
00118 << " *" << endl
00119 << " * This file was automatically generated by CppCC Version " << VERSION
00120 << endl
00121 << " * DO NOT EDIT MANUALLY" << endl
00122 << " ******************************************************************/"
00123 << endl
00124 << endl;
00125 }
00126
00127
00128 void Writer::closeStream ()
00129 {
00130 ofs << endl << endl << endl
00131 << "/* End of file. */"
00132 << endl;
00133 ofs.close();
00134 }
00135
00136
00137 ostream& Writer::writeChunk (const CodeChunk &cc, const string &terminator)
00138 {
00139 if (registry["SHARP_LINES"]) {
00140 int lines = cc.countLines();
00141
00142
00143
00144 ofs << endl << "#line " << cc.pos.ln - lines + 1
00145 << " \"" << registry["input_file"]
00146 << "\"" << endl;
00147 _line += 2;
00148 ofs << cc.code << terminator;
00149 _line += lines + 1;
00150 ofs << endl << "#line " << _line << " \"" << _file << "\"" << endl;
00151 } else {
00152 ofs << cc.code << terminator << endl;
00153 _line += cc.countLines() + 1;
00154 }
00155 return ofs;
00156 }