Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cartographer/common/configuration_file_resolver.h"
00018
00019 #include <fstream>
00020 #include <iostream>
00021 #include <streambuf>
00022
00023 #include "cartographer/common/config.h"
00024 #include "glog/logging.h"
00025
00026 namespace cartographer {
00027 namespace common {
00028
00029 ConfigurationFileResolver::ConfigurationFileResolver(
00030 const std::vector<std::string>& configuration_files_directories)
00031 : configuration_files_directories_(configuration_files_directories) {
00032 configuration_files_directories_.push_back(kConfigurationFilesDirectory);
00033 }
00034
00035 std::string ConfigurationFileResolver::GetFullPathOrDie(
00036 const std::string& basename) {
00037 for (const auto& path : configuration_files_directories_) {
00038 const std::string filename = path + "/" + basename;
00039 std::ifstream stream(filename.c_str());
00040 if (stream.good()) {
00041 LOG(INFO) << "Found '" << filename << "' for '" << basename << "'.";
00042 return filename;
00043 }
00044 }
00045 LOG(FATAL) << "File '" << basename << "' was not found.";
00046 }
00047
00048 std::string ConfigurationFileResolver::GetFileContentOrDie(
00049 const std::string& basename) {
00050 CHECK(!basename.empty()) << "File basename cannot be empty." << basename;
00051 const std::string filename = GetFullPathOrDie(basename);
00052 std::ifstream stream(filename.c_str());
00053 return std::string((std::istreambuf_iterator<char>(stream)),
00054 std::istreambuf_iterator<char>());
00055 }
00056
00057 }
00058 }