-
Notifications
You must be signed in to change notification settings - Fork 2
/
junk_template.cpp
108 lines (88 loc) · 1.77 KB
/
junk_template.cpp
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
#include ".\junk_template.h"
#include <iostream>
#include "junk_variables.h"
junk_template::junk_template(void):_buffer(0),_protect_reg(false),_buff_temp(0)
{
_list.clear();
_map.clear();
}
junk_template::~junk_template(void)
{
delete _buffer;
_buffer=0;
delete _buff_temp;
_buff_temp=0;
junk_var::iterator iter;
for(iter=_list.begin();iter!=_list.end();iter++)
{
delete *iter;
}
}
void junk_template::template_txt(char *text)
{
_buffer = (char *) malloc(strlen(text));
strcpy(_buffer,text);
}
void junk_template::init_map()
{
_map.clear();
junk_var::iterator iter;
for(iter=_list.begin();iter!=_list.end();iter++)
{
_map[(*iter)->name()]=(*iter)->tostr();
}
}
char *junk_template::tostr()
{
if(!_buff_temp)
_buff_temp = (char *) malloc ( strlen(_buffer) );
char *current=_buffer;
char *current_end=current;
char *dest=_buff_temp;
init_map();
junk_variable *var=0;
if(!(var=nextvariable(current)))
{
strcpy(_buff_temp,_buffer);
}
else
{
while(var=nextvariable(current))
{
char *var_rep=_map[var->name()];
current_end=strstr(current,var->name());
strncpy(dest,current,current_end-current);
dest +=current_end-current;
strncpy(dest,var_rep,strlen(var_rep));
dest+=strlen(var_rep);
current = current_end + strlen(var->name());
}
strcpy(dest,current);
dest[strlen(current)]='\0';
}
return _buff_temp;
}
junk_variable *junk_template::nextvariable(char *text)
{
junk_var::iterator iter;
junk_variable *tmp=0;
char *min=0;
char *cur;
for(iter=_list.begin();iter!=_list.end();iter++)
{
char *test=(*iter)->name();
if(cur=strstr(text,(*iter)->name()))
{
if ( cur < min || !min)
{
min=cur;
tmp=*iter;
}
}
}
return tmp;
}
void junk_template::add_var(junk_variable *var)
{
_list.push_front(var);
}