Migrate to STL

This commit is contained in:
topjohnwu
2019-01-19 23:59:37 -05:00
parent 03c39e692a
commit 3e4c12cf56
23 changed files with 226 additions and 548 deletions

View File

@ -6,6 +6,8 @@
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <vector>
#include <algorithm>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include "private/_system_properties.h"
@ -17,6 +19,8 @@
#include "utils.h"
#include "flags.h"
using namespace std;
bool use_pb = false;
static bool verbose = false;
@ -74,12 +78,6 @@ illegal:
exit(1);
}
// Define the way to sort prop_t
template<>
int(*Vector<prop_t>::_cmp)(prop_t&, prop_t&) = [](auto a, auto b) -> int {
return strcmp(a.name, b.name);
};
static void read_props(const prop_info *pi, void *read_cb) {
__system_property_read_callback(
pi, [](auto cb, auto name, auto value, auto) -> void
@ -89,12 +87,12 @@ static void read_props(const prop_info *pi, void *read_cb) {
}
void collect_props(const char *name, const char *value, void *v_plist) {
Vector<prop_t> &prop_list = *static_cast<Vector<prop_t> *>(v_plist);
prop_list.push_back(prop_t(name, value));
auto &prop_list = *static_cast<vector<prop_t> *>(v_plist);
prop_list.emplace_back(name, value);
}
static void collect_unique_props(const char *name, const char *value, void *v_plist) {
Vector<prop_t> &prop_list = *static_cast<Vector<prop_t> *>(v_plist);
auto &prop_list = *static_cast<vector<prop_t> *>(v_plist);
for (auto &prop : prop_list) {
if (strcmp(name, prop.name) == 0)
return;
@ -112,9 +110,9 @@ static int init_resetprop() {
}
static void print_props(bool persist) {
Vector<prop_t> prop_list;
vector<prop_t> prop_list;
getprop(collect_props, &prop_list, persist);
prop_list.sort();
sort(prop_list.begin(), prop_list.end());
for (auto &prop : prop_list)
printf("[%s]: [%s]\n", prop.name, prop.value);
}
@ -128,19 +126,19 @@ int prop_exist(const char *name) {
return __system_property_find(name) != nullptr;
}
// Get prop by name, return string (should free manually!)
CharArray getprop(const char *name, bool persist) {
// Get prop by name, return string
string getprop(const char *name, bool persist) {
if (!check_legal_property_name(name) || init_resetprop())
return nullptr;
const prop_info *pi = __system_property_find(name);
if (pi == nullptr) {
if (persist && strncmp(name, "persist.", 8) == 0) {
CharArray value = persist_getprop(name);
if (!value.empty())
return value;
auto value = persist_getprop(name);
if (value.empty())
LOGD("resetprop: prop [%s] does not exist\n", name);
return value;
}
LOGD("resetprop: prop [%s] does not exist\n", name);
return CharArray();
return string();
} else {
char value[PROP_VALUE_MAX];
read_cb_t read_cb;
@ -162,7 +160,7 @@ void getprop(void (*callback)(const char *, const char *, void *), void *cookie,
}
}
int setprop(const char *name, const char *value, const bool trigger) {
int setprop(const char *name, const char *value, bool trigger) {
if (!check_legal_property_name(name))
return 1;
if (init_resetprop())
@ -170,7 +168,7 @@ int setprop(const char *name, const char *value, const bool trigger) {
int ret;
prop_info *pi = (prop_info*) __system_property_find(name);
auto pi = (prop_info*) __system_property_find(name);
if (pi != nullptr) {
if (trigger) {
if (strncmp(name, "ro.", 3) == 0) deleteprop(name);
@ -208,7 +206,7 @@ int deleteprop(const char *name, bool persist) {
return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0);
}
int load_prop_file(const char *filename, const bool trigger) {
int load_prop_file(const char *filename, bool trigger) {
if (init_resetprop()) return -1;
LOGD("resetprop: Load prop file [%s]\n", filename);
FILE *fp = xfopen(filename, "re");
@ -254,14 +252,14 @@ int resetprop_main(int argc, char *argv[]) {
bool trigger = true, persist = false;
char *argv0 = argv[0];
CharArray prop;
string prop;
--argc;
++argv;
// Parse flags and -- options
while (argc && argv[0][0] == '-') {
for (int idx = 1; 1; ++idx) {
for (int idx = 1; true; ++idx) {
switch (argv[0][idx]) {
case '-':
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
@ -275,10 +273,10 @@ int resetprop_main(int argc, char *argv[]) {
verbose = true;
continue;
case 'p':
persist = 1;
persist = true;
continue;
case 'n':
trigger = 0;
trigger = false;
continue;
case '\0':
break;
@ -297,8 +295,8 @@ int resetprop_main(int argc, char *argv[]) {
print_props(persist);
return 0;
case 1:
prop = utils::move(getprop(argv[0], persist));
if (!prop) return 1;
prop = getprop(argv[0], persist);
if (prop.empty()) return 1;
printf("%s\n", prop.c_str());
return 0;
case 2: