00001 /* 00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico 00003 00004 This file is part of MeRMaID. 00005 00006 MeRMaID is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 MeRMaID is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with MeRMaID. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 00021 00022 /** 00023 * @Filename FilePathSearch.cpp 00024 * @Description FilePathSearch implementation 00025 * @Status Work in Progress 00026 * @Version $Id: FilePathSearch.cpp 1 2011-03-04 18:13:18Z jreis $ 00027 * @Maintainer Marco Barbosa 00028 */ 00029 00030 #include "config.h" 00031 00032 #include "FilePathSearch.hpp" 00033 00034 #include <fstream> 00035 #include <iostream> 00036 00037 #include <Exception.hpp> 00038 00039 using namespace mermaid::support::system; 00040 using mermaid::support::errorhandling::Exception; 00041 00042 std::list<std::string> FilePathSearch::pathList_ = std::list<std::string>(); 00043 00044 void FilePathSearch::addSearchPath (std::string path) 00045 { 00046 //std::cerr << "FilePathSearch::addSearchPath : path=" << path << std::endl; 00047 pathList_.push_back (path); 00048 }; 00049 00050 std::string FilePathSearch::getFullPathForFile (std::string filename) 00051 { 00052 std::list<std::string>::iterator pathIt; 00053 00054 for (pathIt = pathList_.begin(); pathIt != pathList_.end(); pathIt++) { 00055 std::string path = *pathIt; 00056 00057 //std::cerr << "FilePathSearch::getFullPathForFile : trying path: \"" << path << "\"" << std::endl; 00058 00059 std::string fullPath = path + "/" + filename; 00060 00061 //std::cerr << "FilePathSearch::getFullPathForFile : full path: \"" << fullPath << "\"" << std::endl; 00062 00063 std::ifstream stream; 00064 00065 stream.open (fullPath.c_str()); 00066 00067 if (stream.is_open()) { 00068 //std::cerr << "FilePathSearch::getFullPathForFile : found path: \"" << fullPath << "\"" << std::endl; 00069 stream.close(); 00070 return fullPath; 00071 } 00072 else { 00073 //std::cerr << "FilePathSearch::getFullPathForFile : invalid path: \"" << fullPath << "\"" << std::endl; 00074 } 00075 } 00076 00077 throw Exception ("FilePathSearch::getFullPathForFile : path not found for filename: " + filename); 00078 };