sync and clean up
Change-Id: Ia3b2d76f9016454b9e80ca0891ee02e0c2fd1a13
Este commit está contenido en:
+44
-36
@@ -11,6 +11,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace xml {
|
namespace xml {
|
||||||
@@ -52,13 +53,13 @@ class Xml {
|
|||||||
}
|
}
|
||||||
for (auto* incl : incl_nodes) {
|
for (auto* incl : incl_nodes) {
|
||||||
const std::string& incl_name = path + incl->opts["file"];
|
const std::string& incl_name = path + incl->opts["file"];
|
||||||
Xml *ixml = Create(incl_name, xml);
|
Xml* ixml = Create(incl_name, xml);
|
||||||
if (ixml == NULL) {
|
if (ixml == NULL) {
|
||||||
delete xml;
|
delete xml;
|
||||||
xml = NULL;
|
xml = NULL;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
delete(ixml);
|
delete ixml;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (xml) {
|
if (xml) {
|
||||||
@@ -70,7 +71,9 @@ class Xml {
|
|||||||
return xml;
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Destroy(Xml *xml) { delete xml; }
|
static void Destroy(Xml* xml) { delete xml; }
|
||||||
|
|
||||||
|
std::string GetName() { return file_name_; }
|
||||||
|
|
||||||
void AddExpr(const std::string& full_tag, const std::string& name, const std::string& expr) {
|
void AddExpr(const std::string& full_tag, const std::string& name, const std::string& expr) {
|
||||||
const std::size_t pos = full_tag.rfind('.');
|
const std::size_t pos = full_tag.rfind('.');
|
||||||
@@ -91,23 +94,25 @@ class Xml {
|
|||||||
|
|
||||||
nodes_t GetNodes(const std::string& global_tag) { return (*map_)[global_tag]; }
|
nodes_t GetNodes(const std::string& global_tag) { return (*map_)[global_tag]; }
|
||||||
|
|
||||||
template <class F>
|
template <class F> F ForEach(const F& f_i) {
|
||||||
F ForEach(const F& f_i) {
|
|
||||||
F f = f_i;
|
F f = f_i;
|
||||||
if (map_) for (auto& entry : *map_) {
|
if (map_) {
|
||||||
for (auto node : entry.second) {
|
for (auto& entry : *map_) {
|
||||||
if (f.fun(entry.first, node) == false) break;
|
for (auto node : entry.second) {
|
||||||
|
if (f.fun(entry.first, node) == false) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class F>
|
template <class F> F ForEach(const F& f_i) const {
|
||||||
F ForEach(const F& f_i) const {
|
|
||||||
F f = f_i;
|
F f = f_i;
|
||||||
if (map_) for (auto& entry : *map_) {
|
if (map_) {
|
||||||
for (auto node : entry.second) {
|
for (auto& entry : *map_) {
|
||||||
if (f.fun(entry.first, node) == false) break;
|
for (auto node : entry.second) {
|
||||||
|
if (f.fun(entry.first, node) == false) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return f;
|
return f;
|
||||||
@@ -137,8 +142,7 @@ class Xml {
|
|||||||
comment_(false),
|
comment_(false),
|
||||||
included_(false),
|
included_(false),
|
||||||
level_(NULL),
|
level_(NULL),
|
||||||
map_(NULL)
|
map_(NULL) {
|
||||||
{
|
|
||||||
if (obj != NULL) {
|
if (obj != NULL) {
|
||||||
map_ = obj->map_;
|
map_ = obj->map_;
|
||||||
level_ = obj->level_;
|
level_ = obj->level_;
|
||||||
@@ -163,7 +167,7 @@ class Xml {
|
|||||||
bool Init() {
|
bool Init() {
|
||||||
fd_ = open(file_name_.c_str(), O_RDONLY);
|
fd_ = open(file_name_.c_str(), O_RDONLY);
|
||||||
if (fd_ == -1) {
|
if (fd_ == -1) {
|
||||||
//perror((std::string("open XML file ") + file_name_).c_str());
|
// perror((std::string("open XML file ") + file_name_).c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,20 +182,19 @@ class Xml {
|
|||||||
|
|
||||||
void PreProcess() {
|
void PreProcess() {
|
||||||
uint32_t ind = 0;
|
uint32_t ind = 0;
|
||||||
const uint32_t buf_size = 128;
|
char buf[kBufSize];
|
||||||
char buf[buf_size];
|
|
||||||
bool error = false;
|
bool error = false;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
const uint32_t pos = lseek(fd_, 0, SEEK_CUR);
|
const uint32_t pos = lseek(fd_, 0, SEEK_CUR);
|
||||||
uint32_t size = read(fd_, buf, buf_size);
|
uint32_t size = read(fd_, buf, kBufSize);
|
||||||
if (size <= 0) break;
|
if (size <= 0) break;
|
||||||
buf[size - 1] = '\0';
|
buf[size - 1] = '\0';
|
||||||
|
|
||||||
if (strncmp(buf, "#include \"", 10) == 0) {
|
if (strncmp(buf, "#include \"", 10) == 0) {
|
||||||
for (ind = 0; (ind < size) && (buf[ind] != '\n'); ++ind);
|
for (ind = 0; (ind < size) && (buf[ind] != '\n'); ++ind) {}
|
||||||
if (ind == size) {
|
if (ind == size) {
|
||||||
fprintf(stderr, "XML PreProcess failed, line size limit %d\n", (int)buf_size);
|
fprintf(stderr, "XML PreProcess failed, line size limit %zu\n", kBufSize);
|
||||||
error = true;
|
error = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -199,7 +202,7 @@ class Xml {
|
|||||||
size = ind;
|
size = ind;
|
||||||
lseek(fd_, pos + ind + 1, SEEK_SET);
|
lseek(fd_, pos + ind + 1, SEEK_SET);
|
||||||
|
|
||||||
for (ind = 10; (ind < size) && (buf[ind] != '"'); ++ind);
|
for (ind = 10; (ind < size) && (buf[ind] != '"'); ++ind) {}
|
||||||
if (ind == size) {
|
if (ind == size) {
|
||||||
error = true;
|
error = true;
|
||||||
break;
|
break;
|
||||||
@@ -227,9 +230,9 @@ class Xml {
|
|||||||
token_t token = (remainder.size()) ? remainder : NextToken();
|
token_t token = (remainder.size()) ? remainder : NextToken();
|
||||||
remainder.clear();
|
remainder.clear();
|
||||||
|
|
||||||
// token_t token1 = token;
|
// token_t token1 = token;
|
||||||
// token1.push_back('\0');
|
// token1.push_back('\0');
|
||||||
// std::cout << "> " << &token1[0] << std::endl;
|
// std::cout << "> " << &token1[0] << std::endl;
|
||||||
|
|
||||||
// End of file
|
// End of file
|
||||||
if (token.size() == 0) break;
|
if (token.size() == 0) break;
|
||||||
@@ -257,8 +260,9 @@ class Xml {
|
|||||||
else
|
else
|
||||||
BadFormat(token);
|
BadFormat(token);
|
||||||
token.push_back('\0');
|
token.push_back('\0');
|
||||||
} else
|
} else {
|
||||||
token[i] = '\0';
|
token[i] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
const char* tag = &token[ind];
|
const char* tag = &token[ind];
|
||||||
if (node_begin) {
|
if (node_begin) {
|
||||||
@@ -270,8 +274,9 @@ class Xml {
|
|||||||
}
|
}
|
||||||
UpLevel();
|
UpLevel();
|
||||||
}
|
}
|
||||||
} else
|
} else {
|
||||||
BadFormat(token);
|
BadFormat(token);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case DECL_STATE:
|
case DECL_STATE:
|
||||||
if (token[0] == '>') {
|
if (token[0] == '>') {
|
||||||
@@ -298,7 +303,7 @@ class Xml {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SpaceCheck() const {
|
bool SpaceCheck() const {
|
||||||
bool cond = ((buffer_[index_] == ' ') || (buffer_[index_] == ' '));
|
bool cond = ((buffer_[index_] == ' ') || (buffer_[index_] == '\t'));
|
||||||
return cond;
|
return cond;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,29 +328,32 @@ class Xml {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (data_size_ == 0) {
|
if (data_size_ == 0) {
|
||||||
data_size_ = read(fd_, buffer_, buf_size_);
|
data_size_ = read(fd_, buffer_, kBufSize);
|
||||||
if (data_size_ <= 0) break;
|
if (data_size_ <= 0) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token.empty())
|
if (token.empty()) {
|
||||||
while ((index_ < data_size_) && (SpaceCheck() || LineEndCheck())) {
|
while ((index_ < data_size_) && (SpaceCheck() || LineEndCheck())) {
|
||||||
++index_;
|
++index_;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while ((index_ < data_size_) && (in_string || !(SpaceCheck() || LineEndCheck()))) {
|
while ((index_ < data_size_) && (in_string || !(SpaceCheck() || LineEndCheck()))) {
|
||||||
const char symb = buffer_[index_];
|
const char symb = buffer_[index_];
|
||||||
bool skip_symb = false;
|
bool skip_symb = false;
|
||||||
|
|
||||||
switch (symb) {
|
switch (symb) {
|
||||||
case '\\':
|
case '\\':
|
||||||
if (special_symb) special_symb = false;
|
if (special_symb) {
|
||||||
else {
|
special_symb = false;
|
||||||
|
} else {
|
||||||
special_symb = true;
|
special_symb = true;
|
||||||
skip_symb = true;
|
skip_symb = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
if (special_symb) special_symb = false;
|
if (special_symb) {
|
||||||
else {
|
special_symb = false;
|
||||||
|
} else {
|
||||||
in_string = !in_string;
|
in_string = !in_string;
|
||||||
if (!in_string) {
|
if (!in_string) {
|
||||||
buffer_[index_] = ' ';
|
buffer_[index_] = ' ';
|
||||||
@@ -409,8 +417,8 @@ class Xml {
|
|||||||
unsigned file_line_;
|
unsigned file_line_;
|
||||||
int fd_;
|
int fd_;
|
||||||
|
|
||||||
static const unsigned buf_size_ = 256;
|
static const size_t kBufSize = 256;
|
||||||
char buffer_[buf_size_];
|
char buffer_[kBufSize];
|
||||||
|
|
||||||
unsigned data_size_;
|
unsigned data_size_;
|
||||||
unsigned index_;
|
unsigned index_;
|
||||||
|
|||||||
+1
-1
@@ -59,7 +59,7 @@ class Xml {
|
|||||||
xml = NULL;
|
xml = NULL;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
delete (ixml);
|
delete ixml;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (xml) {
|
if (xml) {
|
||||||
|
|||||||
Referencia en una nueva incidencia
Block a user