configuration_file_resolver.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 }  // namespace common
00058 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35