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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
#include <vector>
#include <iostream>
#include <fstream>
#include <stack>
#include <stdio.h>
#include "port.h"
#include "bml.h"
bml_node::bml_node()
{
type = CHILD;
depth = -1;
}
static inline int islf(char c)
{
return (c == '\r' || c == '\n');
}
static inline int isblank(char c)
{
return (c == ' ' || c == '\t');
}
static inline int isblankorlf(char c)
{
return (islf(c) || isblank(c));
}
static inline int isalnum(char c)
{
return ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'));
}
static inline int bml_valid(char c)
{
return (isalnum(c) || c == '-');
}
static std::string trim(std::string str)
{
int start;
int end;
for (start = 0; str[start] && start != (int)str.length() && isblank(str[start]); start++) {}
if (start >= (int)str.length())
return std::string("");
for (end = str.length() - 1; isblankorlf(str[end]); end--) {}
return str.substr(start, end - start + 1);
}
static std::string trimcomments(std::string str)
{
int end = str.length();
size_t comment = str.find("//");
if (comment != std::string::npos)
end = comment;
for (int i = end - 1; i >= 0; i--)
{
if (!isblankorlf(str[i]))
{
end = i + 1;
break;
}
}
return str.substr(0, end);
}
static inline int bml_read_depth(std::string &data)
{
size_t depth;
for (depth = 0; isblank(data[depth]) && depth < data.length(); depth++) {}
return depth == data.length() ? -1 : depth;
}
static void bml_parse_depth(bml_node &node, std::string &line)
{
unsigned int depth = bml_read_depth(line);
line.erase(0, depth);
node.depth = depth;
}
static void bml_parse_name(bml_node &node, std::string &line)
{
int len;
for (len = 0; bml_valid(line[len]); len++) {};
node.name = trim(line.substr(0, len));
line.erase(0, len);
}
static void bml_parse_data(bml_node &node, std::string &line)
{
int len;
if (line[0] == '=' && line[1] == '\"')
{
len = 2;
while (line[len] && line[len] != '\"' && !islf(line[len]))
len++;
if (line[len] != '\"')
return;
node.data = line.substr(2, len - 2);
line.erase(0, len + 1);
}
else if (line[0] == '=')
{
len = 1;
while (line[len] && !islf(line[len]) && line[len] != '"' && line[len] != ' ')
len++;
if (line[len] == '\"')
return;
node.data = line.substr(1, len - 1);
line.erase(0, len);
}
else if (line[0] == ':')
{
len = 1;
while (line[len] && !islf(line[len]))
len++;
node.data = trim(line.substr(1, len - 1));
line.erase(0, len);
}
return;
}
static std::string bml_read_line(std::ifstream &fd)
{
std::string line;
while (fd)
{
std::getline(fd, line);
line = trimcomments(line);
if (!line.empty())
{
return line;
}
}
return std::string("");
}
static void bml_parse_attr(bml_node &node, std::string &line)
{
int len;
while (line.length() > 0)
{
if (!isblank(line[0]))
return;
while (isblank(line[0]))
line.erase(0, 1);
bml_node n;
len = 0;
while (bml_valid(line[len]))
len++;
if (len == 0)
return;
n.name = trim(line.substr(0, len));
line.erase(0, len);
bml_parse_data(n, line);
n.depth = node.depth + 1;
n.type = bml_node::ATTRIBUTE;
node.child.push_back(n);
}
}
static int contains_space(const char *str)
{
for (int i = 0; str[i]; i++)
{
if (isblank(str[i]))
return 1;
}
return 0;
}
static void bml_print_node(bml_node &node, int depth)
{
int i;
for (i = 0; i < depth * 2; i++)
{
printf(" ");
}
if (!node.name.empty())
printf("%s", node.name.c_str());
if (!node.data.empty())
{
if (contains_space(node.data.c_str()))
printf("=\"%s\"", node.data.c_str());
else
printf(": %s", node.data.c_str());
}
for (i = 0; i < (int)node.child.size() && node.child[i].type == bml_node::ATTRIBUTE; i++)
{
if (!node.child[i].name.empty())
{
printf(" %s", node.child[i].name.c_str());
if (!node.child[i].data.empty())
{
if (contains_space(node.child[i].data.c_str()))
printf("=\"%s\"", node.child[i].data.c_str());
else
printf("=%s", node.child[i].data.c_str());
}
}
}
if (depth >= 0)
printf("\n");
for (; i < (int)node.child.size(); i++)
{
bml_print_node(node.child[i], depth + 1);
}
if (depth == 0)
printf("\n");
}
void bml_node::print()
{
bml_print_node(*this, -1);
}
void bml_node::parse(std::ifstream &fd)
{
std::stack<bml_node *> nodestack;
nodestack.push(this);
while (fd)
{
bml_node newnode;
std::string line = bml_read_line(fd);
if (line.empty())
return;
int line_depth = bml_read_depth(line);
while (line_depth <= nodestack.top()->depth && nodestack.size() > 1)
nodestack.pop();
bml_parse_depth(newnode, line);
bml_parse_name(newnode, line);
bml_parse_data(newnode, line);
bml_parse_attr(newnode, line);
nodestack.top()->child.push_back(newnode);
nodestack.push(&nodestack.top()->child.back());
}
return;
}
bml_node *bml_node::find_subnode(std::string name)
{
unsigned int i;
for (i = 0; i < child.size(); i++)
{
if (name.compare(child[i].name) == 0)
return &child[i];
}
return NULL;
}
bool bml_node::parse_file(std::string filename)
{
std::ifstream file(filename.c_str(), std::ios_base::binary);
if (!file)
return false;
parse(file);
return true;
}
|