Flow123d  release_1.8.2-1603-g0109a2b
file_path.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file file_path.cc
15  * @brief
16  */
17 
18 #include <boost/filesystem.hpp>
19 #include <boost/algorithm/string.hpp>
20 
21 #include "file_path.hh"
22 #include "system.hh"
23 
24 
25 // static data members
27 string FilePath::output_dir="";
28 string FilePath::root_dir="";
29 
30 FilePath::FilePath(string file_path, const FileType ft)
31 : file_type_(ft)
32 {
33  abs_file_path_ = file_path;
34  if (output_dir == "") {
35  xprintf(Warn, "Creating FileName object before set_io_dirs is called. No file path resolving.\n");
36  return;
37  }
38 
40  if (ft == input_file) {
42  } else {
44  }
45  } else if (ft == output_file) {
47  if (abs_file_path_.substr(0, output_dir.size()) == output_dir) {
48  abs_file_path_=abs_file_path_.substr(output_dir.size()+1);
49  } else {
50  THROW( ExcAbsOutputPath() << EI_Path( abs_file_path_ ) );
51  }
52  }
54  }
55 
56 }
57 
58 
59 
60 void FilePath::set_io_dirs(const string working_dir, const string root_input_dir,const string input,const string output) {
61  // root directory
62  root_dir = root_input_dir;
63 
64  // relative output dir is relative to working directory
65  // this is possibly independent of position of the main input file
66  output_dir = "";
67  if ( FilePath::is_absolute_path(output) ) {
68  vector<string> dirs;
69  boost::split(dirs, output, boost::is_any_of("/"));
70  for (vector<string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
71  if ( !(*it).size() ) continue;
72  if ( !output_dir.size() ) {
73 #ifdef FLOW123D_HAVE_CYGWIN
74  output_dir = (*it);
75 #else
77 #endif // FLOW123D_HAVE_CYGWIN
78  } else {
80  }
82  }
83  } else {
84  vector<string> dirs;
85  string full_output = working_dir + DIR_DELIMITER + output;
86  boost::split(dirs, full_output, boost::is_any_of("/"));
87  for (vector<string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
88  if ( !(*it).size() ) continue;
89  if ( !output_dir.size() ) output_dir = *it;
92  }
93 
94  FilePath::create_canonical_path(working_dir, output);
95  }
96 
97  // the relative input is relative to the directory of the main input file
98  add_placeholder("${INPUT}", input);
99 }
100 
101 
102 
103 void FilePath::add_placeholder(string key,string value) {
104  placeholder[key] = value;
105 }
106 
107 
109  for (std::map<std::string,std::string>::const_iterator it = this->placeholder.begin(); it != this->placeholder.end(); ++it) {
110  size_t i = abs_file_path_.find(it->first,0);
111  if (i != std::string::npos) {
112  if (it->second == "" ) xprintf(Warn, "Substituting placeholder %s with empty value.\n", it->first.c_str());
113  abs_file_path_.replace(i, it->first.size(), it->second);
114  }
115  }
116 }
117 
118 
119 bool FilePath::is_absolute_path(const string path) {
120  if (path.size() == 0) xprintf(UsrErr, "Path can't be empty!\n");
121 #ifdef FLOW123D_HAVE_CYGWIN
122  if (path.size() == 1) return false;
123  return isalpha(path[0]) && (path[1] == ':');
124 #else
125  return path[0] == DIR_DELIMITER;
126 #endif // FLOW123D_HAVE_CYGWIN
127 }
128 
129 
131  string abs_path = boost::filesystem::current_path().string() + "/";
132 #ifdef FLOW123D_HAVE_CYGWIN
133  boost::replace_all(abs_path, "\\", "/");
134 #endif // FLOW123D_HAVE_CYGWIN
135  return abs_path;
136 }
137 
138 
139 
141  if (file_type_ == output_file) {
142  boost::filesystem::create_directories(
143  boost::filesystem::path(abs_file_path_).parent_path()
144  );
145  }
146 }
147 
148 
149 
150 void FilePath::create_dir(string dir) {
151  if (!boost::filesystem::is_directory(dir)) {
152  boost::filesystem::create_directory(dir);
153  }
154 }
155 
156 
157 void FilePath::create_canonical_path(const string working_dir, const string output) {
158  boost::filesystem::path working_path = boost::filesystem::path(working_dir);
159  boost::filesystem::path output_path = boost::filesystem::path(output);
160 
161  if (working_dir[0] != DIR_DELIMITER)
162  {
163  boost::filesystem::path curr = boost::filesystem::current_path();
164  working_path = boost::filesystem::canonical( curr / working_path );
165  }
166 
167  boost::filesystem::path full_path = boost::filesystem::canonical( working_path / output_path );
168 
169  boost::filesystem::path curr = boost::filesystem::current_path();
170  output_dir = full_path.string();
171 #ifdef FLOW123D_HAVE_CYGWIN
172  boost::replace_all(output_dir, "\\", "/");
173 #endif // FLOW123D_HAVE_CYGWIN
174 }
void substitute_value()
Definition: file_path.cc:108
string abs_file_path_
Final absolute path to the file.
Definition: file_path.hh:149
FilePath()
Definition: file_path.hh:58
void create_output_dir()
Definition: file_path.cc:140
FileType
Possible types of file.
Definition: file_path.hh:50
static void create_dir(string dir)
Definition: file_path.cc:150
static void add_placeholder(string key, string value)
Add new item to place holder.
Definition: file_path.cc:103
Definition: system.hh:59
#define xprintf(...)
Definition: system.hh:87
static string root_dir
Prefix path for input files (directory of the main input file).
Definition: file_path.hh:161
static void create_canonical_path(const string working_dir, const string output)
Definition: file_path.cc:157
static const string get_absolute_working_dir()
Definition: file_path.cc:130
Definition: system.hh:59
static void set_io_dirs(const string working_dir, const string root_input, const string input, const string output)
Definition: file_path.cc:60
static std::map< string, string > placeholder
dictionary of placeholders
Definition: file_path.hh:155
#define DIR_DELIMITER
Definition: system.hh:44
static string output_dir
Prefix path for output files.
Definition: file_path.hh:158
static bool is_absolute_path(const string path)
Definition: file_path.cc:119
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:44
FileType file_type_
File type.
Definition: file_path.hh:152