source: trunk/lib/utility.h @ 687

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

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
RevLine 
[165]1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
[84]4// $Id: utility.h 687 2008-08-04 19:37:10Z peter $
[4]5
[84]6/*
[462]7  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
[687]8  Copyright (C) 2008 Jari Häkkinen
[84]9
[687]10  This file is part of svndigest, http://dev.thep.lu.se/svndigest
[84]11
[149]12  svndigest is free software; you can redistribute it and/or modify it
[84]13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
[149]17  svndigest is distributed in the hope that it will be useful, but
[84]18  WITHOUT ANY WARRANTY; without even the implied warranty of
[126]19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
[84]20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
[4]28#include <algorithm>
29#include <functional>
[129]30#include <iosfwd>
[23]31#include <string>
[4]32#include <utility>
33#include <vector>
34
[126]35#include <sys/stat.h>
36
[4]37namespace theplu{
[149]38namespace svndigest{
[4]39
[23]40  ///
[151]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
[462]56  /**
57     @brief Copy file \a source to \a target.
58
59     @throw std::runtime_error when read error of \a source or write
60     error for \a target is encountered.
61  */
62  void copy_file(const std::string& source, const std::string& target);
63
[482]64  /**
65     Parsing out the directory of path that is everything prior the
66     last '/' with the eception if \a path ends with '/' in which case
67     everything prior the second last '/' is returned.
68
69     \return directory of path.
70   */
71  std::string directory_name(std::string path);
72
[507]73  /**
74     Function to compare strings. Strings may contain '*'.
75
76     \return true if \a a and \a b are identical.
77   */
78  bool equal(std::string::const_iterator first1, 
79             std::string::const_iterator end1,
80             std::string::const_iterator first2,
81             std::string::const_iterator end2);
82
[151]83  ///
[100]84  /// @return everything after last '/'
85  ///
86  std::string file_name(const std::string&);
87
[198]88  ///
89  /// @brief environment variable @a var
90  ///
91  std::string getenv(const std::string& var); 
92
[226]93  ///
[373]94  /// If @a width is set, size is forced to length @a width. This
95  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
96  /// respectively.
97  ///
98  /// @return x in hexadecimal base
99  ///
[589]100  std::string hex(int x, unsigned int width=0);
[373]101
102  ///
[226]103  /// @brief remove trailing whitespaces
104  ///
105  std::string htrim(std::string str);
106
107  ///
[274]108  /// @return true if \a str is an integer
109  ///
110  bool is_int(std::string str);
111
112  ///
[226]113  /// @brief remove leading whitespaces
114  ///
115  std::string ltrim(std::string str);
116
[214]117  inline bool match_begin(std::string::const_iterator first, 
118                          std::string::const_iterator last, 
[118]119                          const std::string& str)
120  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
121            std::equal(str.begin(), str.end(), first)); 
122  }
123
[217]124  inline bool match_end(std::string::const_reverse_iterator first, 
125                        std::string::const_reverse_iterator last, 
[118]126                        const std::string& str)
127  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
128            std::equal(str.rbegin(), str.rend(), first)); 
129  }
130
[152]131  ///
132  /// Create directory \a dir. The call can fail in many ways, cf. 'man
133  /// mkdir'.
134  ///
[482]135  void mkdir(const std::string& dir);
[152]136
137  ///
[226]138  /// @brief Check whether \a path already exists or not.
139  ///
140  /// @return True if \a path exists, false otherwise.
141  ///
142  bool node_exist(const std::string& path);
143
[313]144  /**
145     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
146  */
147  int percent(int a, int b);
148
[226]149  ///
[152]150  /// @return the current working directory.
151  ///
152  std::string pwd(void);
153
[514]154  /**
[516]155     \return true if first range match second range
156
157     Second range may contain wildcards (such as '*' and '?'), in
158     which case vector \a vec is filled with the corresponding strings
159     in first range. The algorithm is greedy, i.e., wildcard * is will
160     consume as many charcters as possible.
161
162     \note \a vec is supposed to be empty
163   */
164  bool regexp(std::string::const_iterator first1, 
165              std::string::const_iterator end1,
166              std::string::const_iterator first2,
167              std::string::const_iterator end2,
168              std::vector<std::string>& vec);
169
170  /**
[514]171     In \a full_str  replace every sub-string \a old_str with
172     \a new_str;
173   */
174  void replace(std::string& full_str, std::string old_str, std::string new_str);
175
[227]176  ///
177  /// Search finds a subsecuence in [first, last) being identical to @ str
178  ///
179  /// @return iterator pointing to first character in found subsequence
180  ///
181  /// @see std::search
182  ///
183  inline std::string::iterator search(std::string::iterator first, 
184                                      std::string::iterator last, 
185                                      std::string str)
[118]186  { return std::search(first, last, str.begin(), str.end()); }
187
[60]188  ///
[201]189  ///
190  ///
191  time_t str2time(const std::string&);
192
193  ///
[297]194  /// If file does not exist create empty file.
195  ///
196  void touch(std::string);
197
198  ///
[226]199  /// remove leading and trailing whitespaces
200  ///
201  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
202
[620]203  ///
204  /// \return string with version info
205  ///
206  std::string version_string(void);
207 
[289]208
[371]209  template <typename T>
[289]210  std::string match(std::string::const_iterator& first,
211                    const std::string::const_iterator& last,
212                    const T& func)
213  {
214    std::string res;
[294]215    for (;first!=last && func(first); ++first) 
[289]216      res.append(1,*first);
217    return res;
218  }
219
[309]220
221  std::string match(std::string::const_iterator& first,
222                    const std::string::const_iterator& last,
223                    std::string);
224
[149]225}} // end of namespace svndigest end of namespace theplu
[4]226
[23]227#endif
Note: See TracBrowser for help on using the repository browser.