Menu

[r17]: / templates_compiler / trunk / static.cpp  Maximize  Restore  History

Download this file

86 lines (75 with data), 1.5 kB

 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
#include <iostream>
#include <stdio.h>
using namespace std;
#include "parser.h"
void compile(string defname,string fname,FILE *fin,FILE *fout)
{
Parser parser(fin);
int i;
string s;
string header="__";
header.reserve(30);
for(i=0;i<(int)fname.size();i++) {
char c=fname[i];
c=toupper(c);
if(c<'A' || c>'Z') {
c='_';
}
header+=c;
}
header+="__";
fprintf(fout,"#ifndef %s\n#define %s\n\n",header.c_str(),header.c_str());
int tok;
fprintf(fout,"#define %s \\\n",defname.c_str());
while((tok=parser.get_tocken(s))!=T_EOF) {
if(tok==T_INLINE) {
fprintf(fout," TEMPLATE_OUTPUT(\"");
for(i=0;i<(int)s.size();i++) {
int c=s[i];
if(0<c && c<32) {
fprintf(fout,"\\x%02x",c);
}
else {
fputc(c,fout);
}
}
fprintf(fout,"\");\\\n");
}
else if(tok==T_SOME_CODE) {
for(i=0;i<(int)s.size();i++) {
int c=s[i];
if(c=='\n') {
fprintf(fout,"\\\n");
}
else {
fputc(c,fout);
}
}
}
}
fprintf(fout,"\n\n#endif\n");
}
int main(int argc,char *argv[])
{
if(argc!=4) {
cerr<<"Usage: compile define-name file-in.tmpl file-out.h\n";
return 1;
}
string def_name=argv[1];
FILE *fin=NULL,*fout=NULL;
if(!(fin=fopen(argv[2],"r")) || !(fout=fopen(argv[3],"w"))){
cerr<<"Failed to open files";
return 1;
}
try {
compile(def_name,argv[2],fin,fout);
}
catch(string &s)
{
cerr<<s<<endl;
return 1;
}
fclose(fin);
fclose(fout);
return 0;
}
MongoDB Logo MongoDB