source: trunk/lib/utility.h @ 519

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

trac moved to new location.

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