source: trunk/lib/utility.h @ 705

Last change on this file since 705 was 705, checked in by Peter Johansson, 13 years ago

importing classes for commandline parsing from yat. This fixes #349 and #265

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 705 2008-11-25 23:33:26Z peter $
5
6/*
7  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2008 Jari Häkkinen
9
10  This file is part of svndigest, http://dev.thep.lu.se/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <algorithm>
27#include <functional>
28#include <iosfwd>
29#include <sstream>
30#include <string>
31#include <stdexcept>
32#include <utility>
33#include <vector>
34
35#include <sys/stat.h>
36
37namespace theplu{
38namespace svndigest{
39
40  ///
41  /// @brief Check if access permissions match \a mode. \a mode must
42  /// be given as r, w, x, or combinations of these letters.
43  ///
44  /// @return On success (all requested permissions granted), zero
45  /// is returned. On error (at least one bit in mode asked for a
46  /// permission that is denied, or some other error occurred), -1
47  /// is returned, and errno is set appropriately.
48  ///
49  /// @throw An std::runtime_error is thrown when checking for write
50  /// permissions for a file/direcotry that does not exist.
51  ///
52  /// @see access(2)
53  ///
54  int access_rights(const std::string& path,const std::string& bits);
55
56  /**
57     \return dir+base if dir ends with '/', else dir+'/'+base
58   */
59  std::string concatenate_path(std::string dir, std::string base);
60
61  /**
62     \brief convert string to (numerical) type
63
64     \throw runtime_error if conversion fails
65   */
66  template<typename T>
67  T convert(const std::string& s);
68
69  /**
70     @brief Copy file \a source to \a target.
71
72     @throw std::runtime_error when read error of \a source or write
73     error for \a target is encountered.
74  */
75  void copy_file(const std::string& source, const std::string& target);
76
77  /**
78     Parsing out the directory of path that is everything prior the
79     last '/' with the eception if \a path ends with '/' in which case
80     everything prior the second last '/' is returned.
81
82     \return directory of path.
83   */
84  std::string directory_name(std::string path);
85
86  /**
87     Function to compare strings. Strings may contain '*'.
88
89     \return true if \a a and \a b are identical.
90   */
91  bool equal(std::string::const_iterator first1, 
92             std::string::const_iterator end1,
93             std::string::const_iterator first2,
94             std::string::const_iterator end2);
95
96  ///
97  /// @return everything after last '/'
98  ///
99  std::string file_name(const std::string&);
100
101  ///
102  /// @brief environment variable @a var
103  ///
104  std::string getenv(const std::string& var); 
105
106  ///
107  /// If @a width is set, size is forced to length @a width. This
108  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
109  /// respectively.
110  ///
111  /// @return x in hexadecimal base
112  ///
113  std::string hex(int x, unsigned int width=0);
114
115  ///
116  /// @brief remove trailing whitespaces
117  ///
118  std::string htrim(std::string str);
119
120  /**
121     @return true if string \a s fulfills regular expression \verbatim
122     ^\w* \endverbatim \a other \verbatim \w*$ \endverbatim (case
123     insensitive)
124  */
125  bool is_equal(std::string s, std::string other);
126
127  ///
128  /// @return true if \a str is an integer
129  ///
130  bool is_int(std::string str);
131
132  ///
133  /// @return true if string is "nan" (case-insensitive)
134  ///
135  bool is_nan(const std::string& s);
136
137  ///
138  /// @brief remove leading whitespaces
139  ///
140  std::string ltrim(std::string str);
141
142  inline bool match_begin(std::string::const_iterator first, 
143                          std::string::const_iterator last, 
144                          const std::string& str)
145  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
146            std::equal(str.begin(), str.end(), first)); 
147  }
148
149  inline bool match_end(std::string::const_reverse_iterator first, 
150                        std::string::const_reverse_iterator last, 
151                        const std::string& str)
152  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
153            std::equal(str.rbegin(), str.rend(), first)); 
154  }
155
156  ///
157  /// Create directory \a dir. The call can fail in many ways, cf. 'man
158  /// mkdir'.
159  ///
160  void mkdir(const std::string& dir);
161
162  ///
163  /// @brief Check whether \a path already exists or not.
164  ///
165  /// @return True if \a path exists, false otherwise.
166  ///
167  bool node_exist(const std::string& path);
168
169  /**
170     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
171  */
172  int percent(int a, int b);
173
174  ///
175  /// @return the current working directory.
176  ///
177  std::string pwd(void);
178
179  /**
180     \return true if first range match second range
181
182     Second range may contain wildcards (such as '*' and '?'), in
183     which case vector \a vec is filled with the corresponding strings
184     in first range. The algorithm is greedy, i.e., wildcard * is will
185     consume as many charcters as possible.
186
187     \note \a vec is supposed to be empty
188   */
189  bool regexp(std::string::const_iterator first1, 
190              std::string::const_iterator end1,
191              std::string::const_iterator first2,
192              std::string::const_iterator end2,
193              std::vector<std::string>& vec);
194
195  /**
196     In \a full_str  replace every sub-string \a old_str with
197     \a new_str;
198   */
199  void replace(std::string& full_str, std::string old_str, std::string new_str);
200
201  ///
202  /// Search finds a subsecuence in [first, last) being identical to @ str
203  ///
204  /// @return iterator pointing to first character in found subsequence
205  ///
206  /// @see std::search
207  ///
208  inline std::string::iterator search(std::string::iterator first, 
209                                      std::string::iterator last, 
210                                      std::string str)
211  { return std::search(first, last, str.begin(), str.end()); }
212
213  ///
214  ///
215  ///
216  time_t str2time(const std::string&);
217
218  ///
219  /// If file does not exist create empty file.
220  ///
221  void touch(std::string);
222
223  ///
224  /// remove leading and trailing whitespaces
225  ///
226  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
227
228  ///
229  /// \return string with version info
230  ///
231  std::string version_string(void);
232 
233
234  template <typename T>
235  std::string match(std::string::const_iterator& first,
236                    const std::string::const_iterator& last,
237                    const T& func)
238  {
239    std::string res;
240    for (;first!=last && func(first); ++first) 
241      res.append(1,*first);
242    return res;
243  }
244
245
246  std::string match(std::string::const_iterator& first,
247                    const std::string::const_iterator& last,
248                    std::string);
249
250  // template implementations
251  template<typename T>
252  T convert(const std::string& s)
253  {
254    if (is_nan(s))
255      return std::numeric_limits<T>::quiet_NaN();
256    if (is_equal(s, "inf"))
257      return std::numeric_limits<T>::infinity();
258    if (is_equal(s, "-inf"))
259      if (std::numeric_limits<T>::is_signed)
260        return -std::numeric_limits<T>::infinity();
261      else
262        throw std::runtime_error(std::string("convert(\"")+s+
263                                 std::string("\"): type is unsigned") );
264    std::stringstream ss(s);
265    T a;
266    ss >> a;
267    bool ok = true;
268    if(ss.fail()) 
269      ok = false;
270    // Check that nothing is left on stream
271    std::string b;
272    ss >> b;
273    if (!b.empty() || !ok)
274      throw std::runtime_error(std::string("convert(\"")+s+std::string("\")"));
275    return a;
276  }
277
278}} // end of namespace svndigest end of namespace theplu
279
280#endif
Note: See TracBrowser for help on using the repository browser.