2018-04-24 21:16:22 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-27 20:42:19 +00:00
|
|
|
#include "port.h"
|
2018-04-24 21:16:22 +00:00
|
|
|
#include "bml.h"
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_node::bml_node()
|
2018-04-27 20:42:19 +00:00
|
|
|
{
|
2019-06-02 20:58:04 +00:00
|
|
|
type = CHILD;
|
2019-05-14 22:42:41 +00:00
|
|
|
depth = -1;
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:42:19 +00:00
|
|
|
static inline int islf(char c)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2018-04-27 20:42:19 +00:00
|
|
|
return (c == '\r' || c == '\n');
|
|
|
|
}
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2018-04-27 20:42:19 +00:00
|
|
|
static inline int isblank(char c)
|
|
|
|
{
|
|
|
|
return (c == ' ' || c == '\t');
|
|
|
|
}
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2018-04-27 20:42:19 +00:00
|
|
|
static inline int isalnum(char c)
|
|
|
|
{
|
|
|
|
return ((c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= '0' && c <= '9'));
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bml_valid (char c)
|
|
|
|
{
|
|
|
|
return (isalnum (c) || c == '-');
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static std::string trim(std::string str)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2018-04-27 20:42:19 +00:00
|
|
|
int start;
|
|
|
|
int end;
|
2019-05-14 22:42:41 +00:00
|
|
|
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; isblank (str[end]) || str[end] == '\n' || str[end] == '\r'; end--) {}
|
|
|
|
return str.substr(start, end - start + 1);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static inline unsigned int bml_read_depth(char *data)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
unsigned int depth;
|
2019-05-14 22:42:41 +00:00
|
|
|
for (depth = 0; isblank(data[depth]); depth++) {}
|
2018-04-24 21:16:22 +00:00
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static void bml_parse_depth(bml_node &node, char **data)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
unsigned int depth = bml_read_depth(*data);
|
2018-04-24 21:16:22 +00:00
|
|
|
*data += depth;
|
2019-05-14 22:42:41 +00:00
|
|
|
node.depth = depth;
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static void bml_parse_name(bml_node &node, char **data)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
for (len = 0; bml_valid(*(*data + len)); len++) {};
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
node.name = trim(std::string(*data, len));
|
2018-04-24 21:16:22 +00:00
|
|
|
*data += len;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static void bml_parse_data(bml_node &node, char **data)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
char *p = *data;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (p[0] == '=' && p[1] == '\"')
|
|
|
|
{
|
|
|
|
len = 2;
|
2019-05-14 22:42:41 +00:00
|
|
|
while (p[len] && p[len] != '\"' && !islf(p[len]))
|
2018-04-24 21:16:22 +00:00
|
|
|
len++;
|
|
|
|
if (p[len] != '\"')
|
|
|
|
return;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
node.data = std::string(p + 2, len - 2);
|
2018-04-24 21:16:22 +00:00
|
|
|
*data += len + 1;
|
|
|
|
}
|
|
|
|
else if (*p == '=')
|
|
|
|
{
|
|
|
|
len = 1;
|
2019-05-14 22:42:41 +00:00
|
|
|
while (p[len] && !islf(p[len]) && p[len] != '"' && p[len] != ' ')
|
2018-04-24 21:16:22 +00:00
|
|
|
len++;
|
|
|
|
if (p[len] == '\"')
|
|
|
|
return;
|
2019-05-14 22:42:41 +00:00
|
|
|
node.data = std::string(p + 1, len - 1);
|
2018-04-24 21:16:22 +00:00
|
|
|
*data += len;
|
|
|
|
}
|
|
|
|
else if (*p == ':')
|
|
|
|
{
|
|
|
|
len = 1;
|
2019-05-14 22:42:41 +00:00
|
|
|
while (p[len] && !islf(p[len]))
|
2018-04-24 21:16:22 +00:00
|
|
|
len++;
|
2019-05-14 22:42:41 +00:00
|
|
|
node.data = std::string(p + 1, len - 1);
|
2018-04-24 21:16:22 +00:00
|
|
|
*data += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static void bml_skip_empty(char **data)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
char *p = *data;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
for (; *p && isblank (*p) ; p++) {}
|
|
|
|
|
|
|
|
if (!islf(p[0]) && (p[0] != '/' && p[1] != '/'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Skip comment data */
|
|
|
|
while (*p && *p != '\r' && *p != '\n')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* If we found new-line, try to skip more */
|
|
|
|
if (*p)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
*data = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *bml_read_line (char **data)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
bml_skip_empty (data);
|
|
|
|
|
|
|
|
line = *data;
|
|
|
|
|
|
|
|
if (line == NULL || *line == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p = strpbrk (line, "\r\n\0");
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (islf (*p))
|
|
|
|
{
|
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data = p;
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static void bml_parse_attr(bml_node &node, char **data)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
char *p = *data;
|
|
|
|
int len;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
while (*p && !islf(*p))
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
if (*p != ' ')
|
|
|
|
return;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
while (isblank(*p))
|
2018-04-24 21:16:22 +00:00
|
|
|
p++;
|
|
|
|
if (p[0] == '/' && p[1] == '/')
|
|
|
|
break;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_node n;
|
2018-04-24 21:16:22 +00:00
|
|
|
len = 0;
|
2019-05-14 22:42:41 +00:00
|
|
|
while (bml_valid(p[len]))
|
|
|
|
len++;
|
2018-04-24 21:16:22 +00:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2019-05-14 22:42:41 +00:00
|
|
|
n.name = trim(std::string(p, len));
|
2018-04-24 21:16:22 +00:00
|
|
|
p += len;
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_parse_data(n, &p);
|
2019-06-02 20:58:04 +00:00
|
|
|
n.depth = node.depth + 1;
|
|
|
|
n.type = bml_node::CHILD;
|
2019-05-14 22:42:41 +00:00
|
|
|
node.child.push_back(n);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*data = p;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static int contains_space (const char *str)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; str[i]; i++)
|
|
|
|
{
|
|
|
|
if (isblank (str[i]))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static void bml_print_node(bml_node &node, int depth)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2018-04-26 00:29:26 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < depth * 2; i++)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
printf (" ");
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
if (!node.name.empty())
|
|
|
|
printf ("%s", node.name.c_str());
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
if (!node.data.empty())
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
if (contains_space(node.data.c_str()))
|
|
|
|
printf ("=\"%s\"", node.data.c_str());
|
2018-04-24 21:16:22 +00:00
|
|
|
else
|
2019-05-14 22:42:41 +00:00
|
|
|
printf (": %s", node.data.c_str());
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
2019-06-02 20:58:04 +00:00
|
|
|
for (i = 0; i < (int) node.child.size () && node.child[i].type == bml_node::CHILD; i++)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
if (!node.child[i].name.empty())
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
printf (" %s", node.child[i].name.c_str());
|
|
|
|
if (!node.child[i].data.empty())
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
if (contains_space(node.child[i].data.c_str()))
|
|
|
|
printf ("=\"%s\"", node.child[i].data.c_str());
|
2018-04-24 21:16:22 +00:00
|
|
|
else
|
2019-05-14 22:42:41 +00:00
|
|
|
printf ("=%s", node.child[i].data.c_str());
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth >= 0)
|
|
|
|
printf ("\n");
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
for (; i < (int) node.child.size(); i++)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_print_node (node.child[i], depth + 1);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (depth == 0)
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
void bml_node::print()
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_print_node(*this, -1);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
static bml_node bml_parse_node(char **doc)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
char *line;
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_node node;
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
if ((line = bml_read_line(doc)))
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_parse_depth(node, &line);
|
|
|
|
bml_parse_name(node, &line);
|
|
|
|
bml_parse_data(node, &line);
|
|
|
|
bml_parse_attr(node, &line);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
else
|
2019-05-14 22:42:41 +00:00
|
|
|
return node;
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_skip_empty(doc);
|
|
|
|
while (*doc && (int)bml_read_depth(*doc) > node.depth)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_node child = bml_parse_node(doc);
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
if (child.depth != -1)
|
|
|
|
node.child.push_back(child);
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_skip_empty(doc);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
void bml_node::parse(char *doc)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_node node;
|
|
|
|
char *ptr = doc;
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
while ((node = bml_parse_node (&ptr)).depth != -1)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
child.push_back(node);
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
bml_node *bml_node::find_subnode (std::string name)
|
2018-04-26 00:29:26 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
for (i = 0; i < child.size (); i++)
|
2018-04-26 00:29:26 +00:00
|
|
|
{
|
2019-05-14 22:42:41 +00:00
|
|
|
if (name.compare(child[i].name) == 0)
|
|
|
|
return &child[i];
|
2018-04-26 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
bool bml_node::parse_file(const char *filename)
|
2018-04-24 21:16:22 +00:00
|
|
|
{
|
|
|
|
FILE *file = NULL;
|
|
|
|
char *buffer = NULL;
|
|
|
|
int file_size = 0;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
file = fopen(filename, "rb");
|
2018-04-24 21:16:22 +00:00
|
|
|
|
|
|
|
if (!file)
|
2019-05-14 22:42:41 +00:00
|
|
|
return false;
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
file_size = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
2018-04-24 21:16:22 +00:00
|
|
|
|
|
|
|
buffer = new char[file_size + 1];
|
2019-05-14 22:42:41 +00:00
|
|
|
fread(buffer, file_size, 1, file);
|
2018-04-24 21:16:22 +00:00
|
|
|
buffer[file_size] = '\0';
|
2019-05-14 22:42:41 +00:00
|
|
|
fclose(file);
|
2018-04-24 21:16:22 +00:00
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
parse(buffer);
|
2018-04-24 21:16:22 +00:00
|
|
|
delete[] buffer;
|
|
|
|
|
2019-05-14 22:42:41 +00:00
|
|
|
return true;
|
2018-04-24 21:16:22 +00:00
|
|
|
}
|