SundancePathUtils.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
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; // Use absolute path!
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 }

Site Contact