lib/utility.h

Code
Comments
Other
Rev Date Author Line
165 24 Aug 06 jari 1 #ifndef _theplu_svndigest_utility_
165 24 Aug 06 jari 2 #define _theplu_svndigest_utility_
165 24 Aug 06 jari 3
84 13 Mar 06 jari 4 // $Id$
4 29 Dec 05 peter 5
84 13 Mar 06 jari 6 /*
768 31 Jan 09 peter 7   Copyright (C) 2005 Peter Johansson
978 12 Dec 09 peter 8   Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
1515 26 Sep 12 peter 9   Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson
84 13 Mar 06 jari 10
687 04 Aug 08 peter 11   This file is part of svndigest, http://dev.thep.lu.se/svndigest
84 13 Mar 06 jari 12
149 12 Aug 06 jari 13   svndigest is free software; you can redistribute it and/or modify it
84 13 Mar 06 jari 14   under the terms of the GNU General Public License as published by
693 11 Sep 08 jari 15   the Free Software Foundation; either version 3 of the License, or
84 13 Mar 06 jari 16   (at your option) any later version.
84 13 Mar 06 jari 17
149 12 Aug 06 jari 18   svndigest is distributed in the hope that it will be useful, but
84 13 Mar 06 jari 19   WITHOUT ANY WARRANTY; without even the implied warranty of
126 01 Aug 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84 13 Mar 06 jari 21   General Public License for more details.
84 13 Mar 06 jari 22
84 13 Mar 06 jari 23   You should have received a copy of the GNU General Public License
693 11 Sep 08 jari 24   along with svndigest. If not, see <http://www.gnu.org/licenses/>.
84 13 Mar 06 jari 25 */
84 13 Mar 06 jari 26
1203 05 Oct 10 peter 27 #include "Functor.h"
1203 05 Oct 10 peter 28
4 29 Dec 05 peter 29 #include <algorithm>
4 29 Dec 05 peter 30 #include <functional>
129 02 Aug 06 jari 31 #include <iosfwd>
727 11 Dec 08 jari 32 #include <limits>
705 25 Nov 08 peter 33 #include <sstream>
727 11 Dec 08 jari 34 #include <stdexcept>
23 02 Jan 06 peter 35 #include <string>
4 29 Dec 05 peter 36 #include <utility>
4 29 Dec 05 peter 37 #include <vector>
4 29 Dec 05 peter 38
126 01 Aug 06 jari 39 #include <sys/stat.h>
126 01 Aug 06 jari 40
4 29 Dec 05 peter 41 namespace theplu{
149 12 Aug 06 jari 42 namespace svndigest{
4 29 Dec 05 peter 43
1547 20 Oct 12 peter 44   /**
1547 20 Oct 12 peter 45      if path is absolute return path otherwise return
1547 20 Oct 12 peter 46      concatenate_path(pwd(), path)
1547 20 Oct 12 peter 47    */
1547 20 Oct 12 peter 48   std::string absolute_path(const std::string& path);
1547 20 Oct 12 peter 49
23 02 Jan 06 peter 50   ///
151 13 Aug 06 jari 51   /// @brief Check if access permissions match \a mode. \a mode must
151 13 Aug 06 jari 52   /// be given as r, w, x, or combinations of these letters.
151 13 Aug 06 jari 53   ///
151 13 Aug 06 jari 54   /// @return On success (all requested permissions granted), zero
151 13 Aug 06 jari 55   /// is returned. On error (at least one bit in mode asked for a
151 13 Aug 06 jari 56   /// permission that is denied, or some other error occurred), -1
151 13 Aug 06 jari 57   /// is returned, and errno is set appropriately.
151 13 Aug 06 jari 58   ///
151 13 Aug 06 jari 59   /// @throw An std::runtime_error is thrown when checking for write
151 13 Aug 06 jari 60   /// permissions for a file/direcotry that does not exist.
151 13 Aug 06 jari 61   ///
151 13 Aug 06 jari 62   /// @see access(2)
151 13 Aug 06 jari 63   ///
151 13 Aug 06 jari 64   int access_rights(const std::string& path,const std::string& bits);
151 13 Aug 06 jari 65
462 21 Aug 07 jari 66   /**
705 25 Nov 08 peter 67      \return dir+base if dir ends with '/', else dir+'/'+base
705 25 Nov 08 peter 68    */
705 25 Nov 08 peter 69   std::string concatenate_path(std::string dir, std::string base);
705 25 Nov 08 peter 70
151 13 Aug 06 jari 71   ///
198 09 Sep 06 peter 72   /// @brief environment variable @a var
198 09 Sep 06 peter 73   ///
1513 23 Sep 12 peter 74   std::string getenv(const std::string& var);
198 09 Sep 06 peter 75
226 11 Mar 07 peter 76   ///
373 19 Jun 07 peter 77   /// If @a width is set, size is forced to length @a width. This
373 19 Jun 07 peter 78   /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
373 19 Jun 07 peter 79   /// respectively.
373 19 Jun 07 peter 80   ///
373 19 Jun 07 peter 81   /// @return x in hexadecimal base
373 19 Jun 07 peter 82   ///
589 12 Apr 08 jari 83   std::string hex(int x, unsigned int width=0);
373 19 Jun 07 peter 84
373 19 Jun 07 peter 85   ///
226 11 Mar 07 peter 86   /// @brief remove trailing whitespaces
226 11 Mar 07 peter 87   ///
226 11 Mar 07 peter 88   std::string htrim(std::string str);
226 11 Mar 07 peter 89
1186 27 Aug 10 peter 90   /**
1547 20 Oct 12 peter 91      \return true if path has finite size and first charcter is '/'
1547 20 Oct 12 peter 92    */
1547 20 Oct 12 peter 93   bool is_absolute_path(const std::string& path);
1547 20 Oct 12 peter 94
1547 20 Oct 12 peter 95   /**
1186 27 Aug 10 peter 96      same as lstat(2) but throws an errno_error if error is detected
1186 27 Aug 10 peter 97    */
1186 27 Aug 10 peter 98   void lstat(const std::string path, struct stat*);
1186 27 Aug 10 peter 99
226 11 Mar 07 peter 100   ///
226 11 Mar 07 peter 101   /// @brief remove leading whitespaces
226 11 Mar 07 peter 102   ///
226 11 Mar 07 peter 103   std::string ltrim(std::string str);
226 11 Mar 07 peter 104
1513 23 Sep 12 peter 105   inline bool match_begin(std::string::const_iterator first,
1513 23 Sep 12 peter 106                           std::string::const_iterator last,
118 03 Jul 06 peter 107                           const std::string& str)
1513 23 Sep 12 peter 108   { return (std::distance(first, last)>=static_cast<int>(str.size()) &&
1513 23 Sep 12 peter 109             std::equal(str.begin(), str.end(), first));
118 03 Jul 06 peter 110   }
118 03 Jul 06 peter 111
1513 23 Sep 12 peter 112   inline bool match_end(std::string::const_reverse_iterator first,
1513 23 Sep 12 peter 113                         std::string::const_reverse_iterator last,
118 03 Jul 06 peter 114                         const std::string& str)
1513 23 Sep 12 peter 115   { return (std::distance(first,last)>=static_cast<int>(str.size()) &&
1513 23 Sep 12 peter 116             std::equal(str.rbegin(), str.rend(), first));
118 03 Jul 06 peter 117   }
118 03 Jul 06 peter 118
152 13 Aug 06 jari 119   ///
226 11 Mar 07 peter 120   /// @brief Check whether \a path already exists or not.
226 11 Mar 07 peter 121   ///
226 11 Mar 07 peter 122   /// @return True if \a path exists, false otherwise.
226 11 Mar 07 peter 123   ///
226 11 Mar 07 peter 124   bool node_exist(const std::string& path);
226 11 Mar 07 peter 125
313 17 May 07 peter 126   /**
1513 23 Sep 12 peter 127      @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
313 17 May 07 peter 128   */
313 17 May 07 peter 129   int percent(int a, int b);
313 17 May 07 peter 130
226 11 Mar 07 peter 131   ///
152 13 Aug 06 jari 132   /// @return the current working directory.
152 13 Aug 06 jari 133   ///
152 13 Aug 06 jari 134   std::string pwd(void);
152 13 Aug 06 jari 135
514 09 Dec 07 peter 136   /**
1098 13 Jun 10 peter 137      \return true if \a str matches \a pattern
516 09 Dec 07 peter 138
1098 13 Jun 10 peter 139      \a pattern may contain wildcards '*', '?' and \a vec will contain
1098 13 Jun 10 peter 140      the matching string in \a str. If it's not a match, \a vec is
1098 13 Jun 10 peter 141      undefined. The algorithm is greedy, i.e., wildcards '*' will
516 09 Dec 07 peter 142      consume as many charcters as possible.
516 09 Dec 07 peter 143
516 09 Dec 07 peter 144      \note \a vec is supposed to be empty
516 09 Dec 07 peter 145    */
1098 13 Jun 10 peter 146   bool regexp(const std::string& pattern,  const std::string& str,
516 09 Dec 07 peter 147               std::vector<std::string>& vec);
516 09 Dec 07 peter 148
516 09 Dec 07 peter 149   /**
1547 20 Oct 12 peter 150      \return \a abs_path relative from \a dir, i.e., abs_path =
1547 20 Oct 12 peter 151      dir/<returned value>
1547 20 Oct 12 peter 152    */
1547 20 Oct 12 peter 153   std::string relative_path(const std::string& abs_path,const std::string& dir);
1547 20 Oct 12 peter 154
227 12 Mar 07 peter 155   ///
227 12 Mar 07 peter 156   /// Search finds a subsecuence in [first, last) being identical to @ str
227 12 Mar 07 peter 157   ///
227 12 Mar 07 peter 158   /// @return iterator pointing to first character in found subsequence
227 12 Mar 07 peter 159   ///
227 12 Mar 07 peter 160   /// @see std::search
227 12 Mar 07 peter 161   ///
1513 23 Sep 12 peter 162   inline std::string::iterator search(std::string::iterator first,
1513 23 Sep 12 peter 163                                       std::string::iterator last,
227 12 Mar 07 peter 164                                       std::string str)
118 03 Jul 06 peter 165   { return std::search(first, last, str.begin(), str.end()); }
118 03 Jul 06 peter 166
1203 05 Oct 10 peter 167   /**
1203 05 Oct 10 peter 168      same as sum(3) but using binary(result, *first) rather than
1203 05 Oct 10 peter 169      result += *first
1203 05 Oct 10 peter 170    */
1203 05 Oct 10 peter 171   template<typename InputIterator, typename T, typename BinaryOperation>
1513 23 Sep 12 peter 172   void sum(InputIterator first, InputIterator last, T& result,
1203 05 Oct 10 peter 173            BinaryOperation binary)
1203 05 Oct 10 peter 174   {
1203 05 Oct 10 peter 175     for (; first!=last; ++first)
1203 05 Oct 10 peter 176       binary(result, *first);
1203 05 Oct 10 peter 177   }
1203 05 Oct 10 peter 178
1203 05 Oct 10 peter 179   /**
1203 05 Oct 10 peter 180      Add all values of [first, last) to result
1203 05 Oct 10 peter 181    */
1203 05 Oct 10 peter 182   template<typename InputIterator, typename T>
1203 05 Oct 10 peter 183   void sum(InputIterator first, InputIterator last, T& result)
1203 05 Oct 10 peter 184   {
1203 05 Oct 10 peter 185     typedef typename std::iterator_traits<InputIterator>::const_reference ref;
1203 05 Oct 10 peter 186     typedef PlusAssign<T&, ref> binary;
1203 05 Oct 10 peter 187     return sum(first, last, result, binary());
1203 05 Oct 10 peter 188   }
1203 05 Oct 10 peter 189
201 09 Sep 06 peter 190   ///
297 08 May 07 peter 191   /// If file does not exist create empty file.
297 08 May 07 peter 192   ///
297 08 May 07 peter 193   void touch(std::string);
297 08 May 07 peter 194
297 08 May 07 peter 195   ///
226 11 Mar 07 peter 196   /// remove leading and trailing whitespaces
226 11 Mar 07 peter 197   ///
226 11 Mar 07 peter 198   inline std::string trim(std::string str) { return htrim(ltrim(str)); }
226 11 Mar 07 peter 199
289 08 May 07 peter 200
371 19 Jun 07 peter 201   template <typename T>
289 08 May 07 peter 202   std::string match(std::string::const_iterator& first,
289 08 May 07 peter 203                     const std::string::const_iterator& last,
289 08 May 07 peter 204                     const T& func)
289 08 May 07 peter 205   {
289 08 May 07 peter 206     std::string res;
1513 23 Sep 12 peter 207     for (;first!=last && func(first); ++first)
289 08 May 07 peter 208       res.append(1,*first);
289 08 May 07 peter 209     return res;
289 08 May 07 peter 210   }
289 08 May 07 peter 211
309 12 May 07 peter 212
309 12 May 07 peter 213   std::string match(std::string::const_iterator& first,
309 12 May 07 peter 214                     const std::string::const_iterator& last,
309 12 May 07 peter 215                     std::string);
309 12 May 07 peter 216
149 12 Aug 06 jari 217 }} // end of namespace svndigest end of namespace theplu
1423 16 Dec 11 peter 218 #endif