Flow123d  JS_before_hm-2039-g19af1f327
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
FilePath Class Reference

Dedicated class for storing path to input and output files. More...

#include <file_path.hh>

Collaboration diagram for FilePath:
Collaboration graph
[legend]

Public Types

enum  FileType { input_file, output_file }
 Possible types of file. More...
 

Public Member Functions

 TYPEDEF_ERR_INFO (EI_Path, string)
 
 TYPEDEF_ERR_INFO (EI_Address_String, string)
 
 DECLARE_EXCEPTION (ExcFileOpen,<< "Can not open file: "<< EI_Path::qval<< "\nAt input address: "<< EI_Address_String::qval)
 
 DECLARE_EXCEPTION (ExcAbsOutputPath,<< "Can not set absolute path "<< EI_Path::qval<< " for an output file.")
 
 DECLARE_EXCEPTION (ExcMkdirFail,<< "Can not create directory: "<< EI_Path::qval)
 
 FilePath ()
 
 FilePath (string file_path, const FileType ft)
 
 FilePath (vector< string > sub_paths, const FileType ft)
 Same as previous, but create path from vector of strings. More...
 
 FilePath (string file_path)
 Same as previous but implicitly use FileType::output_file. More...
 
 operator string () const
 
bool operator== (const FilePath &other) const
 Equality comparison operators for FilePaths. More...
 
void create_output_dir ()
 
string parent_path () const
 
string filename () const
 
string stem () const
 
string extension () const
 
string cut_extension () const
 
template<class Stream >
void open_stream (Stream &stream) const
 
bool exists () const
 

Static Public Member Functions

static void set_io_dirs (const string working_dir, const string root, const string input, const string output)
 Obsolete method for set input and output directories. More...
 
static void set_dirs (const string root, const string input, const string output)
 Method for set input and output directories. More...
 
static string set_dirs_from_input (const string main_yaml, const string input, const string output)
 Method for set input and output directories. More...
 
static void add_placeholder (string key, string value)
 Add new item to place holder. More...
 
static const string get_absolute_working_dir ()
 

Private Member Functions

void substitute_value (string &path)
 

Static Private Member Functions

static void create_dir (const boost::filesystem::path &dir)
 
static string convert_for_check_absolute (string path)
 Prepare path string for check absolute path. More...
 

Private Attributes

std::shared_ptr< boost::filesystem::path > abs_file_path_
 Final absolute path to the file. More...
 
FileType file_type_
 File type. More...
 

Static Private Attributes

static std::map< string, string > placeholder
 dictionary of placeholders More...
 
static std::shared_ptr< boost::filesystem::path > output_dir =std::make_shared<boost::filesystem::path>(".")
 Prefix path for output files. More...
 
static std::shared_ptr< boost::filesystem::path > root_dir =std::make_shared<boost::filesystem::path>(".")
 Prefix path for input files (directory of the main input file). More...
 

Detailed Description

Dedicated class for storing path to input and output files.

FilePath objects are constructed from given absolute or relative path to the file and its type (input or output). Before you create any instance of the class you have to call static method set_io_dirs to set:

Definition at line 54 of file file_path.hh.

Member Enumeration Documentation

◆ FileType

Possible types of file.

Enumerator
input_file 
output_file 

Definition at line 67 of file file_path.hh.

Constructor & Destructor Documentation

◆ FilePath() [1/4]

FilePath::FilePath ( )

Default constructor, necessary when using Input::Record::opt_val() to initialize a FilePath.

Definition at line 89 of file file_path.cc.

◆ FilePath() [2/4]

FilePath::FilePath ( string  file_path,
const FileType  ft 
)

Translates the given absolute or relative path to a file file_path depending on the file type ft.

For input files:

  • For relative path prepend absolute path of the directory of the main input file (root directory).
  • Replace $INPUT_DIR$ place holder with the input directory given at command line.

For output files:

  • Forbids absolute output paths.
  • For relative output path prepends it by the output directory given at the command line.

Definition at line 50 of file file_path.cc.

◆ FilePath() [3/4]

FilePath::FilePath ( vector< string >  sub_paths,
const FileType  ft 
)

Same as previous, but create path from vector of strings.

Definition at line 81 of file file_path.cc.

◆ FilePath() [4/4]

FilePath::FilePath ( string  file_path)

Same as previous but implicitly use FileType::output_file.

Definition at line 85 of file file_path.cc.

Member Function Documentation

◆ add_placeholder()

void FilePath::add_placeholder ( string  key,
string  value 
)
static

Add new item to place holder.

Placeholder is extended by adding a single new item. The item can be used in the name of the input or output file name. Currently, the only supported placeholder is $INPUT_DIR$.

Example usage:
FilePath::add_placeholder_item("${SUBST_VAL}", "path/value");
Parameters
[in]keyKey of new item.
[in]valueValue of new item.

Definition at line 155 of file file_path.cc.

Here is the caller graph for this function:

◆ convert_for_check_absolute()

string FilePath::convert_for_check_absolute ( string  path)
staticprivate

Prepare path string for check absolute path.

Check first char of path string. If it is slash '/', add second slash char. Two slashes at begin is necessary for correct output of boost::filesystem::path.is_absolute() method for detection absolute path in unix format ("/home/x/y/z") under cygwin.

Definition at line 239 of file file_path.cc.

Here is the caller graph for this function:

◆ create_dir()

void FilePath::create_dir ( const boost::filesystem::path &  dir)
staticprivate

Create a directory, and check for exceptions.

