mirror of
https://github.com/hrydgard/ppsspp.git
synced 2025-02-25 00:22:10 +00:00

* Move and rename file_util/fd_util to Common/File/FileUtil and DirListing Let's also move net while we're at it. Move the ZIM/PNG loaders over to Common. Move the UI framework into Common iOS buildfix * Buildfix * Buildfixes * Apple buildfix * This typo again.. * UWP buildfix * Fix build of PPSSPPQt, such as it is (it's not in good condition...) * Guess what? Another buildfix.
125 lines
3.0 KiB
C++
125 lines
3.0 KiB
C++
#include "Common/File/VFS/VFS.h"
|
|
#include "Common/File/FileUtil.h"
|
|
#include "Common/Data/Format/JSONReader.h"
|
|
|
|
namespace json {
|
|
|
|
JsonReader::JsonReader(const std::string &filename) {
|
|
size_t buf_size;
|
|
buffer_ = (char *)VFSReadFile(filename.c_str(), &buf_size);
|
|
if (buffer_) {
|
|
parse();
|
|
} else {
|
|
// Okay, try to read on the local file system
|
|
buffer_ = (char *)ReadLocalFile(filename.c_str(), &buf_size);
|
|
if (buffer_) {
|
|
parse();
|
|
} else {
|
|
ERROR_LOG(IO, "Failed to read json file '%s'", filename.c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
int JsonGet::numChildren() const {
|
|
int count = 0;
|
|
if (value_.getTag() == JSON_OBJECT || value_.getTag() == JSON_ARRAY) {
|
|
for (auto it : value_) {
|
|
(void)it;
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
const JsonNode *JsonGet::get(const char *child_name) const {
|
|
if (!child_name) {
|
|
ERROR_LOG(IO, "JSON: Cannot get from null child name");
|
|
return nullptr;
|
|
}
|
|
if (value_.getTag() != JSON_OBJECT) {
|
|
return nullptr;
|
|
}
|
|
for (auto it : value_) {
|
|
if (!strcmp(it->key, child_name)) {
|
|
return it;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const JsonNode *JsonGet::get(const char *child_name, JsonTag type) const {
|
|
const JsonNode *v = get(child_name);
|
|
if (v && type == v->value.getTag())
|
|
return v;
|
|
return nullptr;
|
|
}
|
|
|
|
const char *JsonGet::getStringOrDie(const char *child_name) const {
|
|
const JsonNode *val = get(child_name, JSON_STRING);
|
|
if (val)
|
|
return val->value.toString();
|
|
ERROR_LOG(IO, "String '%s' missing from node", child_name);
|
|
return nullptr;
|
|
}
|
|
|
|
const char *JsonGet::getString(const char *child_name, const char *default_value) const {
|
|
const JsonNode *val = get(child_name, JSON_STRING);
|
|
if (!val)
|
|
return default_value;
|
|
return val->value.toString();
|
|
}
|
|
|
|
bool JsonGet::getStringVector(std::vector<std::string> *vec) const {
|
|
vec->clear();
|
|
if (value_.getTag() == JSON_ARRAY) {
|
|
for (auto it : value_) {
|
|
if (it->value.getTag() == JSON_STRING) {
|
|
vec->push_back(it->value.toString());
|
|
}
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
double JsonGet::getFloat(const char *child_name) const {
|
|
return get(child_name, JSON_NUMBER)->value.toNumber();
|
|
}
|
|
|
|
double JsonGet::getFloat(const char *child_name, double default_value) const {
|
|
const JsonNode *val = get(child_name, JSON_NUMBER);
|
|
if (!val)
|
|
return default_value;
|
|
return val->value.toNumber();
|
|
}
|
|
|
|
int JsonGet::getInt(const char *child_name) const {
|
|
return (int)get(child_name, JSON_NUMBER)->value.toNumber();
|
|
}
|
|
|
|
int JsonGet::getInt(const char *child_name, int default_value) const {
|
|
const JsonNode *val = get(child_name, JSON_NUMBER);
|
|
if (!val)
|
|
return default_value;
|
|
return (int)val->value.toNumber();
|
|
}
|
|
|
|
bool JsonGet::getBool(const char *child_name) const {
|
|
return get(child_name)->value.getTag() == JSON_TRUE;
|
|
}
|
|
|
|
bool JsonGet::getBool(const char *child_name, bool default_value) const {
|
|
const JsonNode *val = get(child_name);
|
|
if (val) {
|
|
JsonTag tag = val->value.getTag();
|
|
if (tag == JSON_TRUE)
|
|
return true;
|
|
if (tag == JSON_FALSE)
|
|
return false;
|
|
}
|
|
return default_value;
|
|
}
|
|
|
|
} // namespace json
|