jccl::ParseUtil Class Reference

Utility functions for handling the parsing of input files. More...

#include <jccl/Config/ParseUtil.h>

Collaboration diagram for jccl::ParseUtil:

Collaboration graph
[legend]
List of all members.

Static Public Member Functions

static std::string expandFileName (const std::string &name, const std::string &parentFile)
 Expands a filename to a string capable of being passed to an I/O system call.
static void setCfgSearchPath (const std::string &path)
 Replaces the current configuration file search path with the given string.

Static Private Member Functions

static vpr::ReturnStatus findFileUsingPath (const std::string &fileName, std::string &absoluteFile)
 Looks for a file in a path and opens it if found.

Static Private Attributes

static SearchPathData mSearchInfo

Classes

struct  SearchPathData
 Simple helper type to ensure that the static data in this class is initialized at the same time. More...

Detailed Description

Utility functions for handling the parsing of input files.

Definition at line 48 of file ParseUtil.h.


Member Function Documentation

std::string jccl::ParseUtil::expandFileName ( const std::string &  name,
const std::string &  parentFile 
) [static]

Expands a filename to a string capable of being passed to an I/O system call.

The result may be an absolute path depending on various factors.

Postcondition:
All environment variables used in name are expanded. If name is not an absolute path and parentFile is not an empty string, the path of parentFile will be prepended to the result. If parentFile is an empty string, the search path will be used to try to find the named file.
Parameters:
name The name to expand.
parentFile The name of the file that n is being loaded relative to.
Returns:
An expanded version of the string name. All environment
Note:
parentFile is always assumed to refer to a file, so if name is "included_file" and parentFile is "mydir/including_file", the result will be "mydir/included_file".

Definition at line 55 of file ParseUtil.cpp.

References findFileUsingPath(), and jcclDBG_CONFIG().

Referenced by jccl::ElementFactory::loadDef(), and jccl::ElementFactory::loadDefs().

00057 {
00058    std::string fname = vpr::replaceEnvVars(name);
00059 
00060    fs::path fname_path(fname, fs::native);
00061 
00062    // If the given file name does not exist, try searching for it.
00063    if ( ! fs::exists(fname_path) )
00064    {
00065       // If the given file name is not absolute, look it up relative to
00066       // parentfile or using the config file search path.
00067       if ( ! fname_path.is_complete() )
00068       {
00069          // If the parentfile string is empty, we'll use the search path to
00070          // find the file.
00071          if ( parentfile.empty() )
00072          {
00073             vprDEBUG(jcclDBG_CONFIG, vprDBG_STATE_LVL)
00074                << "[jccl::ParseUtil::expandFileName()] Looking up '" << fname
00075                << "' using search path ...\n" << vprDEBUG_FLUSH;
00076 
00077             std::string absolute_fname;
00078             if ( findFileUsingPath(fname, absolute_fname).success() )
00079             {
00080                fname_path = fs::path(absolute_fname, fs::native);
00081             }
00082          }
00083          // parentfile is not empty, so we assume that fname is relative to
00084          // parentfile.
00085          else
00086          {
00087             fs::path parentfile_path(parentfile, fs::native);
00088             fs::path parentdir = parentfile_path.branch_path();
00089             fname_path = parentdir / fname_path;
00090          }
00091       }
00092    }
00093 
00094    return fname_path.native_file_string();
00095 }

void jccl::ParseUtil::setCfgSearchPath ( const std::string &  path  )  [static]

Replaces the current configuration file search path with the given string.

Parameters:
path A string of directories separted by a platform-specific path separation character. On Windows, this character is ';', and on UNIX and Mac OS X, this character is ':'.

Definition at line 97 of file ParseUtil.cpp.

References mSearchInfo, and jccl::ParseUtil::SearchPathData::setSearchPath().

00098 {
00099    mSearchInfo.setSearchPath(path);
00100 }

vpr::ReturnStatus jccl::ParseUtil::findFileUsingPath ( const std::string &  fileName,
std::string &  absoluteFile 
) [static, private]

Looks for a file in a path and opens it if found.

Parameters:
fileName The name of a file to search for.
absoluteFile Storage for the absolute path to the file if it was found in the path.
Returns:
vpr::ReturnStatus::Succeed is returned if a file is found in the path; vpr::ReturnStatus::Fail otherwise.

Definition at line 102 of file ParseUtil.cpp.

References jcclDBG_CONFIG(), jccl::ParseUtil::SearchPathData::mPath, and mSearchInfo.

Referenced by expandFileName().

00104 {
00105    vpr::ReturnStatus status(vpr::ReturnStatus::Fail);
00106    fs::path filename_path(fileName, fs::native);
00107 
00108    for ( std::vector<std::string>::iterator i = mSearchInfo.mPath.begin();
00109          i != mSearchInfo.mPath.end();
00110          ++i )
00111    {
00112       fs::path full_path = fs::path(*i, fs::native) / filename_path;
00113 
00114       vprDEBUG(jcclDBG_CONFIG, vprDBG_HVERB_LVL)
00115          << "[jccl::ParseUtil::findFileUsingPath()] Attempting to open file '"
00116          << full_path.native_file_string() << "'\n" << vprDEBUG_FLUSH;
00117 
00118       try
00119       {
00120          if ( fs::exists(full_path) && ! fs::is_directory(full_path) )
00121          {
00122             status.setCode(vpr::ReturnStatus::Succeed);
00123             absoluteFile = full_path.native_file_string();
00124             break;
00125          }
00126       }
00127       catch(fs::filesystem_error&)
00128       {
00129          /* Ignore the exception. */ ;
00130       }
00131    }
00132 
00133    return status;
00134 }


Member Data Documentation

ParseUtil::SearchPathData jccl::ParseUtil::mSearchInfo [static, private]

Definition at line 111 of file ParseUtil.h.

Referenced by findFileUsingPath(), setCfgSearchPath(), and jccl::ParseUtil::SearchPathData::setSearchPath().


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:49:54 2007 for JCCL: Juggler Configuration and Control Library by  doxygen 1.5.1