FileRepository.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include <sofa/helper/system/FileRepository.h>
00028 #include <sofa/helper/system/SetDirectory.h>
00029 
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #ifndef WIN32
00033 #include <unistd.h>
00034 #else
00035 #include <windows.h>
00036 #include <direct.h>
00037 #endif
00038 
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <iostream>
00042 
00043 namespace sofa
00044 {
00045 
00046 namespace helper
00047 {
00048 
00049 namespace system
00050 {
00051 
00052 #if defined (WIN32)
00053 FileRepository DataRepository("SOFA_DATA_PATH", "../share;../examples");
00054 #elif defined (__APPLE__)
00055 FileRepository DataRepository("SOFA_DATA_PATH", "../share:../examples:../Resources/examples:../Resources:../../../../examples:../../../../share");
00056 #else
00057 FileRepository DataRepository("SOFA_DATA_PATH", "../share:../examples");
00058 #endif
00059 
00060 FileRepository::FileRepository(const char* envVar, const char* relativePath)
00061 {
00062     if (envVar != NULL && envVar[0]!='\0')
00063     {
00064         const char* envpath = getenv(envVar);
00065         if (envpath != NULL && envpath[0]!='\0')
00066             addFirstPath(envpath);
00067     }
00068     if (relativePath != NULL && relativePath[0]!='\0')
00069     {
00070         std::string path = relativePath;
00071         size_t p0 = 0;
00072         size_t p1;
00073         while ( p0 < path.size() )
00074         {
00075             p1 = path.find(entrySeparator(),p0);
00076             if (p1 == std::string::npos) p1 = path.size();
00077             if (p1>p0+1)
00078             {
00079                 std::string p = path.substr(p0,p1-p0);
00080                 addLastPath(SetDirectory::GetRelativeFromProcess(p.c_str()));
00081             }
00082             p0 = p1+1;
00083         }
00084     }
00085     //print();
00086 }
00087 
00088 FileRepository::~FileRepository()
00089 {
00090 }
00091 
00092 void FileRepository::addFirstPath(const std::string& path)
00093 {
00094     std::vector<std::string> entries;
00095     size_t p0 = 0;
00096     size_t p1;
00097     while ( p0 < path.size() )
00098     {
00099         p1 = path.find(entrySeparator(),p0);
00100         if (p1 == std::string::npos) p1 = path.size();
00101         if (p1>p0+1)
00102         {
00103             entries.push_back(path.substr(p0,p1-p0));
00104         }
00105         p0 = p1+1;
00106     }
00107     vpath.insert(vpath.begin(), entries.begin(), entries.end());
00108 }
00109 
00110 void FileRepository::addLastPath(const std::string& path)
00111 {
00112     std::vector<std::string> entries;
00113     size_t p0 = 0;
00114     size_t p1;
00115     while ( p0 < path.size() )
00116     {
00117         p1 = path.find(entrySeparator(),p0);
00118         if (p1 == std::string::npos) p1 = path.size();
00119         if (p1>p0+1)
00120         {
00121             entries.push_back(path.substr(p0,p1-p0));
00122         }
00123         p0 = p1+1;
00124     }
00125     vpath.insert(vpath.end(), entries.begin(), entries.end());
00126 //     std::cout << path << std::endl;
00127 }
00128 
00129 std::string FileRepository::getFirstPath()
00130 {
00131     if (vpath.size() > 0)
00132         return vpath[0];
00133     else return "";
00134 }
00135 
00136 bool FileRepository::findFileIn(std::string& filename, const std::string& path)
00137 {
00138     if (filename.empty()) return false; // no filename
00139     struct stat s;
00140     std::string newfname = SetDirectory::GetRelativeFromDir(filename.c_str(), path.c_str());
00141     //std::cout << "Looking for " << newfname <<std::endl;
00142     if (!stat(newfname.c_str(),&s))
00143     {
00144         // File found
00145         //std::cout << "File "<<filename<<" found in "<<path.substr(p0,p1-p0)<<std::endl;
00146         filename = newfname;
00147         return true;
00148     }
00149     return false;
00150 }
00151 
00152 bool FileRepository::findFile(std::string& filename, const std::string& basedir)
00153 {
00154     if (filename.empty()) return false; // no filename
00155     if (basedir.empty())
00156     {
00157         if (findFileIn(filename, SetDirectory::GetCurrentDir())) return true;
00158     }
00159     else
00160     {
00161         if (findFileIn(filename, SetDirectory::GetRelativeFromDir(basedir.c_str(),SetDirectory::GetCurrentDir().c_str()))) return true;
00162     }
00163     if (SetDirectory::IsAbsolute(filename)) return false; // absolute file path
00164     if (filename.substr(0,2)=="./" || filename.substr(0,3)=="../") return false; // local file path
00165     for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
00166         if (findFileIn(filename, *it)) return true;
00167     std::cerr << "File "<<filename<<" NOT FOUND in "<<basedir;
00168     for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
00169       std::cerr << ':'<<*it;
00170     std::cerr<<std::endl;
00171     return false;
00172 }
00173 
00174 bool FileRepository::findFileFromFile(std::string& filename, const std::string& basefile)
00175 {
00176     return findFile(filename, SetDirectory::GetParentDir(basefile.c_str()));
00177 }
00178 
00179 void FileRepository::print()
00180 {
00181     for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
00182         std::cout << *it << std::endl;
00183 }
00184 
00185 } // namespace system
00186 
00187 } // namespace helper
00188 
00189 } // namespace sofa
00190 

Generated on Sun Nov 23 02:29:30 2008 for Sofa by  doxygen 1.5.7.1