-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathxml_stringsub.cc
More file actions
164 lines (141 loc) · 4.51 KB
/
xml_stringsub.cc
File metadata and controls
164 lines (141 loc) · 4.51 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
#include <list>
#include <string>
#include <map>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <libxml/parser.h>
#include "xml_data.hh"
#include "xml_utils.hh"
struct xmlStyle
{
std::string name;
xmlNodePtr node;
};
static std::map<std::string, std::string> arglist;
static std::map<std::string, std::string> ppclist;
static std::list<xmlNodePtr> xmldefaults;
static std::list<struct xmlStyle> xmlstyles;
static std::string replace_string(const std::string &instr)
{
if (instr.find_first_of("#$") == std::string::npos) return instr;
std::string outstr;
for (unsigned i=0; i<instr.length(); i++)
{
if (instr[i] == '\\' && i+1 < instr.length() && (instr[i+1] == '#' || instr[i+1] == '$'))
{
outstr += instr[i+1];
i++;
}
else if (instr[i] == '#' || instr[i] == '$')
{
unsigned brackets = 0; // keep track of how many characters are brackets
std::string evalstr, tmpstr;
if (i+1 < instr.length() && instr[i+1] == '{')
{
brackets = 2;
for (unsigned j=i+2, count=1; count>0 && j<instr.length(); j++)
{
if (instr[j] == '{') count++;
else if (instr[j] == '}') count--;
if (count) evalstr += instr[j];
}
}
else
{
for (unsigned j=i+1; j<instr.length() && (isalnum(instr[j]) || instr[j] == '_'); j++) evalstr += instr[j];
}
tmpstr = replace_string(evalstr);
if (instr[i] == '#')
{
// First, check arguments list...
std::map<std::string, std::string>::iterator it = arglist.find(tmpstr);
if (it != arglist.end()) outstr += it->second;
else
{
// If not in arguments list, check if the constant has been set in the XML file hierarchy...
it = ppclist.find(tmpstr);
if (it != ppclist.end()) outstr += it->second;
}
}
else
{
char *myenv = getenv(tmpstr.c_str());
if (myenv) outstr += myenv;
}
i += evalstr.length() + brackets;
}
else outstr += instr[i];
}
return outstr;
}
xmldata get_node_content(xmlNodePtr node)
{
char *mycontent = get_XML_content(node);
if (mycontent) return replace_string(mycontent);
else return 0x0;
}
xmldata get_element_data(xmlNodePtr innode, const char *key)
{
char *myattr;
myattr = get_XML_attribute(innode, key);
if (myattr) return replace_string(myattr);
char *type = get_node_type(innode);
// If not explicitly defined, check to see if a Style has been defined...
char *style = get_XML_attribute(innode, "Style");
if (style)
{
std::string mystyle = replace_string(style);
std::list<struct xmlStyle>::iterator xmls;
for (xmls = xmlstyles.begin(); xmls != xmlstyles.end(); xmls++)
{
if (NodeCheck(xmls->node, type) && mystyle == xmls->name)
{
myattr = get_XML_attribute(xmls->node, key);
if (myattr) return replace_string(myattr);
}
}
}
// ...if not, check to see if a Default has been defined...
std::list<xmlNodePtr>::iterator xmld;
for (xmld = xmldefaults.begin(); xmld != xmldefaults.end(); xmld++)
{
if (NodeCheck(*xmld, type))
{
myattr = get_XML_attribute(*xmld, key);
if (myattr) return replace_string(myattr);
}
}
// ...if not, return undefined xmlstring
return 0x0;
}
void processArgument(const std::string &key, const std::string &value)
{
arglist[key] = value;
}
void processConstantNode(xmlNodePtr node)
{
ppclist[get_element_data(node, "Name")] = get_node_content(node);
}
void processStyleNode(xmlNodePtr node)
{
xmlNodePtr node1;
for (node1 = node->children; node1; node1 = node1->next)
{
if (NodeValid(node1))
{
struct xmlStyle mystruct;
mystruct.name = get_element_data(node, "Name");
mystruct.node = xmlCopyNode(node1, 1);
xmlstyles.push_front(mystruct);
}
}
}
void processDefaultsNode(xmlNodePtr node)
{
xmlNodePtr node1;
for (node1 = node->children; node1; node1 = node1->next)
{
if (NodeValid(node1)) xmldefaults.push_front(xmlCopyNode(node1, 1));
}
}