Files
rathena/src/common/yamlwrapper.cpp
Jittapan Pluemsumran 4a2574c599 Migrate core source files to C++. Add yaml-cpp project as dependency (#2207)
By updating to this commit, Linux users have to regenerate Makefiles by re-running the configure script.

* Added yaml-cpp project as a 3rdparty library
* Migrated source files core to C++
* Updated configure scripts
* Make Linux installations compile *.cpp files
* Made server components' main source file C++
* Also made headers CPP-aware.

* Added basic C wrapper for yaml-cpp library
* YAML-node path is delimited by periods.
* Basic integer types and string are supported.
* Strings returned from this wrapper have to be freed with malloc.h::aFree
* Arrays (sequence) is supported with iterator wrapper.
* Remember to free every wrapper you create!

* Add yaml-cpp as dependency of common project
* Made the repo not ignore *.yml files

Thanks to @aleos89 and @Lemongrass3110.
2017-06-25 23:33:24 +07:00

146 lines
3.8 KiB
C++

/**
* This file is a part of rAthena++.
* Copyright(C) 2017 rAthena Development Team
* https://rathena.org - https://github.com/rathena
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author(s)
* Jittapan Pluemsumran <secret@rathena.org>
*/
#include "yamlwrapper.h"
#include <cstring>
#include "malloc.h"
#include "showmsg.h"
extern "C" {
yamlwrapper::yamlwrapper(YAML::Node node) {
this->root = node;
}
yamliterator::yamliterator(YAML::Node sequence) {
this->sequence = sequence;
this->index = 0;
}
yamliterator* yamlwrapper::iterator() {
return new yamliterator(this->root);
}
yamlwrapper* yaml_load_file(const char* file_name) {
YAML::Node node = YAML::LoadFile(file_name);
if (!node.IsDefined())
return NULL;
return new yamlwrapper(node);
}
extern "C++" YAML::Node yaml_get_node(YAML::Node& node, std::string& key) {
if (key.empty())
return node;
size_t pos = key.find('.');
if (pos == std::string::npos)
return node[key];
else
return yaml_get_node(node[key.substr(0, pos)], key.substr(pos+1));
}
void yaml_destroy_wrapper(yamlwrapper* wrapper) {
delete wrapper;
}
char* yaml_get_c_string(yamlwrapper* wrapper, const char* key) {
std::string cpp_str = yaml_get_node(wrapper->root, std::string(key)).as<std::string>();
const char* c_str = cpp_str.c_str();
size_t str_size = std::strlen(c_str) + 1;
char* buf = (char*)aCalloc(1, str_size);
strcpy(buf, c_str);
return buf;
}
extern "C++" {
template<typename T>
T yaml_get_value(yamlwrapper* wrapper, const char* key) {
if (wrapper == nullptr || key == nullptr)
return {};
try {
return yaml_get_node(wrapper->root, std::string(key)).as<T>();
}
catch (const std::exception& e) {
ShowError("Error during YAML node value resolving in node %s.\n", e.what());
return {};
}
}
}
int yaml_get_int(yamlwrapper* wrapper, const char* key) {
return yaml_get_value<int>(wrapper, key);
}
int16 yaml_get_int16(yamlwrapper* wrapper, const char* key) {
return yaml_get_value<int16>(wrapper, key);
}
int32 yaml_get_int32(yamlwrapper* wrapper, const char* key) {
return yaml_get_value<int32>(wrapper, key);
}
int64 yaml_get_int64(yamlwrapper* wrapper, const char* key) {
return yaml_get_value<int64>(wrapper, key);
}
bool yaml_get_boolean(yamlwrapper* wrapper, const char* key) {
return yaml_get_value<bool>(wrapper, key);
}
bool yaml_node_is_defined(yamlwrapper* wrapper, const char* key) {
if (wrapper == nullptr || key == nullptr)
return false;
return yaml_get_node(wrapper->root, std::string(key)).IsDefined();
}
yamlwrapper* yaml_get_subnode(yamlwrapper* wrapper, const char* key) {
return new yamlwrapper(yaml_get_node(wrapper->root, std::string(key)));
}
yamliterator* yaml_get_iterator(yamlwrapper* wrapper) {
return new yamliterator(wrapper->root);
}
bool yaml_iterator_is_valid(yamliterator* it) {
return it->sequence.IsSequence();
}
yamlwrapper* yaml_iterator_first(yamliterator* it) {
it->index++;
return new yamlwrapper(it->sequence[0]);
}
yamlwrapper* yaml_iterator_next(yamliterator* it) {
return new yamlwrapper(it->sequence[it->index++]);
}
bool yaml_iterator_has_next(yamliterator* it) {
return it->index <= it->sequence.size();
}
void yaml_iterator_destroy(yamliterator* it) {
delete it;
}
} /* extern "C" */