SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pathops.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
25 
26 #ifndef SHORE_MISC_PATHOPS_HPP__
27 #define SHORE_MISC_PATHOPS_HPP__
28 
29 #include <stdexcept>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include <glob.h>
35 
36 #include <boost/cstdint.hpp>
37 
38 #include "shore/base/stringops.hpp"
39 
40 namespace shore {
41 
43 std::string home();
44 
46 std::string config_home();
47 
49 std::string expand_home(const std::string& path);
50 
52 std::string path_parent(const std::string& path);
53 
55 std::string path_leaf(const std::string& path);
56 
58 std::string path_trunk(const std::string& path);
59 
61 std::string path_clean(const std::string& path);
62 
65 std::string path_enum(const std::string& path,const std::string& suffix,
66  int& num_ofs,const char sep='_');
67 
70 std::string path_enum(const std::string& path,
71  const std::string& suffix=std::string());
72 
74 bool is_executable(const std::string& fn);
75 
78 std::string mktempdir(const std::string& prefix);
79 
81 std::string which_nofail(const std::string& command,
82  const std::string& env=std::string());
83 
85 uint64_t file_size(const std::string &fn);
86 
87 // Forward declaration for which().
88 void toupper(std::string& str);
89 
91 template<class Ex>
92 std::string which_fail(const std::string& command,
93  const std::string& env=std::string())
94 {
95  const std::string ret=which_nofail(command,env);
96  if(ret.empty())
97  {
98  std::string msg="Program not found: "+command+"\n\n";
99  msg+="\tPlease make sure that "+command+" is in your path";
100  if(!env.empty())
101  {
102  std::string uccmd=command;
103  toupper(uccmd);
104 
105  msg+=",\n\tor set the environment variable '"+env
106  +"' to the folder with the "+command+" binary."\
107  "\n\n\tFor example, bash users type or permantely set in ~/.bashrc:"\
108  "\n\n\t\texport "+env+"=FOLDER_TO_"+uccmd+"\n\n";
109 
110  }
111  throw Ex(msg);
112  }
113  return ret;
114 }
115 
117 std::string which(const std::string& command,const std::string& env=std::string());
118 
119 template<class Iter>
120 void glob(Iter b,Iter e,std::vector<std::string> &ret)
121 {
122  for(Iter i=b;i!=e;++i)
123  {
124  glob_t globbuf;
125 #ifdef GLOB_TILDE
126  const int e=glob(to_string(*i).c_str(),GLOB_TILDE,0,&globbuf);
127 #else
128  const int e=glob(to_string(*i).c_str(),0,0,&globbuf);
129 #endif
130 
131  if(e==0)
132  {
133  for(size_t j=0;j<globbuf.gl_pathc;++j)
134  {
135  ret.push_back(globbuf.gl_pathv[j]);
136  }
137  }
138 
139  globfree(&globbuf);
140 
141  if(e!=0)
142  {
143  if(e!=GLOB_NOMATCH)
144  throw std::runtime_error("glob error");
145  }
146  }
147 }
148 
150 template<class Iter>
151 std::vector<std::string> glob(Iter b,Iter e)
152 {
153  std::vector<std::string> ret;
154  glob(b,e,ret);
155  return ret;
156 }
157 
159 void glob(const std::string& pattern,std::vector<std::string> &ret);
160 
162 std::vector<std::string> glob(const std::string& pattern);
163 
164 typedef boost::uint64_t uint64_t;
165 
167 uint64_t get_mtime(const std::string &path);
168 
170 bool is_older(const std::string &path1,const std::string &path2);
171 
172 } // namespace shore
173 
174 #endif // SHORE_MISC_PATHOPS_HPP__
175