Flow123d  jenkins-Flow123d-windows32-release-multijob-51
file_path.cc
Go to the documentation of this file.
1 /*
2  * file_name.cc
3  *
4  * Created on: May 23, 2012
5  * Author: jb
6  */
7 
8 
9 #include <boost/filesystem.hpp>
10 #include <boost/algorithm/string.hpp>
11 
12 #include "file_path.hh"
13 #include "system.hh"
14 
15 
16 // static data members
18 string FilePath::output_dir="";
19 string FilePath::root_dir="";
20 
21 FilePath::FilePath(const string file_path, const FileType ft) {
22  if (output_dir == "") {
23  xprintf(Warn, "Creating FileName object before set_io_dirs is called. No file path resolving.\n");
24  abs_file_path = file_path;
25  return;
26  }
27 
28  if (ft == input_file) {
29  abs_file_path = root_dir + DIR_DELIMITER + file_path;
31  } else if (ft == output_file) {
32  if (file_path[0] == DIR_DELIMITER) {
33  THROW( ExcAbsOutputPath() << EI_Path( file_path ) );
34  }
35  abs_file_path = output_dir + DIR_DELIMITER + file_path;
37  }
38 }
39 
40 
41 
42 void FilePath::set_io_dirs(const string working_dir, const string root_input_dir,const string input,const string output) {
43  // root directory
44  root_dir = root_input_dir;
45 
46  // relative output dir is relative to working directory
47  // this is possibly independent of position of the main input file
48  output_dir = "";
49  if ( FilePath::is_absolute_path(output) ) {
50  vector<string> dirs;
51  boost::split(dirs, output, boost::is_any_of("/"));
52  for (vector<string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
53  if ( !(*it).size() ) continue;
54  if ( !output_dir.size() ) {
55 #ifdef CYGWIN
56  output_dir = (*it);
57 #else
58  output_dir = DIR_DELIMITER + *it;
59 #endif
60  } else {
62  }
64  }
65  } else {
66  vector<string> dirs;
67  string full_output = working_dir + DIR_DELIMITER + output;
68  boost::split(dirs, full_output, boost::is_any_of("/"));
69  for (vector<string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
70  if ( !(*it).size() ) continue;
71  if ( !output_dir.size() ) output_dir = *it;
72  else output_dir = output_dir + DIR_DELIMITER + *it;
74  }
75 
76  FilePath::create_canonical_path(working_dir, output);
77  }
78 
79  // the relative input is relative to the directory of the main input file
80  add_placeholder("${INPUT}", input);
81 }
82 
83 
84 
85 void FilePath::add_placeholder(string key,string value) {
86  placeholder[key] = value;
87 }
88 
89 
91  for (std::map<std::string,std::string>::const_iterator it = this->placeholder.begin(); it != this->placeholder.end(); ++it) {
92  size_t i = abs_file_path.find(it->first,0);
93  if (i != std::string::npos) {
94  if (it->second == "" ) xprintf(Warn, "Substituting placeholder %s with empty value.\n", it->first.c_str());
95  abs_file_path.replace(i, it->first.size(), it->second);
96  }
97  }
98 }
99 
100 
101 bool FilePath::is_absolute_path(const string path) {
102  if (path.size() == 0) xprintf(UsrErr, "Path can't be empty!\n");
103 #ifdef CYGWIN
104  if (path.size() == 1) return false;
105  return isalpha(path[0]) && (path[1] == ':');
106 #else
107  return path[0] == DIR_DELIMITER;
108 #endif
109 }
110 
111 
113  string abs_path = boost::filesystem::current_path().string();
114 #ifdef CYGWIN
115  boost::replace_all(abs_path, "\\", "/");
116 #endif
117  return abs_path;
118 }
119 
120 
122  if (!boost::filesystem::is_directory(output_dir)) {
123  boost::filesystem::create_directory(output_dir);
124  return true;
125  }
126  return false;
127 }
128 
129 
130 void FilePath::create_canonical_path(const string working_dir, const string output) {
131  boost::filesystem::path working_path = boost::filesystem::path(working_dir);
132  boost::filesystem::path output_path = boost::filesystem::path(output);
133 
134  if (working_dir[0] != DIR_DELIMITER)
135  {
136  boost::filesystem::path curr = boost::filesystem::current_path();
137  working_path = boost::filesystem::canonical( curr / working_path );
138  }
139 
140  boost::filesystem::path full_path = boost::filesystem::canonical( working_path / output_path );
141 
142  boost::filesystem::path curr = boost::filesystem::current_path();
143  output_dir = full_path.string();
144 #ifdef CYGWIN
145  boost::replace_all(output_dir, "\\", "/");
146 #endif
147 }
void substitute_value()
Definition: file_path.cc:90
FilePath()
Definition: file_path.hh:48
FileType
Possible types of file.
Definition: file_path.hh:40
static bool create_output_dir()
Definition: file_path.cc:121
string abs_file_path
Final absolute path to the file.
Definition: file_path.hh:126
static void add_placeholder(string key, string value)
Add new item to place holder.
Definition: file_path.cc:85
Definition: system.hh:72
#define xprintf(...)
Definition: system.hh:100
static string root_dir
Prefix path for input files (directory of the main input file).
Definition: file_path.hh:135
static void create_canonical_path(const string working_dir, const string output)
Definition: file_path.cc:130
static const string get_absolute_working_dir()
Definition: file_path.cc:112
Definition: system.hh:72
static void set_io_dirs(const string working_dir, const string root_input, const string input, const string output)
Definition: file_path.cc:42
static std::map< string, string > placeholder
dictionary of placeholders
Definition: file_path.hh:129
#define DIR_DELIMITER
Definition: system.hh:56
static string output_dir
Prefix path for output files.
Definition: file_path.hh:132
static bool is_absolute_path(const string path)
Definition: file_path.cc:101
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34