1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkObject.h"
#include <vtkstd/string>
#include <vtkstd/vector>
#include <vtksys/SystemTools.hxx>
#include <vtksys/RegularExpression.hxx>
#include <vtksys/ios/fstream>
#include <vtksys/ios/sstream>
class Output
{
public:
Output()
{
this->MaxLen = 16000;
this->CurrentPosition = 0;
}
~Output()
{
}
Output(const Output&){}
void operator=(const Output&){}
vtksys_ios::ostringstream Stream;
int MaxLen;
long CurrentPosition;
int Count;
vtkstd::string Prefix;
vtkstd::string Suffix;
void PrintHeader(const char* title, const char* file)
{
this->Stream << endl
<< "// From file " << file << endl
<< "static const char* " << this->Prefix.c_str() << title << this->Suffix.c_str() << this->Count
<< " =" << endl;
this->CurrentPosition = this->Stream.tellp();
}
void CheckSplit(const char* title, const char* file, int force=0)
{
if ( (static_cast<long>(this->Stream.tellp()) - this->CurrentPosition) > this->MaxLen ||
force )
{
this->Count ++;
this->Stream << ";" << endl;
this->PrintHeader(title, file);
}
}
int ProcessFile(const char* file, const char* title)
{
vtksys_ios::ifstream ifs(file);
if ( !ifs )
{
cout << "Canot open file: " << file << endl;
return 0;
}
int ch;
int in_ifdef = 0;
this->Count = 0;
this->PrintHeader(title, file);
this->Stream << "\"";
vtkstd::string line;
vtkstd::string::size_type cc;
vtksys::RegularExpression reIfDef("^[ \r\n\t]*#[ \r\n\t]*if");
vtksys::RegularExpression reElse("^[ \r\n\t]*#[ \r\n\t]*el(se|if)");
vtksys::RegularExpression reEndif("^[ \r\n\t]*#[ \r\n\t]*endif");
int res = 0;
while ( vtksys::SystemTools::GetLineFromStream(ifs, line) )
{
res ++;
int regex = 0;
int ifdef_line = 0;
if ( reIfDef.find(line) )
{
in_ifdef ++;
regex = 1;
ifdef_line = 1;
}
else if ( reElse.find(line) )
{
regex = 1;
}
else if ( reEndif.find(line) )
{
in_ifdef --;
regex = 1;
}
if ( regex )
{
this->Stream << "\\n\"" << endl;
if ( ifdef_line )
{
this->CheckSplit(title, file, 1);
}
this->Stream << line.c_str() << endl;
if ( !ifdef_line )
{
this->CheckSplit(title, file);
}
this->Stream << "\"";
}
else
{
for ( cc = 0; cc < line.size(); cc ++ )
{
ch = line[cc];
if ( ch == '\\' )
{
this->Stream << "\\\\";
}
else if ( ch == '\"' )
{
this->Stream << "\\\"";
}
else
{
this->Stream << (unsigned char)ch;
}
}
this->Stream << "\\n\"" << endl;
if ( !in_ifdef )
{
this->CheckSplit(title, file);
}
this->Stream << "\"";
}
}
this->Stream << "\\n\";" << endl;
if ( !res )
{
return 0;
}
return this->Count+1;
}
};
int main(int argc, char* argv[])
{
if ( argc < 4 )
{
cerr << "Usage: " << argv[0] << " <output-file> <prefix> <suffix> <getmethod> <modules>..."
<< endl;
return 1;
}
Output ot;
ot.Prefix = argv[2];
ot.Suffix = argv[3];
ot.Stream << "// Loadable modules" << endl
<< "//" << endl
<< "// Generated by " << argv[0] << endl
<< "//" << endl
<< "#ifndef __" << ot.Prefix.c_str() << "_h" << endl
<< "#define __" << ot.Prefix.c_str() << "_h" << endl
<< endl
<< "#include <string.h>" << endl
<< endl;
vtkstd::string output = argv[1];
int cc;
for ( cc = 5; cc < argc; cc ++ )
{
vtkstd::string fname = argv[cc];
vtkstd::string moduleName;
vtksys::RegularExpression mnrex(".*/(.*).(xml|pvsm|py)$");
if ( mnrex.find(fname) )
{
moduleName = mnrex.match(1);
}
else
{
cerr << "Cannot extract module name from the file: " << argv[cc] << endl;
return 1;
}
cout << "-- Generate module: " << moduleName << endl;
int num = 0;
if ( (num = ot.ProcessFile(fname.c_str(), moduleName.c_str())) == 0 )
{
cout << "Problem generating header file from XML file: " << fname.c_str() << endl;
return 1;
}
int kk;
vtksys_ios::ostringstream createstring;
vtksys_ios::ostringstream lenstr;
for ( kk = 0; kk < num; kk ++ )
{
lenstr << endl
<< " + strlen(" << ot.Prefix.c_str()
<< moduleName.c_str() << ot.Suffix.c_str() << kk << ")";
createstring << " strcat(res, " << ot.Prefix.c_str() << moduleName.c_str() << ot.Suffix.c_str()
<< kk << ");" << endl;
}
ot.Stream
<< "// Get single string" << endl
<< "char* " << ot.Prefix.c_str() << moduleName.c_str() << argv[4] << "()" << endl
<< "{" << endl
<< " size_t len = ( 0"
<< lenstr.str()
<< " );" << endl
<< " char* res = new char[ len + 1];" << endl
<< " res[0] = 0;" << endl
<< createstring.str()
<< " return res;" << endl
<< "}" << endl << endl;
}
ot.Stream
<< endl << endl
<< "#endif" << endl;
FILE* fp = fopen(output.c_str(), "w");
if ( !fp )
{
cout << "Cannot open output file: " << output.c_str() << endl;
return 1;
}
fprintf(fp, "%s", ot.Stream.str().c_str());
fclose(fp);
return 0;
}
|