Skip to content

Commit

Permalink
Add config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiSonoko committed Jun 14, 2017
1 parent 4876072 commit 178a1bf
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
67 changes: 67 additions & 0 deletions core/common/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef __RANDOM_HPP_
#define __RANDOM_HPP_

#include "config.hpp"

#include <random>
#include <fstream>

namespace common {

namespace config {
using namespace rapidjson;

ConfigLoader::ConfigLoader(const std::string& file_name){
std::ifstream ifs(file_name);
if(ifs.is_open()){
IStreamWrapper isw(ifs);
doc.ParseStream(isw);
if(doc.HasParseError()){
throw std::runtime_error("parse error");
}
}
}

int ConfigLoader::getIntOrElse(const std::string& key, int def){
if(doc[key.c_str()].IsInt()){
return doc[key.c_str()].GetInt();
}else{
return def;
};
}

std::string ConfigLoader::getStringOrElse(const std::string& key, std::string def){
if(doc[key.c_str()].IsString()){
return doc[key.c_str()].GetString();
}else{
return def;
};
}

bool ConfigLoader::getBoolOrElse(const std::string& key, bool def){
if(doc[key.c_str()].IsBool()){
return doc[key.c_str()].GetBool();
}else{
return def;
};
}
};
}; // namespace common

#endif
45 changes: 45 additions & 0 deletions core/common/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef __COMMON_CONFIG_HPP_
#define __COMMON_CONFIG_HPP_

#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/istreamwrapper.h>

#include <string>

namespace common {

namespace config {

using namespace rapidjson;

class ConfigLoader{
Document doc;
public:
ConfigLoader(const std::string& file);

int getIntOrElse(const std::string& key, int def);
std::string getStringOrElse(const std::string& key, std::string def);
bool getBoolOrElse(const std::string& key, bool def);

};
};
}; // namespace common

#endif // __COMMON_CONFIG_HPP_

0 comments on commit 178a1bf

Please sign in to comment.