source: trunk/lib/utility.h @ 795

Last change on this file since 795 was 795, checked in by Peter Johansson, 12 years ago

refs #388

Create a sub-directory 'lib/yat' in which files from yat are
placed. The files may be updated via 'make fetch', see file README for
more details on how to change which files are updated/copied through
this mechanism.

The reason we do not use subversion's external mechanism is that we
don't want the files to be synchronised with yat automatically. We
want the update to be moderated by some developer.

I chose to fetch files using the svn client rather than wget because
the svn allows us to get an Id string with information from the yat
repository. 'wget' would just download the file as it appears on the
server that is the string is not expanded.

The file 'config_public.h' is created by configure just as in
yat. This implies that automake adds $(top_builddir)/lib/yat to the
include path so unless in a VPATH build $(top_srcdir)/lib/yat is in
the include path. As we have already added $(top_srcdir)/lib to the
include path, and there are both a lib/utility.h and a
lib/yat/utility.h it is not obvious which file is included when having
'include "utility.h"'. For this reason, when being in bin/ and test/
it is needed to include "../lib/utility.h" rather than "utility.h"
when we want to include 'lib/utility.h'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 795 2009-06-30 03:57:27Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
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 Copy file \a source to \a target.
64
65     @throw std::runtime_error when read error of \a source or write
66     error for \a target is encountered.
67  */
68  void copy_file(const std::string& source, const std::string& target);
69
70  /**
71     Parsing out the directory of path that is everything prior the
72     last '/' with the eception if \a path ends with '/' in which case
73     everything prior the second last '/' is returned.
74
75     \return directory of path.
76   */
77  std::string directory_name(std::string path);
78
79  /**
80     Function to compare strings. Strings may contain '*'.
81
82     \return true if \a a and \a b are identical.
83   */
84  bool equal(std::string::const_iterator first1, 
85             std::string::const_iterator end1,
86             std::string::const_iterator first2,
87             std::string::const_iterator end2);
88
89  ///
90  /// @return everything after last '/'
91  ///
92  std::string file_name(const std::string&);
93
94  ///
95  /// @brief environment variable @a var
96  ///
97  std::string getenv(const std::string& var); 
98
99  ///
100  /// If @a width is set, size is forced to length @a width. This
101  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
102  /// respectively.
103  ///
104  /// @return x in hexadecimal base
105  ///
106  std::string hex(int x, unsigned int width=0);
107
108  ///
109  /// @brief remove trailing whitespaces
110  ///
111  std::string htrim(std::string str);
112
113  ///
114  /// @brief remove leading whitespaces
115  ///
116  std::string ltrim(std::string str);
117
118  inline bool match_begin(std::string::const_iterator first, 
119                          std::string::const_iterator last, 
120                          const std::string& str)
121  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
122            std::equal(str.begin(), str.end(), first)); 
123  }
124
125  inline bool match_end(std::string::const_reverse_iterator first, 
126                        std::string::const_reverse_iterator last, 
127                        const std::string& str)
128  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
129            std::equal(str.rbegin(), str.rend(), first)); 
130  }
131
132  ///
133  /// Create directory \a dir. The call can fail in many ways, cf. 'man
134  /// mkdir'.
135  ///
136  void mkdir(const std::string& dir);
137
138  ///
139  /// @brief Check whether \a path already exists or not.
140  ///
141  /// @return True if \a path exists, false otherwise.
142  ///
143  bool node_exist(const std::string& path);
144
145  /**
146     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
147  */
148  int percent(int a, int b);
149
150  ///
151  /// @return the current working directory.
152  ///
153  std::string pwd(void);
154
155  /**
156     \return true if first range match second range
157
158     Second range may contain wildcards (such as '*' and '?'), in
159     which case vector \a vec is filled with the corresponding strings
160     in first range. The algorithm is greedy, i.e., wildcard * is will
161     consume as many charcters as possible.
162
163     \note \a vec is supposed to be empty
164   */
165  bool regexp(std::string::const_iterator first1, 
166              std::string::const_iterator end1,
167              std::string::const_iterator first2,
168              std::string::const_iterator end2,
169              std::vector<std::string>& vec);
170
171  /**
172     In \a full_str  replace every sub-string \a old_str with
173     \a new_str;
174   */
175  void replace(std::string& full_str, std::string old_str, std::string new_str);
176
177  ///
178  /// Search finds a subsecuence in [first, last) being identical to @ str
179  ///
180  /// @return iterator pointing to first character in found subsequence
181  ///
182  /// @see std::search
183  ///
184  inline std::string::iterator search(std::string::iterator first, 
185                                      std::string::iterator last, 
186                                      std::string str)
187  { return std::search(first, last, str.begin(), str.end()); }
188
189  ///
190  ///
191  ///
192  time_t str2time(const std::string&);
193
194  ///
195  /// If file does not exist create empty file.
196  ///
197  void touch(std::string);
198
199  ///
200  /// remove leading and trailing whitespaces
201  ///
202  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
203
204
205  template <typename T>
206  std::string match(std::string::const_iterator& first,
207                    const std::string::const_iterator& last,
208                    const T& func)
209  {
210    std::string res;
211    for (;first!=last && func(first); ++first) 
212      res.append(1,*first);
213    return res;
214  }
215
216
217  std::string match(std::string::const_iterator& first,
218                    const std::string::const_iterator& last,
219                    std::string);
220
221}} // end of namespace svndigest end of namespace theplu
222
223#endif
Note: See TracBrowser for help on using the repository browser.