Line 16... |
Line 16... |
static const int64_t MIN_ALLOC_BYTES = 1 << 12;
|
static const int64_t MIN_ALLOC_BYTES = 1 << 12;
|
static AttributeType NilAttribute;
|
static AttributeType NilAttribute;
|
static AutoBuffer strBuffer;
|
static AutoBuffer strBuffer;
|
|
|
char *attribute_to_string(const AttributeType *attr);
|
char *attribute_to_string(const AttributeType *attr);
|
const char *string_to_attribute(const char *cfg, AttributeType *out);
|
int string_to_attribute(const char *cfg, int &off, AttributeType *out);
|
|
|
void AttributeType::attr_free() {
|
void AttributeType::attr_free() {
|
if (size()) {
|
if (size()) {
|
if (is_string()) {
|
if (is_string()) {
|
RISCV_free(u_.string);
|
RISCV_free(u_.string);
|
Line 383... |
Line 383... |
attribute_to_string(this);
|
attribute_to_string(this);
|
return strBuffer.getBuffer();
|
return strBuffer.getBuffer();
|
}
|
}
|
|
|
void AttributeType::from_config(const char *str) {
|
void AttributeType::from_config(const char *str) {
|
string_to_attribute(str, this);
|
int off = 0;
|
|
string_to_attribute(str, off, this);
|
}
|
}
|
|
|
char *attribute_to_string(const AttributeType *attr) {
|
char *attribute_to_string(const AttributeType *attr) {
|
IService *iserv;
|
IService *iserv;
|
AutoBuffer *buf = &strBuffer;
|
AutoBuffer *buf = &strBuffer;
|
Line 464... |
Line 465... |
buf->write_string(fstr);
|
buf->write_string(fstr);
|
}
|
}
|
return buf->getBuffer();
|
return buf->getBuffer();
|
}
|
}
|
|
|
const char *skip_special_symbols(const char *cfg) {
|
int skip_special_symbols(const char *cfg, int off) {
|
const char *pcur = cfg;
|
const char *pcur = &cfg[off];
|
while (*pcur == ' ' || *pcur == '\r' || *pcur == '\n' || *pcur == '\t') {
|
while (*pcur == ' ' || *pcur == '\r' || *pcur == '\n' || *pcur == '\t') {
|
pcur++;
|
pcur++;
|
|
off++;
|
}
|
}
|
return pcur;
|
return off;
|
}
|
}
|
|
|
const char *string_to_attribute(const char *cfg,
|
int string_to_attribute(const char *cfg, int &off,
|
AttributeType *out) {
|
AttributeType *out) {
|
const char *pcur = skip_special_symbols(cfg);
|
off = skip_special_symbols(cfg, off);
|
|
int checkstart = off;
|
if (pcur[0] == '\'' || pcur[0] == '"') {
|
if (cfg[off] == '\'' || cfg[off] == '"') {
|
AutoBuffer buf;
|
AutoBuffer buf;
|
uint8_t t1 = pcur[0];
|
uint8_t t1 = cfg[off];
|
int str_sz = 0;
|
int str_sz = 0;
|
pcur++;
|
const char *pcur = &cfg[++off];
|
while (*pcur != t1 && *pcur != '\0') {
|
while (*pcur != t1 && *pcur != '\0') {
|
pcur++;
|
pcur++;
|
str_sz++;
|
str_sz++;
|
}
|
}
|
buf.write_bin(&cfg[1], str_sz);
|
buf.write_bin(&cfg[off], str_sz);
|
pcur++;
|
|
out->make_string(buf.getBuffer());
|
out->make_string(buf.getBuffer());
|
} else if (pcur[0] == '[') {
|
off += str_sz;
|
pcur++;
|
if (cfg[off] != t1) {
|
pcur = skip_special_symbols(pcur);
|
RISCV_printf(NULL, LOG_ERROR,
|
|
"JSON parser error: Wrong string format");
|
|
out->attr_free();
|
|
return -1;
|
|
}
|
|
off = skip_special_symbols(cfg, off + 1);
|
|
} else if (cfg[off] == '[') {
|
|
off = skip_special_symbols(cfg, off + 1);
|
AttributeType new_item;
|
AttributeType new_item;
|
out->make_list(0);
|
out->make_list(0);
|
while (*pcur != ']' && *pcur != '\0') {
|
while (cfg[off] != ']' && cfg[off] != '\0') {
|
pcur = string_to_attribute(pcur, &new_item);
|
if (string_to_attribute(cfg, off, &new_item)) {
|
|
/* error handling */
|
|
out->attr_free();
|
|
return -1;
|
|
}
|
out->realloc_list(out->size() + 1);
|
out->realloc_list(out->size() + 1);
|
(*out)[out->size() - 1] = new_item;
|
(*out)[out->size() - 1] = new_item;
|
|
|
pcur = skip_special_symbols(pcur);
|
off = skip_special_symbols(cfg, off);
|
if (*pcur == ',') {
|
if (cfg[off] == ',') {
|
pcur++;
|
off = skip_special_symbols(cfg, off + 1);
|
pcur = skip_special_symbols(pcur);
|
|
}
|
}
|
}
|
}
|
pcur++;
|
if (cfg[off] != ']') {
|
pcur = skip_special_symbols(pcur);
|
RISCV_printf(NULL, LOG_ERROR,
|
} else if (pcur[0] == '{') {
|
"JSON parser error: Wrong list format");
|
|
out->attr_free();
|
|
return -1;
|
|
}
|
|
off = skip_special_symbols(cfg, off + 1);
|
|
} else if (cfg[off] == '{') {
|
AttributeType new_key;
|
AttributeType new_key;
|
AttributeType new_value;
|
AttributeType new_value;
|
out->make_dict();
|
out->make_dict();
|
|
off = skip_special_symbols(cfg, off + 1);
|
pcur++;
|
while (cfg[off] != '}' && cfg[off] != '\0') {
|
pcur = skip_special_symbols(pcur);
|
if (string_to_attribute(cfg, off, &new_key)) {
|
while (*pcur != '}' && *pcur != '\0') {
|
RISCV_printf(NULL, LOG_ERROR,
|
pcur = string_to_attribute(pcur, &new_key);
|
"JSON parser error: Wrong dictionary key");
|
pcur = skip_special_symbols(pcur);
|
out->attr_free();
|
if (*pcur == ':') {
|
return -1;
|
pcur++;
|
}
|
|
off = skip_special_symbols(cfg, off);
|
|
if (cfg[off] != ':') {
|
|
out->attr_free();
|
|
RISCV_printf(NULL, LOG_ERROR,
|
|
"JSON parser error: Wrong dictionary delimiter");
|
|
return -1;
|
|
}
|
|
off = skip_special_symbols(cfg, off + 1);
|
|
if (string_to_attribute(cfg, off, &new_value)) {
|
|
RISCV_printf(NULL, LOG_ERROR,
|
|
"JSON parser error: Wrong dictionary value");
|
|
out->attr_free();
|
|
return -1;
|
}
|
}
|
pcur = skip_special_symbols(pcur);
|
|
pcur = string_to_attribute(pcur, &new_value);
|
|
|
|
(*out)[new_key.to_string()] = new_value;
|
(*out)[new_key.to_string()] = new_value;
|
|
|
pcur = skip_special_symbols(pcur);
|
off = skip_special_symbols(cfg, off);
|
if (*pcur == ',') {
|
if (cfg[off] == ',') {
|
pcur++;
|
off = skip_special_symbols(cfg, off + 1);
|
pcur = skip_special_symbols(pcur);
|
|
}
|
}
|
}
|
}
|
pcur++;
|
if (cfg[off] != '}') {
|
pcur = skip_special_symbols(pcur);
|
RISCV_printf(NULL, LOG_ERROR,
|
|
"JSON parser error: Wrong dictionary format");
|
|
out->attr_free();
|
|
return -1;
|
|
}
|
|
off = skip_special_symbols(cfg, off + 1);
|
|
|
if (out->has_key("Type")) {
|
if (out->has_key("Type")) {
|
if (strcmp((*out)["Type"].to_string(), IFACE_SERVICE) == 0) {
|
if (strcmp((*out)["Type"].to_string(), IFACE_SERVICE) == 0) {
|
IService *iserv;
|
IService *iserv;
|
iserv = static_cast<IService *>(
|
iserv = static_cast<IService *>(
|
Line 545... |
Line 576... |
} else {
|
} else {
|
RISCV_printf(NULL, LOG_ERROR,
|
RISCV_printf(NULL, LOG_ERROR,
|
"Not implemented string to dict. attribute");
|
"Not implemented string to dict. attribute");
|
}
|
}
|
}
|
}
|
} else if (pcur[0] == '(') {
|
} else if (cfg[off] == '(') {
|
AutoBuffer buf;
|
AutoBuffer buf;
|
char byte_value;
|
char byte_value;
|
pcur++;
|
off = skip_special_symbols(cfg, off);
|
pcur = skip_special_symbols(pcur);
|
while (cfg[off] != ')' && cfg[off] != '\0') {
|
while (*pcur != ')' && *pcur != '\0') {
|
|
byte_value = 0;
|
byte_value = 0;
|
for (int n = 0; n < 2; n++) {
|
for (int n = 0; n < 2; n++) {
|
if (*pcur >= 'A' && *pcur <= 'F') {
|
if (cfg[off] >= 'A' && cfg[off] <= 'F') {
|
byte_value = (byte_value << 4) | ((*pcur - 'A') + 10);
|
byte_value = (byte_value << 4) | ((cfg[off] - 'A') + 10);
|
} else {
|
} else {
|
byte_value = (byte_value << 4) | (*pcur - '0');
|
byte_value = (byte_value << 4) | (cfg[off] - '0');
|
}
|
}
|
pcur++;
|
off++;
|
}
|
}
|
buf.write_bin(&byte_value, 1);
|
buf.write_bin(&byte_value, 1);
|
|
|
pcur = skip_special_symbols(pcur);
|
off = skip_special_symbols(cfg, off);
|
if (*pcur == ',') {
|
if (cfg[off] != ',') {
|
pcur++;
|
RISCV_printf(NULL, LOG_ERROR,
|
pcur = skip_special_symbols(pcur);
|
"JSON parser error: Wrong data dytes delimiter");
|
|
out->attr_free();
|
|
return -1;
|
|
}
|
|
off = skip_special_symbols(cfg, off + 1);
|
}
|
}
|
|
if (cfg[off] != ')') {
|
|
RISCV_printf(NULL, LOG_ERROR,
|
|
"JSON parser error: Wrong data format");
|
|
out->attr_free();
|
|
return -1;
|
}
|
}
|
out->make_data(buf.size(), buf.getBuffer());
|
out->make_data(buf.size(), buf.getBuffer());
|
pcur++;
|
off = skip_special_symbols(cfg, off + 1);
|
pcur = skip_special_symbols(pcur);
|
} else if (cfg[off] == 'N' && cfg[off + 1] == 'o' && cfg[off + 2] == 'n'
|
} else {
|
&& cfg[off + 3] == 'e') {
|
pcur = skip_special_symbols(pcur);
|
out->make_nil();
|
if (pcur[0] == 'N' && pcur[1] == 'o' && pcur[2] == 'n'
|
off = skip_special_symbols(cfg, off + 4);
|
&& pcur[3] == 'e') {
|
} else if (cfg[off] == 'f' && cfg[off + 1] == 'a' && cfg[off + 2] == 'l'
|
pcur += 4;
|
&& cfg[off + 3] == 's' && cfg[off + 4] == 'e') {
|
} else if (pcur[0] == 'f' && pcur[1] == 'a' && pcur[2] == 'l'
|
|
&& pcur[3] == 's' && pcur[4] == 'e') {
|
|
pcur += 5;
|
|
out->make_boolean(false);
|
out->make_boolean(false);
|
} else if (pcur[0] == 't' && pcur[1] == 'r' && pcur[2] == 'u'
|
off = skip_special_symbols(cfg, off + 5);
|
&& pcur[3] == 'e') {
|
} else if (cfg[off] == 't' && cfg[off + 1] == 'r' && cfg[off + 2] == 'u'
|
pcur += 4;
|
&& cfg[off + 3] == 'e') {
|
out->make_boolean(true);
|
out->make_boolean(true);
|
|
off = skip_special_symbols(cfg, off + 4);
|
} else {
|
} else {
|
char digits[64] = {0};
|
char digits[64] = {0};
|
int digits_cnt = 0;
|
int digits_cnt = 0;
|
if (pcur[0] == '0' && pcur[1] == 'x') {
|
bool negative = false;
|
pcur += 2;
|
if (cfg[off] == '0' && cfg[off + 1] == 'x') {
|
|
off += 2;
|
digits[digits_cnt++] = '0';
|
digits[digits_cnt++] = '0';
|
digits[digits_cnt++] = 'x';
|
digits[digits_cnt++] = 'x';
|
}
|
} else if (cfg[off] == '-') {
|
while ((*pcur >= '0' && *pcur <= '9')
|
negative = true;
|
|| (*pcur >= 'a' && *pcur <= 'f')
|
off++;
|
|| (*pcur >= 'A' && *pcur <= 'F')) {
|
}
|
digits[digits_cnt++] = *pcur++;
|
while (digits_cnt < 63 && ((cfg[off] >= '0' && cfg[off] <= '9')
|
|
|| (cfg[off] >= 'a' && cfg[off] <= 'f')
|
|
|| (cfg[off] >= 'A' && cfg[off] <= 'F'))) {
|
|
digits[digits_cnt++] = cfg[off++];
|
digits[digits_cnt] = 0;
|
digits[digits_cnt] = 0;
|
}
|
}
|
int64_t t1 = strtoull(digits, NULL, 0);
|
int64_t t1 = strtoull(digits, NULL, 0);
|
if (pcur[0] == '.') {
|
if (cfg[off] == '.') {
|
digits_cnt = 0;
|
digits_cnt = 0;
|
digits[0] = 0;
|
digits[0] = 0;
|
double divrate = 1.0;
|
double divrate = 1.0;
|
double d1 = static_cast<double>(t1);
|
double d1 = static_cast<double>(t1);
|
pcur++;
|
off++;
|
while (*pcur >= '0' && *pcur <= '9') {
|
while (digits_cnt < 63 && cfg[off] >= '0' && cfg[off] <= '9') {
|
digits[digits_cnt++] = *pcur++;
|
digits[digits_cnt++] = cfg[off++];
|
digits[digits_cnt] = 0;
|
digits[digits_cnt] = 0;
|
divrate *= 10.0;
|
divrate *= 10.0;
|
}
|
}
|
t1 = strtoull(digits, NULL, 0);
|
t1 = strtoull(digits, NULL, 0);
|
d1 += static_cast<double>(t1)/divrate;
|
d1 += static_cast<double>(t1)/divrate;
|
|
if (negative) {
|
|
d1 = -d1;
|
|
}
|
out->make_floating(d1);
|
out->make_floating(d1);
|
} else {
|
} else {
|
out->make_int64(t1);
|
if (negative) {
|
|
t1 = -t1;
|
}
|
}
|
|
out->make_int64(t1);
|
/** Guard to skip wrong formatted string and avoid hanging: */
|
|
while ((pcur[0] >= 'a' && pcur[0] <= 'z')
|
|
|| (pcur[0] >= 'A' && pcur[0] <= 'Z')) {
|
|
pcur++;
|
|
}
|
}
|
|
off = skip_special_symbols(cfg, off);
|
}
|
}
|
|
/** Guard to skip wrong formatted string and avoid hanging: */
|
|
if (off == checkstart) {
|
|
RISCV_printf(NULL, LOG_ERROR,
|
|
"JSON parser error: Can't detect format");
|
|
out->attr_free();
|
|
return -1;
|
}
|
}
|
return pcur;
|
return 0;
|
}
|
}
|
|
|
} // namespace debugger
|
} // namespace debugger
|
|
|
No newline at end of file
|
No newline at end of file
|