Minor code improvements

This commit is contained in:
topjohnwu 2018-11-05 14:37:47 -05:00
parent 0742901cd2
commit 9fe5f37337
4 changed files with 23 additions and 23 deletions

View File

@ -14,6 +14,7 @@ struct prop_t {
prop_t() = default;
prop_t(const char *name) {
this->name = strdup(name);
value[0] = '\0';
}
prop_t(const char *name, const char *value) {
this->name = strdup(name);
@ -36,8 +37,7 @@ struct prop_t {
struct read_cb_t {
void (*cb)(const char *, const char *, void *);
void *arg;
read_cb_t() = default;
read_cb_t(void (*cb)(const char *, const char *, void *), void *arg)
read_cb_t(void (*cb)(const char *, const char *, void *) = nullptr, void *arg = nullptr)
: cb(cb), arg(arg) {}
void exec(const char *name, const char *value) {
cb(name, value, arg);
@ -49,7 +49,7 @@ struct read_cb_t {
extern bool use_pb;
char *persist_getprop(const char *name);
void persist_getprop_all(read_cb_t *read_cb);
void persist_getprop(read_cb_t *read_cb);
bool persist_deleteprop(const char *name);
void collect_props(const char *name, const char *value, void *v_plist);

View File

@ -87,7 +87,7 @@ const pb_field_t PersistentProperties_fields[2] = {
* ***************************/
static bool name_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
uint8_t *name = (uint8_t *) xmalloc(stream->bytes_left + 1);
auto name = new pb_byte_t[stream->bytes_left + 1];
name[stream->bytes_left] = '\0';
if (!pb_read(stream, name, stream->bytes_left))
return false;
@ -106,6 +106,7 @@ static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **ar
if (!pb_decode(stream, PersistentProperties_PersistentPropertyRecord_fields, &prop))
return false;
((read_cb_t *) *arg)->exec((const char *) prop.name.arg, prop.value);
delete[] (pb_byte_t *) prop.name.arg;
return true;
}
@ -143,19 +144,16 @@ static pb_ostream_t create_ostream(const char *filename) {
static void pb_getprop_cb(const char *name, const char *value, void *v) {
struct prop_t *prop = static_cast<prop_t *>(v);
if (prop->name && strcmp(name, prop->name) == 0) {
if (prop->name && strcmp(name, prop->name) == 0)
strcpy(prop->value, value);
free(prop->name);
prop->name = nullptr;
}
}
static void pb_getprop_all(read_cb_t *read_cb) {
static void pb_getprop(read_cb_t *read_cb) {
LOGD("resetprop: decode with protobuf [" PERSISTENT_PROPERTY_DIR "/persistent_properties]\n");
PersistentProperties props = {};
props.properties.funcs.decode = prop_decode;
props.properties.arg = read_cb;
uint8_t *buf;
pb_byte_t *buf;
size_t size;
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", (void **) &buf, &size);
pb_istream_t stream = pb_istream_from_buffer(buf, size);
@ -175,9 +173,9 @@ static void file_getprop(const char *name, char *value) {
close(fd);
}
void persist_getprop_all(read_cb_t *read_cb) {
void persist_getprop(read_cb_t *read_cb) {
if (use_pb) {
pb_getprop_all(read_cb);
pb_getprop(read_cb);
} else {
DIR *dir = opendir(PERSISTENT_PROPERTY_DIR);
struct dirent *entry;
@ -195,9 +193,9 @@ void persist_getprop_all(read_cb_t *read_cb) {
char *persist_getprop(const char *name) {
prop_t prop(name);
if (use_pb) {
struct read_cb_t read_cb(pb_getprop_cb, &prop);
pb_getprop_all(&read_cb);
if (prop.name == nullptr)
read_cb_t read_cb(pb_getprop_cb, &prop);
pb_getprop(&read_cb);
if (prop.value[0])
return strdup(prop.value);
} else {
// Try to read from file
@ -211,9 +209,9 @@ char *persist_getprop(const char *name) {
bool persist_deleteprop(const char *name) {
if (use_pb) {
auto prop_list = Array<prop_t>();
struct read_cb_t read_cb(collect_props, &prop_list);
persist_getprop_all(&read_cb);
Array<prop_t> prop_list;
read_cb_t read_cb(collect_props, &prop_list);
persist_getprop(&read_cb);
for (auto it = prop_list.begin(); it != prop_list.end(); ++it) {
if (strcmp((*it).name, name) == 0) {

View File

@ -113,7 +113,7 @@ static int init_resetprop() {
}
static void print_props(bool persist) {
auto prop_list = Array<prop_t>();
Array<prop_t> prop_list;
getprop(collect_props, &prop_list, persist);
prop_list.sort();
for (auto &prop : prop_list)
@ -159,7 +159,7 @@ void getprop(void (*callback)(const char *, const char *, void *), void *cookie,
__system_property_foreach(read_props, &read_cb);
if (persist) {
read_cb.cb = collect_unique_props;
persist_getprop_all(&read_cb);
persist_getprop(&read_cb);
}
}

View File

@ -69,9 +69,11 @@ public:
T& operator = (const T& a) {
_size = a._size;
_capacity = a._capacity;
_data = new T[_capacity];
for(int i = 0; i < _size; ++i)
_data[i] = (T&&) a[i];
if (_capacity) {
_data = new T[_capacity];
for(int i = 0; i < _size; ++i)
_data[i] = (T&&) a[i];
}
return *this;
}