Line 1... |
Line 1... |
/**
|
/*
|
* @file
|
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
*
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* @brief Core attribute methods implementation.
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
*/
|
*/
|
|
|
#include "api_core.h"
|
#include <attribute.h>
|
#include "autobuffer.h"
|
#include <api_core.h>
|
#include "iservice.h"
|
#include <autobuffer.h>
|
#include "api_utils.h"
|
#include <iservice.h>
|
|
#include <api_utils.h>
|
#include <cstdlib>
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <algorithm>
|
|
|
namespace debugger {
|
namespace debugger {
|
|
|
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;
|
|
|
|
char *attribute_to_string(const AttributeType *attr);
|
void attribute_to_string(const AttributeType *attr, AutoBuffer *buf);
|
int string_to_attribute(const char *cfg, int &off, AttributeType *out);
|
int string_to_attribute(const char *cfg, int &off, AttributeType *out);
|
|
|
|
void AttributeType::allocAttrName(const char *name) {
|
|
size_t len = strlen(name) + 1;
|
|
attr_name_ = static_cast<char *>(RISCV_malloc(len));
|
|
memcpy(attr_name_, name, len);
|
|
}
|
|
|
|
void AttributeType::freeAttrName() {
|
|
if (attr_name_) {
|
|
RISCV_free(attr_name_);
|
|
}
|
|
}
|
|
|
|
void AttributeType::allocAttrDescription(const char *descr) {
|
|
size_t len = strlen(descr) + 1;
|
|
attr_descr_ = static_cast<char *>(RISCV_malloc(len));
|
|
memcpy(attr_descr_, descr, len);
|
|
}
|
|
|
|
void AttributeType::freeAttrDescription() {
|
|
if (attr_descr_) {
|
|
RISCV_free(attr_descr_);
|
|
}
|
|
}
|
|
|
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);
|
} else if (is_data() && size() > 8) {
|
} else if (is_data() && size() > 8) {
|
Line 175... |
Line 210... |
} else {
|
} else {
|
memcpy(u_.data_bytes, data, size);
|
memcpy(u_.data_bytes, data, size);
|
}
|
}
|
}
|
}
|
|
|
|
void AttributeType::realloc_data(unsigned size) {
|
|
if (!is_data()) {
|
|
return;
|
|
}
|
|
if (size <= 8) {
|
|
if (size_ > 8) {
|
|
memcpy(u_.data_bytes, u_.data, size);
|
|
RISCV_free(u_.data);
|
|
}
|
|
size_ = size;
|
|
return;
|
|
}
|
|
uint8_t *pnew = static_cast<uint8_t *>(RISCV_malloc(size));
|
|
unsigned sz = size;
|
|
if (size_ < sz) {
|
|
sz = size_;
|
|
}
|
|
if (sz > 8) {
|
|
memcpy(pnew, u_.data, sz);
|
|
RISCV_free(u_.data);
|
|
} else {
|
|
memcpy(pnew, u_.data_bytes, sz);
|
|
}
|
|
u_.data = pnew;
|
|
size_ = size;
|
|
}
|
|
|
void AttributeType::make_list(unsigned size) {
|
void AttributeType::make_list(unsigned size) {
|
attr_free();
|
attr_free();
|
kind_ = Attr_List;
|
kind_ = Attr_List;
|
if (size) {
|
if (size) {
|
realloc_list(size);
|
realloc_list(size);
|
Line 328... |
Line 390... |
quicksort(this, 0, static_cast<int>(size()) - 1, idx);
|
quicksort(this, 0, static_cast<int>(size()) - 1, idx);
|
}
|
}
|
|
|
bool AttributeType::has_key(const char *key) const {
|
bool AttributeType::has_key(const char *key) const {
|
for (unsigned i = 0; i < size(); i++) {
|
for (unsigned i = 0; i < size(); i++) {
|
if (strcmp(u_.dict[i].key_.to_string(), key) == 0
|
AttributePairType &pair = u_.dict[i];
|
&& !u_.dict[i].value_.is_nil()) {
|
if (pair.key_.is_equal(key) && !pair.value_.is_nil()) {
|
return true;
|
return true;
|
}
|
}
|
}
|
}
|
return false;
|
return false;
|
}
|
}
|
Line 376... |
Line 438... |
u_.dict = t1;
|
u_.dict = t1;
|
}
|
}
|
size_ = size;
|
size_ = size;
|
}
|
}
|
|
|
char *AttributeType::to_config() {
|
const AttributeType& AttributeType::to_config() {
|
strBuffer.clear();
|
AutoBuffer strBuffer;
|
attribute_to_string(this);
|
attribute_to_string(this, &strBuffer);
|
return strBuffer.getBuffer();
|
make_string(strBuffer.getBuffer());
|
|
return (*this);
|
}
|
}
|
|
|
void AttributeType::from_config(const char *str) {
|
void AttributeType::from_config(const char *str) {
|
int off = 0;
|
int off = 0;
|
string_to_attribute(str, off, this);
|
string_to_attribute(str, off, this);
|
}
|
}
|
|
|
char *attribute_to_string(const AttributeType *attr) {
|
void attribute_to_string(const AttributeType *attr, AutoBuffer *buf) {
|
IService *iserv;
|
IService *iserv;
|
AutoBuffer *buf = &strBuffer;
|
|
if (attr->is_nil()) {
|
if (attr->is_nil()) {
|
buf->write_string("None");
|
buf->write_string("None");
|
} else if (attr->is_int64() || attr->is_uint64()) {
|
} else if (attr->is_int64() || attr->is_uint64()) {
|
buf->write_uint64(attr->to_uint64());
|
buf->write_uint64(attr->to_uint64());
|
} else if (attr->is_string()) {
|
} else if (attr->is_string()) {
|
buf->write_string('\'');
|
buf->write_string('\'');
|
buf->write_string(attr->to_string());
|
buf->write_string(attr->to_string());
|
buf->write_string('\'');
|
buf->write_string('\'');
|
} else if (attr->is_bool()) {
|
} else if (attr->is_bool()) {
|
if (attr->to_bool()) {
|
if (attr->to_bool()) {
|
buf->write_string("true");
|
buf->write_string("True");
|
} else {
|
} else {
|
buf->write_string("false");
|
buf->write_string("False");
|
}
|
}
|
} else if (attr->is_list()) {
|
} else if (attr->is_list()) {
|
AttributeType list_item;
|
AttributeType list_item;
|
unsigned list_sz = attr->size();
|
unsigned list_sz = attr->size();
|
buf->write_string('[');
|
buf->write_string('[');
|
for (unsigned i = 0; i < list_sz; i++) {
|
for (unsigned i = 0; i < list_sz; i++) {
|
list_item = (*attr)[i];
|
list_item = (*attr)[i];
|
attribute_to_string(&list_item);
|
attribute_to_string(&list_item, buf);
|
if (i < (list_sz - 1)) {
|
if (i < (list_sz - 1)) {
|
buf->write_string(',');
|
buf->write_string(',');
|
}
|
}
|
}
|
}
|
buf->write_string(']');
|
buf->write_string(']');
|
Line 427... |
Line 489... |
buf->write_string('\'');
|
buf->write_string('\'');
|
buf->write_string(attr->u_.dict[i].key_.to_string());
|
buf->write_string(attr->u_.dict[i].key_.to_string());
|
buf->write_string('\'');
|
buf->write_string('\'');
|
buf->write_string(':');
|
buf->write_string(':');
|
const AttributeType &dict_value = (*attr)[i];
|
const AttributeType &dict_value = (*attr)[i];
|
attribute_to_string(&dict_value);
|
attribute_to_string(&dict_value, buf);
|
if (i < (dict_sz - 1)) {
|
if (i < (dict_sz - 1)) {
|
buf->write_string(',');
|
buf->write_string(',');
|
}
|
}
|
}
|
}
|
buf->write_string('}');
|
buf->write_string('}');
|
Line 462... |
Line 524... |
} else if (attr->is_floating()) {
|
} else if (attr->is_floating()) {
|
char fstr[64];
|
char fstr[64];
|
RISCV_sprintf(fstr, sizeof(fstr), "%.4f", attr->to_float());
|
RISCV_sprintf(fstr, sizeof(fstr), "%.4f", attr->to_float());
|
buf->write_string(fstr);
|
buf->write_string(fstr);
|
}
|
}
|
return buf->getBuffer();
|
|
}
|
}
|
|
|
int skip_special_symbols(const char *cfg, int off) {
|
int skip_special_symbols(const char *cfg, int off) {
|
const char *pcur = &cfg[off];
|
const char *pcur = &cfg[off];
|
while (*pcur == ' ' || *pcur == '\r' || *pcur == '\n' || *pcur == '\t') {
|
while (*pcur == ' ' || *pcur == '\r' || *pcur == '\n' || *pcur == '\t') {
|
Line 579... |
Line 640... |
}
|
}
|
}
|
}
|
} else if (cfg[off] == '(') {
|
} else if (cfg[off] == '(') {
|
AutoBuffer buf;
|
AutoBuffer buf;
|
char byte_value;
|
char byte_value;
|
off = skip_special_symbols(cfg, off);
|
off = skip_special_symbols(cfg, off + 1);
|
while (cfg[off] != ')' && cfg[off] != '\0') {
|
while (cfg[off] != ')' && cfg[off] != '\0') {
|
byte_value = 0;
|
byte_value = 0;
|
for (int n = 0; n < 2; n++) {
|
for (int n = 0; n < 2; n++) {
|
if (cfg[off] >= 'A' && cfg[off] <= 'F') {
|
if (cfg[off] >= 'A' && cfg[off] <= 'F') {
|
byte_value = (byte_value << 4) | ((cfg[off] - 'A') + 10);
|
byte_value = (byte_value << 4) | ((cfg[off] - 'A') + 10);
|
|
} else if (cfg[off] >= 'a' && cfg[off] <= 'f') {
|
|
byte_value = (byte_value << 4) | ((cfg[off] - 'a') + 10);
|
} else {
|
} else {
|
byte_value = (byte_value << 4) | (cfg[off] - '0');
|
byte_value = (byte_value << 4) | (cfg[off] - '0');
|
}
|
}
|
off++;
|
off++;
|
}
|
}
|
buf.write_bin(&byte_value, 1);
|
buf.write_bin(&byte_value, 1);
|
|
|
off = skip_special_symbols(cfg, off);
|
off = skip_special_symbols(cfg, off);
|
|
if (cfg[off] == ')') {
|
|
break;
|
|
}
|
if (cfg[off] != ',') {
|
if (cfg[off] != ',') {
|
RISCV_printf(NULL, LOG_ERROR,
|
RISCV_printf(NULL, LOG_ERROR,
|
"JSON parser error: Wrong data dytes delimiter");
|
"JSON parser error: Wrong data dytes delimiter");
|
out->attr_free();
|
out->attr_free();
|
return -1;
|
return -1;
|
Line 613... |
Line 679... |
off = skip_special_symbols(cfg, off + 1);
|
off = skip_special_symbols(cfg, off + 1);
|
} else if (cfg[off] == 'N' && cfg[off + 1] == 'o' && cfg[off + 2] == 'n'
|
} else if (cfg[off] == 'N' && cfg[off + 1] == 'o' && cfg[off + 2] == 'n'
|
&& cfg[off + 3] == 'e') {
|
&& cfg[off + 3] == 'e') {
|
out->make_nil();
|
out->make_nil();
|
off = skip_special_symbols(cfg, off + 4);
|
off = skip_special_symbols(cfg, off + 4);
|
} else if (cfg[off] == 'f' && cfg[off + 1] == 'a' && cfg[off + 2] == 'l'
|
} else if ((cfg[off] == 'f' || cfg[off] == 'F') && cfg[off + 1] == 'a'
|
&& cfg[off + 3] == 's' && cfg[off + 4] == 'e') {
|
&& cfg[off + 2] == 'l' && cfg[off + 3] == 's'
|
|
&& cfg[off + 4] == 'e') {
|
out->make_boolean(false);
|
out->make_boolean(false);
|
off = skip_special_symbols(cfg, off + 5);
|
off = skip_special_symbols(cfg, off + 5);
|
} else if (cfg[off] == 't' && cfg[off + 1] == 'r' && cfg[off + 2] == 'u'
|
} else if ((cfg[off] == 't' || cfg[off] == 'T') && cfg[off + 1] == 'r'
|
&& cfg[off + 3] == 'e') {
|
&& cfg[off + 2] == 'u' && cfg[off + 3] == 'e') {
|
out->make_boolean(true);
|
out->make_boolean(true);
|
off = skip_special_symbols(cfg, off + 4);
|
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;
|
Line 646... |
Line 713... |
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);
|
off++;
|
off++;
|
|
bool trim_zeros = true;
|
while (digits_cnt < 63 && cfg[off] >= '0' && cfg[off] <= '9') {
|
while (digits_cnt < 63 && cfg[off] >= '0' && cfg[off] <= '9') {
|
|
if (trim_zeros && cfg[off] == '0') {
|
|
off++;
|
|
divrate *= 10; // Fix: strtoull(0008) gives 0
|
|
continue;
|
|
}
|
|
trim_zeros = false;
|
digits[digits_cnt++] = cfg[off++];
|
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);
|