*/
struct ConfigException: public std::runtime_error {
- ConfigException(const std::string& error = "") : std::runtime_error(error) {}
+ ConfigException(const std::string& error) : std::runtime_error(error) {}
};
} // namespace config
{
json_object* object = nullptr;
if (!json_object_object_get_ex(mObject, name.c_str(), &object)) {
- throw ConfigException("Missing field '" + name + "' in json");
+ throw ConfigException("Missing field '" + name + "'");
}
fromJsonObject(object, value);
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
+ * @brief Filesystem helper functions
+ */
+
+#include "config/fs-utils.hpp"
+
+#include <fstream>
+#include <streambuf>
+
+
+namespace config {
+namespace fsutils {
+
+bool readFileContent(const std::string& path, std::string& result)
+{
+ std::ifstream file(path);
+
+ if (!file) {
+ return false;
+ }
+
+ file.seekg(0, std::ios::end);
+ std::streampos length = file.tellg();
+ if (length < 0) {
+ return false;
+ }
+ result.resize(static_cast<size_t>(length));
+ file.seekg(0, std::ios::beg);
+
+ file.read(&result[0], length);
+ if (!file) {
+ result.clear();
+ return false;
+ }
+
+ return true;
+}
+
+bool saveFileContent(const std::string& path, const std::string& content)
+{
+ std::ofstream file(path);
+ if (!file) {
+ return false;
+ }
+ file.write(content.data(), static_cast<std::streamsize>(content.size()));
+ if (!file) {
+ return false;
+ }
+ return true;
+}
+
+} // namespace fsutils
+} // namespace config
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
+ * @brief src/config/manager.hpp
+ */
+
+#ifndef CONFIG_FS_UTILS_HPP
+#define CONFIG_FS_UTILS_HPP
+
+#include <string>
+
+namespace config {
+namespace fsutils {
+
+bool readFileContent(const std::string& path, std::string& result);
+bool saveFileContent(const std::string& path, const std::string& content);
+
+} // namespace fsutils
+} // namespace config
+
+#endif // CONFIG_FS_UTILS_HPP
#include "config/to-json-visitor.hpp"
#include "config/from-json-visitor.hpp"
#include "config/is-visitable.hpp"
-#include "logger/logger.hpp"
-
-#include <fstream>
-#include <streambuf>
+#include "config/fs-utils.hpp"
namespace config {
return visitor.toString();
}
-static bool readFileContent(const std::string& path, std::string& result)
-{
- std::ifstream file(path);
-
- if (!file) {
- LOGD(path << ": could not open for reading");
- return false;
- }
-
- file.seekg(0, std::ios::end);
- std::streampos length = file.tellg();
- if (length < 0) {
- LOGD(path << ": tellg failed");
- return false;
- }
- result.resize(static_cast<size_t>(length));
- file.seekg(0, std::ios::beg);
-
- file.read(&result[0], length);
- if (!file) {
- LOGD(path << ": read error");
- result.clear();
- return false;
- }
-
- return true;
-}
-
-static bool saveFileContent(const std::string& path, const std::string& content)
-{
- std::ofstream file(path);
- if (!file) {
- LOGD(path << ": could not open for writing");
- return false;
- }
- file.write(content.data(), content.size());
- if (!file) {
- LOGD(path << ": could not write to");
- return false;
- }
- return true;
-}
-
template <class Config>
void loadFromFile(const std::string& filename, Config& config)
{
+ std::string content;
+ if (!fsutils::readFileContent(filename, content)) {
+ throw ConfigException("Could not load " + filename);
+ }
try {
- std::string content;
- if (!readFileContent(filename, content)) {
- throw ConfigException("Could not load " + filename);
- }
loadFromString(content, config);
- }
- catch(...) {
- throw ConfigException("Could not load configuration.");
+ } catch (ConfigException& e) {
+ throw ConfigException("Error in " + filename + ": " + e.what());
}
}
void saveToFile(const std::string& filename, const Config& config)
{
const std::string content = saveToString(config);
- if (!saveFileContent(filename, content)) {
+ if (!fsutils::saveFileContent(filename, content)) {
throw ConfigException("Could not save " + filename);
}
}