libjansson replaced to rapidjson.
Sync changes with proxy.
This commit is contained in:
parent
4cf3bb9930
commit
af51513614
78 changed files with 15550 additions and 6420 deletions
110
src/Options.cpp
110
src/Options.cpp
|
@ -22,7 +22,6 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <string.h>
|
||||
#include <uv.h>
|
||||
|
||||
|
@ -39,6 +38,9 @@
|
|||
#include "net/Url.h"
|
||||
#include "Options.h"
|
||||
#include "Platform.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/error/en.h"
|
||||
#include "rapidjson/filereadstream.h"
|
||||
#include "version.h"
|
||||
|
||||
|
||||
|
@ -268,6 +270,35 @@ Options::~Options()
|
|||
}
|
||||
|
||||
|
||||
bool Options::getJSON(const char *fileName, rapidjson::Document &doc)
|
||||
{
|
||||
uv_fs_t req;
|
||||
const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd));
|
||||
return false;
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(&req);
|
||||
|
||||
FILE *fp = fdopen(fd, "rb");
|
||||
char buf[8192];
|
||||
rapidjson::FileReadStream is(fp, buf, sizeof(buf));
|
||||
|
||||
doc.ParseStream(is);
|
||||
|
||||
uv_fs_close(uv_default_loop(), &req, fd, nullptr);
|
||||
uv_fs_req_cleanup(&req);
|
||||
|
||||
if (doc.HasParseError()) {
|
||||
printf("%s:%d: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return doc.IsObject();
|
||||
}
|
||||
|
||||
|
||||
bool Options::parseArg(int key, const char *arg)
|
||||
{
|
||||
switch (key) {
|
||||
|
@ -529,85 +560,53 @@ Url *Options::parseUrl(const char *arg) const
|
|||
|
||||
void Options::parseConfig(const char *fileName)
|
||||
{
|
||||
uv_fs_t req;
|
||||
const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd));
|
||||
return;
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(&req);
|
||||
|
||||
json_error_t err;
|
||||
json_t *config = json_loadfd(fd, 0, &err);
|
||||
|
||||
uv_fs_close(uv_default_loop(), &req, fd, nullptr);
|
||||
uv_fs_req_cleanup(&req);
|
||||
|
||||
if (!json_is_object(config)) {
|
||||
if (config) {
|
||||
json_decref(config);
|
||||
return;
|
||||
}
|
||||
|
||||
if (err.line < 0) {
|
||||
fprintf(stderr, "%s\n", err.text);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "%s:%d: %s\n", fileName, err.line, err.text);
|
||||
}
|
||||
|
||||
rapidjson::Document doc;
|
||||
if (!getJSON(fileName, doc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(config_options); i++) {
|
||||
parseJSON(&config_options[i], config);
|
||||
parseJSON(&config_options[i], doc);
|
||||
}
|
||||
|
||||
json_t *pools = json_object_get(config, "pools");
|
||||
if (json_is_array(pools)) {
|
||||
size_t index;
|
||||
json_t *value;
|
||||
const rapidjson::Value &pools = doc["pools"];
|
||||
if (pools.IsArray()) {
|
||||
for (const rapidjson::Value &value : pools.GetArray()) {
|
||||
if (!value.IsObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
json_array_foreach(pools, index, value) {
|
||||
if (json_is_object(value)) {
|
||||
for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) {
|
||||
parseJSON(&pool_options[i], value);
|
||||
}
|
||||
for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) {
|
||||
parseJSON(&pool_options[i], value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
json_t *api = json_object_get(config, "api");
|
||||
if (json_is_object(api)) {
|
||||
const rapidjson::Value &api = doc["api"];
|
||||
if (api.IsObject()) {
|
||||
for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) {
|
||||
parseJSON(&api_options[i], api);
|
||||
}
|
||||
}
|
||||
|
||||
json_decref(config);
|
||||
}
|
||||
|
||||
|
||||
void Options::parseJSON(const struct option *option, json_t *object)
|
||||
void Options::parseJSON(const struct option *option, const rapidjson::Value &object)
|
||||
{
|
||||
if (!option->name) {
|
||||
if (!option->name || !object.HasMember(option->name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
json_t *val = json_object_get(object, option->name);
|
||||
if (!val) {
|
||||
return;
|
||||
}
|
||||
const rapidjson::Value &value = object[option->name];
|
||||
|
||||
if (option->has_arg && json_is_string(val)) {
|
||||
parseArg(option->val, json_string_value(val));
|
||||
if (option->has_arg && value.IsString()) {
|
||||
parseArg(option->val, value.GetString());
|
||||
}
|
||||
else if (option->has_arg && json_is_integer(val)) {
|
||||
parseArg(option->val, json_integer_value(val));
|
||||
else if (option->has_arg && value.IsUint64()) {
|
||||
parseArg(option->val, value.GetUint64());
|
||||
}
|
||||
else if (!option->has_arg && json_is_boolean(val)) {
|
||||
parseBoolean(option->val, json_is_true(val));
|
||||
else if (!option->has_arg && value.IsBool()) {
|
||||
parseBoolean(option->val, value.IsTrue());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -652,7 +651,6 @@ void Options::showVersion()
|
|||
"\n");
|
||||
|
||||
printf("\nlibuv/%s\n", uv_version_string());
|
||||
printf("libjansson/%s\n", JANSSON_VERSION);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue