00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 #include "SundancePathUtils.hpp"
00032 #include "SundanceDefaultPath.hpp"
00033 #include <unistd.h>
00034 #ifndef _MSC_VER
00035 #include <sys/unistd.h>
00036 #endif
00037 
00038 using Teuchos::Array;
00039 
00040 using std::ifstream;
00041 
00042 namespace Sundance
00043 {
00044 string searchForFile(const std::string& name)
00045 {
00046   std::string pathSep = "/";
00047   Array<string> path = parsePathStr();
00048 
00049   if (name.length() && name[0]=='/') return name; 
00050   for (int i=0; i<path.size(); i++)
00051   {
00052     ifstream fileToTry((path[i] + pathSep + name).c_str());
00053     if (!fileToTry) continue;
00054     return path[i] + pathSep + name;
00055   }
00056 
00057   TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "could not find file "
00058     << name << " in path " << path);
00059 }
00060 
00061 string getPathStr() 
00062 {
00063   char* pathEnvStr = getenv("SUNDANCE_PATH");
00064   char* pyPathEnvStr = getenv("PYTHONPATH");
00065   std::string path;
00066   
00067   if (pathEnvStr == NULL) 
00068   {
00069     path = defaultSundancePath();
00070   }
00071   else
00072   {
00073     path = pathEnvStr;
00074   }
00075   if (pyPathEnvStr!=NULL)
00076   {
00077     path = std::string(pyPathEnvStr) + ":" + path; 
00078   }
00079   return path;
00080 }
00081 
00082 Array<string> parsePathStr() 
00083 {
00084   std::string pathStr = getPathStr();
00085   
00086   Array<string> rtn;
00087 
00088   unsigned int begin;
00089   unsigned int end;
00090   
00091   begin = pathStr.find_first_not_of(":");
00092   
00093   while (begin < pathStr.length())
00094   {
00095     end = pathStr.find_first_of(":", begin);
00096 
00097     rtn.append(pathStr.substr(begin, end-begin));
00098     begin = pathStr.find_first_not_of(":", end);
00099   }
00100 
00101   return rtn;
00102 }
00103 }