Flow123d  jenkins-Flow123d-windows-release-multijob-285
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(string file_path, const FileType ft)
22 : file_type_(ft)
23 {
24  if (output_dir == "") {
25  xprintf(Warn, "Creating FileName object before set_io_dirs is called. No file path resolving.\n");
26  abs_file_path = file_path;
27  return;
28  }
29 
30  if (ft == input_file) {
31  abs_file_path = root_dir + DIR_DELIMITER + file_path;
33  } else if (ft == output_file) {
34  if ( FilePath::is_absolute_path(file_path) ) {
35  if (file_path.substr(0, output_dir.size()) == output_dir) {
36  file_path=file_path.substr(output_dir.size()+1);
37  } else {
38  THROW( ExcAbsOutputPath() << EI_Path( file_path ) );
39  }
40  }
41  abs_file_path = output_dir + DIR_DELIMITER + file_path;
43  }
44 }
45 
46 
47 
48 void FilePath::set_io_dirs(const string working_dir, const string root_input_dir,const string input,const string output) {
49  // root directory
50  root_dir = root_input_dir;
51 
52  // relative output dir is relative to working directory
53  // this is possibly independent of position of the main input file
54  output_dir = "";
55  if ( FilePath::is_absolute_path(output) ) {
56  vector<string> dirs;
57  boost::split(dirs, output, boost::is_any_of("/"));
58  for (vector<string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
59  if ( !(*it).size() ) continue;
60  if ( !output_dir.size() ) {
61 #ifdef CYGWIN
62  output_dir = (*it);
63 #else
65 #endif
66  } else {
68  }
70  }
71  } else {
72  vector<string> dirs;
73  string full_output = working_dir + DIR_DELIMITER + output;
74  boost::split(dirs, full_output, boost::is_any_of("/"));
75  for (vector<string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
76  if ( !(*it).size() ) continue;
77  if ( !output_dir.size() ) output_dir = *it;
80  }
81 
82  FilePath::create_canonical_path(working_dir, output);
83  }
84 
85  // the relative input is relative to the directory of the main input file
86  add_placeholder("${INPUT}", input);
87 }
88 
89 
90 
91 void FilePath::add_placeholder(string key,string value) {
92  placeholder[key] = value;
93 }
94 
95 
97  for (std::map<std::string,std::string>::const_iterator it = this->placeholder.begin(); it != this->placeholder.end(); ++it) {
98  size_t i = abs_file_path.find(it->first,0);
99  if (i != std::string::npos) {
100  if (it->second == "" ) xprintf(Warn, "Substituting placeholder %s with empty value.\n", it->first.c_str());
101  abs_file_path.replace(i, it->first.size(), it->second);
102  }
103  }
104 }
105 
106 
107 bool FilePath::is_absolute_path(const string path) {
108  if (path.size() == 0) xprintf(UsrErr, "Path can't be empty!\n");
109 #ifdef CYGWIN
110  if (path.size() == 1) return false;
111  return isalpha(path[0]) && (path[1] == ':');
112 #else
113  return path[0] == DIR_DELIMITER;
114 #endif
115 }
116 
117 
119  string abs_path = boost::filesystem::current_path().string();
120 #ifdef CYGWIN
121  boost::replace_all(abs_path, "\\", "/");
122 #endif
123  return abs_path;
124 }
125 
126 
127 
129  if (file_type_ == output_file) {
130  boost::filesystem::create_directories(
131  boost::filesystem::path(abs_file_path).parent_path()
132  );
133  }
134 }
135 
136 
137 
138 void FilePath::create_dir(string dir) {
139  if (!boost::filesystem::is_directory(dir)) {
140  boost::filesystem::create_directory(dir);
141  }
142 }
143 
144 
145 void FilePath::create_canonical_path(const string working_dir, const string output) {
146  boost::filesystem::path working_path = boost::filesystem::path(working_dir);
147  boost::filesystem::path output_path = boost::filesystem::path(output);
148 
149  if (working_dir[0] != DIR_DELIMITER)
150  {
151  boost::filesystem::path curr = boost::filesystem::current_path();
152  working_path = boost::filesystem::canonical( curr / working_path );
153  }
154 
155  boost::filesystem::path full_path = boost::filesystem::canonical( working_path / output_path );
156 
157  boost::filesystem::path curr = boost::filesystem::current_path();
158  output_dir = full_path.string();
159 #ifdef CYGWIN
160  boost::replace_all(output_dir, "\\", "/");
161 #endif
162 }
void substitute_value()
Definition: file_path.cc:96
FilePath()
Definition: file_path.hh:48
void create_output_dir()
Definition: file_path.cc:128
FileType
Possible types of file.
Definition: file_path.hh:40
static void create_dir(string dir)
Definition: file_path.cc:138
string abs_file_path
Final absolute path to the file.
Definition: file_path.hh:139
static void add_placeholder(string key, string value)
Add new item to place holder.
Definition: file_path.cc:91
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:151
static void create_canonical_path(const string working_dir, const string output)
Definition: file_path.cc:145
static const string get_absolute_working_dir()
Definition: file_path.cc:118
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:48
static std::map< string, string > placeholder
dictionary of placeholders
Definition: file_path.hh:145
#define DIR_DELIMITER
Definition: system.hh:56
static string output_dir
Prefix path for output files.
Definition: file_path.hh:148
static bool is_absolute_path(const string path)
Definition: file_path.cc:107
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
FileType file_type_
File type.
Definition: file_path.hh:142