Changeset 465 for trunk


Ignore:
Timestamp:
Aug 24, 2007, 3:28:24 PM (14 years ago)
Author:
Peter Johansson
Message:

fixes #217. Moving functors to a Functor.h and Functor.cc

Location:
trunk/lib
Files:
6 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/lib/Configuration.cc

    r449 r465  
    2424#include "Configuration.h"
    2525
    26 #include "utility.h"
     26#include "Functor.h"
    2727
    2828#include <cassert>
  • trunk/lib/Functor.cc

    r463 r465  
    2222*/
    2323
    24 #include "utility.h"
     24#include "Functor.h"
    2525
    26 #include <cstdlib>
    27 #include <fstream>
    28 #include <sstream>
    29 #include <stdexcept>
    3026#include <string>
    31 #include <sys/param.h>
    32 #include <unistd.h>
    33 
    34 #include <iostream>
    3527
    3628namespace theplu{
    3729namespace svndigest{
    38 
    39   int access_rights(const std::string& path, const std::string& bits)
    40   {
    41     if (access(path.c_str(),F_OK)) {
    42       throw std::runtime_error(std::string("access_rights: ") + path +
    43                                "' does not exist.");
    44     }
    45     int mode=0;
    46     for (u_int i=0; i<bits.length(); ++i)
    47       switch (bits[i]) {
    48           case 'r':
    49             mode|=R_OK;
    50             break;
    51           case 'w':
    52             mode|=W_OK;
    53             break;
    54           case 'x':
    55             mode|=X_OK;
    56             break;
    57       }
    58     return access(path.c_str(),mode);
    59   }
    60 
    61 
    62   void copy_file(const std::string& source, const std::string& target)
    63   {
    64     std::ofstream o(target.c_str());
    65     std::ifstream i(source.c_str());
    66     while (i.good()) {
    67       char ch=i.get();
    68       if (i.good())
    69         o.put(ch);
    70       if (!o.good())
    71         throw std::runtime_error(std::string("copy_file: ") +
    72                                  "writing target file failed '" + target + "'");
    73     }
    74     if (!i.eof() && (i.bad() || i.fail()))  // fail on everything except eof
    75       throw std::runtime_error(std::string("copy_file: ") +
    76                                "error reading source file '" + source + "'");
    77     i.close(); o.close();
    78   }
    79 
    80 
    81   std::string file_name(const std::string& full_path)
    82   {
    83     std::stringstream ss(full_path);
    84     std::string name;
    85     while (getline(ss,name,'/')) {}
    86     return name;
    87   }
    88 
    89 
    90   std::string getenv(const std::string& var)
    91   {
    92     char* buffer=std::getenv(var.c_str());
    93     if (!buffer)
    94       throw std::runtime_error("Environment variable "+var+" is not set");
    95     return std::string(buffer);
    96   }
    97 
    98 
    99   std::string hex(int x, u_int width)
    100   {
    101     std::stringstream ss;
    102     ss << std::hex << x;
    103     if (!width)
    104       return ss.str();
    105     if (ss.str().size()<width)
    106       return std::string(width-ss.str().size(), '0') + ss.str();
    107     return ss.str().substr(0, width);
    108   }
    109 
    110 
    111   std::string htrim(std::string str)
    112   {
    113     size_t length=str.size();
    114     while(length && isspace(str[length-1]))
    115       --length;
    116     return str.substr(0,length);
    117   }
    118 
    119 
    120   bool is_int(std::string s)
    121   {
    122     std::stringstream ss(s);
    123     int a;
    124     ss >> a;
    125     if(ss.fail())
    126       return false;
    127     // Check that nothing is left on stream
    128     std::string b;
    129     ss >> b;
    130     return b.empty();
    131   }
    132 
    133 
    134   std::string ltrim(std::string str)
    135   {
    136     size_t i = 0;
    137     while(i<str.size() && isspace(str[i]))
    138       ++i;
    139     return str.substr(i);
    140   }
    141 
    142 
    143   int percent(int a, int b)
    144   {
    145     if (b)
    146       return (100*a)/b;
    147     return 0;
    148   }
    149 
    150 
    151   bool node_exist(const std::string& path)
    152   {
    153     struct stat buf;
    154     return !stat(path.c_str(),&buf);
    155   }
    156 
    157 
    158   std::string pwd(void)
    159   {
    160     char buffer[MAXPATHLEN];
    161     if (!getcwd(buffer, MAXPATHLEN))
    162       throw std::runtime_error("Failed to get current working directory");
    163     return std::string(buffer);
    164   }
    165 
    166 
    167   void touch(std::string str)
    168   {
    169     if (!node_exist(str)) {
    170       std::ofstream os(str.c_str());
    171       os.close();
    172     }
    173   }
    174 
    175   time_t str2time(const std::string& str)
    176   {
    177     //  str in format 2006-09-09T10:55:52.132733Z
    178     std::stringstream sstream(str);
    179     time_t rawtime;
    180     struct tm * timeinfo;
    181     time ( &rawtime );
    182     timeinfo =  gmtime ( &rawtime );
    183 
    184     u_int year, month, day, hour, minute, second;
    185     std::string tmp;
    186     getline(sstream,tmp,'-');
    187     year=atoi(tmp.c_str());
    188     timeinfo->tm_year = year - 1900;
    189 
    190     getline(sstream,tmp,'-');
    191     month=atoi(tmp.c_str());
    192     timeinfo->tm_mon = month - 1;
    193 
    194     getline(sstream,tmp,'T');
    195     day=atoi(tmp.c_str());
    196     timeinfo->tm_mday = day;
    197 
    198     getline(sstream,tmp,':');
    199     hour=atoi(tmp.c_str());
    200     timeinfo->tm_hour = hour;
    201 
    202     getline(sstream,tmp,':');
    203     minute=atoi(tmp.c_str());
    204     timeinfo->tm_min = minute;
    205 
    206     getline(sstream,tmp,'.');
    207     second=atoi(tmp.c_str());
    208     timeinfo->tm_sec = second;
    209 
    210     return mktime(timeinfo);
    211   }
    212 
    213 
    214   std::string match(std::string::const_iterator& first,
    215                     const std::string::const_iterator& last,
    216                     std::string str)
    217   {
    218     if (match_begin(first, last, str)){
    219       first+=str.size();
    220       return str;
    221     }
    222     return std::string();
    223   }
    22430
    22531  notChar::notChar(char c)
  • trunk/lib/Functor.h

    r463 r465  
    1 #ifndef _theplu_svndigest_utility_
    2 #define _theplu_svndigest_utility_
     1#ifndef _theplu_svndigest_functor_
     2#define _theplu_svndigest_functor_
    33
    44// $Id$
     
    2525*/
    2626
    27 #include <algorithm>
     27//#include <algorithm>
    2828#include <functional>
    29 #include <iosfwd>
     29//#include <iosfwd>
    3030#include <string>
    3131#include <utility>
     
    3636namespace theplu{
    3737namespace 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   /// @return everything after last '/'
    65   ///
    66   std::string file_name(const std::string&);
    67 
    68   ///
    69   /// @brief environment variable @a var
    70   ///
    71   std::string getenv(const std::string& var);
    72 
    73   ///
    74   /// If @a width is set, size is forced to length @a width. This
    75   /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
    76   /// respectively.
    77   ///
    78   /// @return x in hexadecimal base
    79   ///
    80   std::string hex(int x, u_int width=0);
    81 
    82   ///
    83   /// @brief remove trailing whitespaces
    84   ///
    85   std::string htrim(std::string str);
    86 
    87   ///
    88   /// @return true if \a str is an integer
    89   ///
    90   bool is_int(std::string str);
    91 
    92   ///
    93   /// @brief remove leading whitespaces
    94   ///
    95   std::string ltrim(std::string str);
    96 
    97   inline bool match_begin(std::string::const_iterator first,
    98                           std::string::const_iterator last,
    99                           const std::string& str)
    100   { return (std::distance(first, last)>=static_cast<int>(str.size()) &&
    101             std::equal(str.begin(), str.end(), first));
    102   }
    103 
    104   inline bool match_end(std::string::const_reverse_iterator first,
    105                         std::string::const_reverse_iterator last,
    106                         const std::string& str)
    107   { return (std::distance(first,last)>=static_cast<int>(str.size()) &&
    108             std::equal(str.rbegin(), str.rend(), first));
    109   }
    110 
    111   ///
    112   /// Create directory \a dir. The call can fail in many ways, cf. 'man
    113   /// mkdir'.
    114   ///
    115   inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
    116 
    117   ///
    118   /// @brief Check whether \a path already exists or not.
    119   ///
    120   /// @return True if \a path exists, false otherwise.
    121   ///
    122   bool node_exist(const std::string& path);
    123 
    124   /**
    125      @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
    126   */
    127   int percent(int a, int b);
    128 
    129   ///
    130   /// @return the current working directory.
    131   ///
    132   std::string pwd(void);
    133 
    134   ///
    135   /// Search finds a subsecuence in [first, last) being identical to @ str
    136   ///
    137   /// @return iterator pointing to first character in found subsequence
    138   ///
    139   /// @see std::search
    140   ///
    141   inline std::string::iterator search(std::string::iterator first,
    142                                       std::string::iterator last,
    143                                       std::string str)
    144   { return std::search(first, last, str.begin(), str.end()); }
    145 
    146   ///
    147   ///
    148   ///
    149   time_t str2time(const std::string&);
    150 
    151   ///
    152   /// If file does not exist create empty file.
    153   ///
    154   void touch(std::string);
    155 
    156   ///
    157   /// remove leading and trailing whitespaces
    158   ///
    159   inline std::string trim(std::string str) { return htrim(ltrim(str)); }
    160 
    161 
    162   template <typename T>
    163   std::string match(std::string::const_iterator& first,
    164                     const std::string::const_iterator& last,
    165                     const T& func)
    166   {
    167     std::string res;
    168     for (;first!=last && func(first); ++first)
    169       res.append(1,*first);
    170     return res;
    171   }
    172 
    173 
    174   std::string match(std::string::const_iterator& first,
    175                     const std::string::const_iterator& last,
    176                     std::string);
    17738
    17839  struct AlNum
  • trunk/lib/Makefile.am

    r448 r465  
    2727
    2828noinst_HEADERS = Alias.h ColumnStream.h Commitment.h Configuration.h css.h\
    29   Date.h Directory.h File.h first_page.h Gnuplot.h GnuplotFE.h \
     29  Date.h Directory.h File.h first_page.h Functor.h Gnuplot.h GnuplotFE.h \
    3030  HtmlStream.h html_utility.h LogIterator.h Node.h Parser.h rmdirhier.h \
    3131  Stats.h SVN.h SVNblame.h  \
     
    3535  Commitment.cc Configuration.cc \
    3636  css.cc Date.cc Directory.cc File.cc first_page.cc\
    37   Gnuplot.cc GnuplotFE.cc HtmlStream.cc \
     37  Functor.cc Gnuplot.cc GnuplotFE.cc  HtmlStream.cc \
    3838  html_utility.cc LogIterator.cc Node.cc Parser.cc \
    3939  rmdirhier.cc Stats.cc SVN.cc        \
  • trunk/lib/Stats.cc

    r454 r465  
    2525#include "Stats.h"
    2626
     27#include "Functor.h"
    2728#include "GnuplotFE.h"
    2829#include "SVNblame.h"
  • trunk/lib/Trac.cc

    r439 r465  
    2525
    2626#include "Configuration.h"
     27#include "Functor.h"
    2728#include "HtmlStream.h"
    2829#include "html_utility.h"
    29 #include "utility.h"
     30//#include "utility.h"
    3031
    3132namespace theplu{
  • trunk/lib/utility.cc

    r462 r465  
    223223  }
    224224
    225   notChar::notChar(char c)
    226     : char_(c)
    227   {}
    228 
    229 
    230   not2Char::not2Char(char c1, char c2)
    231     : char1_(c1), char2_(c2)
    232   {}
    233    
    234 
    235   not2Str::not2Str(std::string s1, std::string s2)
    236     : str1_(s1), str2_(s2)
    237   {}
    238    
    239225}} // end of namespace svndigest and namespace theplu
  • trunk/lib/utility.h

    r462 r465  
    176176                    std::string);
    177177
    178   struct AlNum
    179   {
    180     inline bool operator()(std::string::const_iterator i) const
    181     { return isalnum(*i); }
    182   };
    183 
    184 
    185   struct Digit
    186   {
    187     inline bool operator()(std::string::const_iterator i) const
    188     { return isdigit(*i); }
    189   };
    190 
    191 
    192   class notChar
    193   {
    194   public:
    195     notChar(char);
    196     inline bool operator()(std::string::const_iterator i) const
    197     { return *i!=char_; }
    198   private:
    199     char char_;
    200   };
    201 
    202 
    203   class not2Char
    204   {
    205   public:
    206     not2Char(char, char);
    207     inline bool operator()(std::string::const_iterator i) const
    208     { return *i!=char1_ && *i!=char2_; }
    209   private:
    210     const char char1_;
    211     const char char2_;
    212   };
    213 
    214   class not2Str
    215   {
    216   public:
    217     not2Str(std::string, std::string);
    218     inline bool operator()(std::string::const_iterator i) const
    219     {
    220       return !(std::equal(str1_.begin(), str1_.end(), i) ||
    221               std::equal(str2_.begin(), str2_.end(), i));
    222     }
    223 
    224   private:
    225     const std::string str1_;
    226     const std::string str2_;
    227   };
    228 
    229   ///
    230   /// Functor to be used on contaioners and works as standard less,
    231   /// but on the reversed container.
    232   ///
    233   /// Requirements on T is that has rend and rbegin. T::value_type
    234   /// must be comparable (i.e. have operator<)
    235   ///
    236   template <typename T>
    237   struct LessReversed
    238   {
    239     ///
    240     /// using std::lexicographical_compare on the reversed container
    241     ///
    242     inline bool operator()(const T& x, const T& y) const
    243     { return std::lexicographical_compare(x.rbegin(),x.rend(),
    244                                           y.rbegin(),y.rend()); }
    245   };
    246 
    247 
    248   ///
    249   /// @brief Functor comparing pairs using second.
    250   ///
    251   /// STL provides operator< for the pair.first element, but none for
    252   /// pair.second. This template provides this and can be used as the
    253   /// comparison object in generic functions such as the STL sort.
    254   ///
    255   template <typename T1, typename T2>
    256   struct pair_value_compare
    257   {
    258     ///
    259     /// @return true if x.second<y.second or (x.second==y.second and
    260     /// x.first<y.first)
    261     ///
    262     inline bool operator()(const std::pair<T1,T2>& x,
    263                            const std::pair<T1,T2>& y) {
    264       return ((x.second<y.second) ||
    265               (!(y.second<x.second) && (x.first<y.first)));
    266     }
    267   };
    268 
    269  
    270   ///
    271   /// Functor working on pair.second, using a user passed functor.
    272   ///
    273   template <typename T1, typename T2, typename T3>
    274   struct PairSecondCompare
    275   {
    276 
    277     ///
    278     /// @brief Constructor
    279     ///
    280     explicit PairSecondCompare(const T3& comp)
    281       : compare_(comp) {}
    282 
    283     ///
    284     /// @return compare(x.second, y.second) where compare is a
    285     /// internal comparison functor.
    286     ///
    287     inline bool operator()(const std::pair<T1,T2>& x,
    288                            const std::pair<T1,T2>& y) const
    289     { return compare_(x.second,y.second); }
    290 
    291   private:
    292     T3 compare_;
    293 
    294   };
    295  
    296   ///
    297   /// Calculating sum of two vectors.
    298   ///
    299   /// @return resulting vector
    300   ///
    301   template <typename T >
    302   struct VectorPlus :
    303     public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
    304   {
    305     std::vector<T> operator()(const std::vector<T>& u,
    306                               const std::vector<T>& v) const
    307     {
    308       if ( u.size() > v.size() ){
    309         std::vector<T> res(u.size());
    310         transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
    311         copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
    312         return res;
    313       }
    314  
    315       std::vector<T> res(v.size());
    316       transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
    317       if ( v.size() > u.size() )
    318         copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
    319       return res;
    320     }
    321 
    322   };
    323 
    324   ///
    325   /// @return resulting vector
    326   ///
    327   template <typename Key, typename T>
    328   struct PairValuePlus :
    329     public std::binary_function<std::vector<T>,
    330                                 std::pair<const Key, std::vector<T> >,
    331                                 std::vector<T> >
    332   {
    333     std::vector<T> operator()(const std::vector<T>& sum,
    334                               const std::pair<const Key,std::vector<T> >& p)
    335     {
    336       return VectorPlus<T>()(sum, p.second);
    337     }
    338   };
    339 
    340178}} // end of namespace svndigest end of namespace theplu
    341179
Note: See TracChangeset for help on using the changeset viewer.