source: trunk/lib/utility.h @ 482

Last change on this file since 482 was 482, checked in by Peter Johansson, 14 years ago

fixes #79 and #5. Only updating changed files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 482 2007-10-13 01:40:38Z peter $
5
6/*
7  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
8
9  This file is part of svndigest, http://trac.thep.lu.se/trac/svndigest
10
11  svndigest is free software; you can redistribute it and/or modify it
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
16  svndigest is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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
27#include <algorithm>
28#include <functional>
29#include <iosfwd>
30#include <string>
31#include <utility>
32#include <vector>
33
34#include <sys/stat.h>
35
36namespace theplu{
37namespace svndigest{
38
39  ///
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
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
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
72  ///
73  /// @return everything after last '/'
74  ///
75  std::string file_name(const std::string&);
76
77  ///
78  /// @brief environment variable @a var
79  ///
80  std::string getenv(const std::string& var); 
81
82  ///
83  /// If @a width is set, size is forced to length @a width. This
84  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
85  /// respectively.
86  ///
87  /// @return x in hexadecimal base
88  ///
89  std::string hex(int x, u_int width=0);
90
91  ///
92  /// @brief remove trailing whitespaces
93  ///
94  std::string htrim(std::string str);
95
96  ///
97  /// @return true if \a str is an integer
98  ///
99  bool is_int(std::string str);
100
101  ///
102  /// @brief remove leading whitespaces
103  ///
104  std::string ltrim(std::string str);
105
106  inline bool match_begin(std::string::const_iterator first, 
107                          std::string::const_iterator last, 
108                          const std::string& str)
109  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
110            std::equal(str.begin(), str.end(), first)); 
111  }
112
113  inline bool match_end(std::string::const_reverse_iterator first, 
114                        std::string::const_reverse_iterator last, 
115                        const std::string& str)
116  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
117            std::equal(str.rbegin(), str.rend(), first)); 
118  }
119
120  ///
121  /// Create directory \a dir. The call can fail in many ways, cf. 'man
122  /// mkdir'.
123  ///
124  void mkdir(const std::string& dir);
125
126  ///
127  /// @brief Check whether \a path already exists or not.
128  ///
129  /// @return True if \a path exists, false otherwise.
130  ///
131  bool node_exist(const std::string& path);
132
133  /**
134     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
135  */
136  int percent(int a, int b);
137
138  ///
139  /// @return the current working directory.
140  ///
141  std::string pwd(void);
142
143  ///
144  /// Search finds a subsecuence in [first, last) being identical to @ str
145  ///
146  /// @return iterator pointing to first character in found subsequence
147  ///
148  /// @see std::search
149  ///
150  inline std::string::iterator search(std::string::iterator first, 
151                                      std::string::iterator last, 
152                                      std::string str)
153  { return std::search(first, last, str.begin(), str.end()); }
154
155  ///
156  ///
157  ///
158  time_t str2time(const std::string&);
159
160  ///
161  /// If file does not exist create empty file.
162  ///
163  void touch(std::string);
164
165  ///
166  /// remove leading and trailing whitespaces
167  ///
168  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
169
170
171  template <typename T>
172  std::string match(std::string::const_iterator& first,
173                    const std::string::const_iterator& last,
174                    const T& func)
175  {
176    std::string res;
177    for (;first!=last && func(first); ++first) 
178      res.append(1,*first);
179    return res;
180  }
181
182
183  std::string match(std::string::const_iterator& first,
184                    const std::string::const_iterator& last,
185                    std::string);
186
187}} // end of namespace svndigest end of namespace theplu
188
189#endif
Note: See TracBrowser for help on using the repository browser.