Definition at line 256 of file file_path.cc.

Here is the caller graph for this function:

◆ create_output_dir()

void FilePath::create_output_dir ( )

For an output filepath, the directory part (up to last separator) is extracted and all subdirectories are created if doesn't exist yet.

Definition at line 176 of file file_path.cc.

Here is the caller graph for this function:

◆ cut_extension()

string FilePath::cut_extension ( ) const

Return path to file with filename without extension.

Definition at line 203 of file file_path.cc.

◆ DECLARE_EXCEPTION() [1/3]

FilePath::DECLARE_EXCEPTION ( ExcAbsOutputPath  ,
<< "Can not set absolute path "<< EI_Path::qval<< " for an output file."   
)

◆ DECLARE_EXCEPTION() [2/3]

FilePath::DECLARE_EXCEPTION ( ExcFileOpen  ,
<< "Can not open file: "<< EI_Path::qval<< "\nAt input address: "<< EI_Address_String::qval   
)

◆ DECLARE_EXCEPTION() [3/3]

FilePath::DECLARE_EXCEPTION ( ExcMkdirFail  ,
<< "Can not create directory: "<< EI_Path::qval   
)

◆ exists()

bool FilePath::exists ( ) const

Return true if the FilePath is a file.

Definition at line 228 of file file_path.cc.

◆ extension()

string FilePath::extension ( ) const

Return extension of file.

Definition at line 198 of file file_path.cc.

Here is the caller graph for this function:

◆ filename()

string FilePath::filename ( ) const

Return name of file with extension.

Definition at line 188 of file file_path.cc.

◆ get_absolute_working_dir()

const string FilePath::get_absolute_working_dir ( )
static

Return absolute path of actual working directory.

Definition at line 171 of file file_path.cc.

◆ open_stream()

template<class Stream >
template void FilePath::open_stream ( Stream &  stream) const

Open stream for this FilePath. Open mode is determined from the FilePath type.

Definition at line 211 of file file_path.cc.

Here is the caller graph for this function:

◆ operator string()

FilePath::operator string ( ) const

This class is implicitly convertible to string.

Definition at line 251 of file file_path.cc.

◆ operator==()

bool FilePath::operator== ( const FilePath other) const

Equality comparison operators for FilePaths.

Definition at line 266 of file file_path.cc.

◆ parent_path()

string FilePath::parent_path ( ) const

Return path to file.

Definition at line 183 of file file_path.cc.

Here is the caller graph for this function:

◆ set_dirs()

void FilePath::set_dirs ( const string  root,
const string  input,
const string  output 
)
static

Method for set input and output directories.

Set:

  • root directory (of the main input file)
  • input directory to replace $INPUT_DIR$ place holder
  • output directory used as prefix to the output files (relative output dirs are relative to the working directory)

Definition at line 102 of file file_path.cc.

Here is the caller graph for this function:

◆ set_dirs_from_input()

string FilePath::set_dirs_from_input ( const string  main_yaml,
const string  input,
const string  output 
)
static

Method for set input and output directories.

Same as previous, but in first argument accepts full path of yaml file and returns filename of this yaml file.

Set:

  • root directory (of the main yaml input file)
  • input directory to replace $INPUT_DIR$ place holder
  • output directory used as prefix to the output files (relative output dirs are relative to the working directory)

Definition at line 137 of file file_path.cc.

Here is the caller graph for this function:

◆ set_io_dirs()

void FilePath::set_io_dirs ( const string  working_dir,
const string  root,
const string  input,
const string  output 
)
static

Obsolete method for set input and output directories.

Ensures consistency of unit tests.

Set:

  • working directory (used only if the output directory is relative)
  • root directory (of the main input file)
  • input directory to replace $INPUT_DIR$ place holder
  • output directory used as prefix to the output files (relative output dirs are relative to the working directory)

Definition at line 96 of file file_path.cc.

◆ stem()

string FilePath::stem ( ) const

Return name of file without extension.

Definition at line 193 of file file_path.cc.

Here is the caller graph for this function:

◆ substitute_value()

void FilePath::substitute_value ( string &  path)
private

Substitutes placeholders in path.

Definition at line 160 of file file_path.cc.

Here is the caller graph for this function:

◆ TYPEDEF_ERR_INFO() [1/2]

FilePath::TYPEDEF_ERR_INFO ( EI_Address_String  ,
string   
)

◆ TYPEDEF_ERR_INFO() [2/2]

FilePath::TYPEDEF_ERR_INFO ( EI_Path  ,
string   
)

Reporting failure when openning a file.

Member Data Documentation

◆ abs_file_path_

std::shared_ptr<boost::filesystem::path> FilePath::abs_file_path_
private

Final absolute path to the file.

Definition at line 227 of file file_path.hh.

◆ file_type_

FileType FilePath::file_type_
private

File type.

Definition at line 230 of file file_path.hh.

◆ output_dir

std::shared_ptr< boost::filesystem::path > FilePath::output_dir =std::make_shared<boost::filesystem::path>(".")
staticprivate

Prefix path for output files.

Definition at line 236 of file file_path.hh.

◆ placeholder

map< string, string > FilePath::placeholder
staticprivate

dictionary of placeholders

Definition at line 233 of file file_path.hh.

◆ root_dir

std::shared_ptr< boost::filesystem::path > FilePath::root_dir =std::make_shared<boost::filesystem::path>(".")
staticprivate

Prefix path for input files (directory of the main input file).

Definition at line 239 of file file_path.hh.


The documentation for this class was generated from the following files: