source: trunk/lib/utility.h @ 727

Last change on this file since 727 was 727, checked in by Jari Häkkinen, 13 years ago

Changes needed to cleanly compile on g++ 4.3 on SuSE 11 x64.

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