Menu

[r329]: / framework / trunk / templates.cpp  Maximize  Restore  History

Download this file

232 lines (218 with data), 4.8 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
 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
#include "templates.h"
#include "global_config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
using namespace std;
bool Renderer::debug_defined=false;
bool Renderer::debug=false;
Renderer::Renderer(Templates_Set &tset,int id,Content &cont)
{
if(!debug_defined) {
debug=global_config.lval("templates.debug_level",0);
debug_defined=true;
}
templates_set=&tset;
tmpl=tset.get(id);
if(!tmpl && debug) {
char buf[32];
snprintf(buf,32,"%d",id);
throw HTTP_Error(string("Failed to load template")+buf);
}
pos=0;
stack_size=0;
content=&cont;
returns_stack.reserve(10);
templates_stack.reserve(10);
}
int Renderer::render(string &s)
{
if(!tmpl) return 0;
Content &cont=*content;
Base_Template *tmp_tmpl;
int templ_id;
for(;;) {
if(pos<0 || pos>(int64_t)(tmpl->len-sizeof(Tmpl_Op))){
throw HTTP_Error("Template overflow");
}
Tmpl_Op *op=(Tmpl_Op*)(tmpl->mem_ptr+pos);
pos+=sizeof(Tmpl_Op);
Variable var;
switch(op->opcode){
case OP_VAR:
case OP_GOTO_IF_TRUE:
case OP_GOTO_IF_FALSE:
case OP_GOTO_IF_DEF:
case OP_GOTO_IF_NDEF:
case OP_INCLUDE_REF:
var=cont[op->parameter];
break;
}
switch(op->opcode) {
case OP_INLINE:
if(pos+op->parameter>tmpl->len){
throw HTTP_Error("Template overflow");
}
s.append(tmpl->mem_ptr+pos,
op->parameter);
pos+=op->parameter;
break;
case OP_CALL:
return op->parameter;
case OP_VAR:
if(var.isstr()) {
s+=var.gets();
}
else if(debug) {
throw HTTP_Error("Undefined variable");
}
break;
case OP_INCLUDE_REF:
if(var.isint()){
templ_id=var.geti();
}
else {
throw HTTP_Error("Undefined variable in INCLUDE_REF");
}
case OP_INCLUDE:
if(op->opcode==OP_INCLUDE){
templ_id=op->parameter;
}
if((tmp_tmpl=templates_set->get(templ_id))==NULL){
if(debug)
throw HTTP_Error("Undefined template");
break;
}
if(returns_stack.size()<(unsigned)stack_size+1){
returns_stack.push_back(pos);
templates_stack.push_back(tmpl);
}
else {
returns_stack[stack_size]=pos;
templates_stack[stack_size]=tmpl;
}
tmpl=tmp_tmpl;
stack_size++;
pos=0;
break;
case OP_GOTO_IF_TRUE:
if(var.isint()){
if(var.geti()){
pos=op->jump;
}
}
else if(debug){
throw HTTP_Error("Undefined variable");
}
break;
case OP_GOTO_IF_FALSE:
if(var.isint()){
if(!var.geti()){
pos=op->jump;
}
}
else if(debug){
throw HTTP_Error("Undefined variable");
}
break;
case OP_GOTO_IF_DEF:
if(var.isdef()){
pos=op->jump;
}
break;
case OP_GOTO_IF_NDEF:
if(!var.isdef()){
pos=op->jump;
}
break;
case OP_GOTO:
pos=op->jump;
break;
case OP_STOP:
if(stack_size==0){
return 0;
}
stack_size--;
pos=returns_stack[stack_size];
tmpl=templates_stack[stack_size];
break;
default:
throw HTTP_Error("Unknown opcode");
}
}
}
void Templates_Set::load()
{
load(global_config.sval("templates.file").c_str());
}
void Templates_Set::load(char const *file,int use_mmap)
{
if(use_mmap==-1) {
use_mmap=global_config.lval("templates.use_mmap",0);
}
if(use_mmap) {
// Rationale:
// In case of many processes openning same file
struct stat statbuf;
char *buf;
if((fd=open(file,O_RDONLY))<0
|| fstat (fd,&statbuf) < 0
|| (buf=(char*)mmap(0,statbuf.st_size,
PROT_READ,MAP_SHARED, fd, 0))==(char*)-1)
{
throw HTTP_Error(string("Falied to open file:")+file);
}
file_size=statbuf.st_size;
base_ptr=buf;
}
else {
FILE *f;
f=fopen(file,"r");
if(!f) {
throw HTTP_Error(string("Falied to open file:")+file);
}
fseek(f,0,SEEK_END);
file_size=ftell(f);
rewind(f);
char *buf=new char [file_size];
base_ptr=buf;
if((int)fread(buf,1,file_size,f)!=file_size) {
fclose(f);
throw HTTP_Error(string("Falied to read file:")+file);
}
fclose(f);
}
setup_map();
}
void Templates_Set::setup_map()
{
if(file_size<4) {
throw HTTP_Error("Incorrect file format");
}
map_size=*(uint32_t*)base_ptr;
if(4+4*map_size>file_size || map_size<0){
throw HTTP_Error("Incorrect file format");
}
map=((uint32_t*)base_ptr)+1;
templates.reserve(map_size);
int offset=4+4*map_size;
int i;
for(i=0;i<map_size;i++){
if((int64_t)map[i]+offset>file_size){
HTTP_Error("Incorrect file format");
}
templates.push_back(Base_Template(base_ptr+offset,map[i]));
offset+=map[i];
}
if(offset!=file_size)
HTTP_Error("Incorrect file format");
}
Base_Template *Templates_Set::get(int id)
{
if(id<0 || id>=map_size) {
return NULL;
}
return &(templates[id]);
}
MongoDB Logo MongoDB