Changeset 23 for trunk/lib


Ignore:
Timestamp:
Jan 2, 2006, 9:14:57 AM (17 years ago)
Author:
Peter Johansson
Message:

added print functionality

Location:
trunk/lib
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Directory.cc

    r22 r23  
    66
    77#include <algorithm>
     8#include <fstream>
    89#include <iostream>
    910#include <iterator>
     
    7071
    7172
    72   void Directory::print(void) {
    73     // Jari, temporary using this to print directory tree
    74     for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::print));
     73  void Directory::print(const std::string& path) const
     74  {
     75    // Peter, creation of dir should be done without system call
     76    std::string system_call("mkdir " + path + name()+">& /dev/null");
     77    system(system_call.c_str());
     78    std::string output(path + name() + "/index.html");
     79    std::ofstream os(output.c_str());
     80    print_header(os);
     81    os << std::endl;
     82    stats_.print(os);
     83    os << std::endl;
     84
     85    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++){
     86      (*i)->print_link(os);
     87      os << "<br/>" << std::endl;
     88    }
     89
     90    print_footer(os);
     91    os.close();
     92
     93    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++)
     94      (*i)->print(path+name()+"/");
     95  }
     96
     97  void Directory::print_link(std::ostream& os) const
     98  {
     99    os << "<a href=\"" << name() << "/index.html\">" << name() << "</a>";
    75100  }
    76101
  • trunk/lib/Directory.h

    r22 r23  
    3939    const Stats& parse(void);
    4040
    41     void print(void);
     41    void print(const std::string&) const;
     42
     43    void print_link(std::ostream&) const;
    4244
    4345    void purge(void);
     
    5052
    5153    typedef std::list<Node*>::iterator NodeIterator;
     54    typedef std::list<Node*>::const_iterator NodeConstIter_;
    5255    std::list<Node*> daughters_;
    5356  };
  • trunk/lib/File.cc

    r16 r23  
    2222  }
    2323
     24  void File::info(void)
     25  {
     26    std::string system_call = "svn info " + path_ + " > svnstat.tmp";
     27    int system_return = system(system_call.c_str());
     28    if (system_return){
     29      // Jari, throw exception.
     30      std::cerr << "svnstat: svn info " << path_ << std::endl;     
     31      exit(-1);
     32    }
     33    std::ifstream is("svnstat.tmp");
     34    std::string line;
     35    while (getline(is,line)){
     36      std::stringstream ss(line);
     37      std::string tag;
     38      getline(ss,tag,':');
     39      if (tag == std::string("Last Changed Author"))
     40        ss >> author_;
     41      else if (tag == std::string("Last Changed Rev"))
     42        ss >> revision_;
     43    }
     44  }
     45
     46
    2447  const Stats& File::parse(void)
    2548  {
    2649    stats_.reset();
     50    info();
     51
    2752    if (binary_){
    2853      stats_.add(author_,revision_);
     
    4166      getline(is,line,' ');
    4267      if (line==std::string("binary")){
    43   is.close();
    44   binary_ = true;
    45   return parse();
     68        is.close();
     69        binary_ = true;
     70        return parse();
    4671      }
    4772    }
     
    5176    while (getline(is,line, '\n')){
    5277      if (!line.size()) // skip empty line
    53   continue;
     78        continue;
    5479      std::stringstream ss(line);
    5580      u_int revision;
     
    6388  }
    6489
    65   void File::print(void) {
    66     // Jari, temporary using this to print directory tree
    67     std::cout << "File '" << path_ << "'" << std::endl;
     90  void File::print(const std::string& path) const
     91  {
     92    std::string output(path + name() + ".html");
     93    std::ofstream os(output.c_str());
     94    print_header(os);
     95    os << std::endl;
     96    stats_.print(os);
     97    os << std::endl;
     98    print_footer(os);
     99    os.close();
     100
     101  }
     102
     103  void File::print_link(std::ostream& os) const
     104  {
     105    os << "<a href=\"" << name() << ".html\">" << name() << "</a>";
    68106  }
    69107
  • trunk/lib/File.h

    r15 r23  
    2626    const Stats& parse(void);
    2727
    28     void print(void);
     28    ///
     29    ///
     30    ///
     31    void print(const std::string& path) const;
     32
     33    ///
     34    ///
     35    ///
     36    void print_link(std::ostream&) const;
    2937
    3038  private:
    31     ///
    32     /// @brief Copy Constructor, not implemented
    33     ///
    34     File(const File&);
    35 
    3639    ///
    3740    /// @brief Parsing svn blame output
     
    4144    bool blame(void) const;
    4245   
    43    
     46    ///
     47    /// @brief Copy Constructor, not implemented
     48    ///
     49    File(const File&);
     50
     51    ///
     52    /// Extracts information from 'svn info <node>'
     53    ///
     54    /// @note <node> must be in subversion control.
     55    ///
     56    void info(void);
     57
     58    std::string author_;
    4459    bool binary_;
     60    u_int revision_;
    4561 
    4662  };
  • trunk/lib/Node.cc

    r20 r23  
    44#include "utility.h"
    55
     6#include <ctime>
    67#include <fstream>
    78#include <iostream>
     
    1112namespace svnstat{
    1213
    13   void Node::info(void)
    14   {
    15     std::string system_call = "svn info " + path_ + " > svnstat.tmp";
    16     int system_return = system(system_call.c_str());
    17     if (system_return){
    18       // Jari, throw exception.
    19       std::cerr << "Error: svn info " << path_ << std::endl;     
    20       exit(-1);
    21     }
    22     std::cerr << "Ok: svn info " << path_ << std::endl;     
    23     std::ifstream is("svnstat.tmp");
    24     std::string line;
    25     while (getline(is,line)){
    26       std::stringstream ss(line);
    27       std::string tag;
    28       getline(ss,tag,':');
    29       if (tag == std::string("Repository UUID"))
    30         ss >> uuid_;
    31       else if (tag == std::string("Last Changed Author"))
    32         ss >> author_;
    33       else if (tag == std::string("Last Changed Rev"))
    34         ss >> revision_;
    35     }
    36   }
     14  std::string Node::name(void) const
     15  {
     16    std::stringstream ss(path_);
     17    std::string name;
     18    while (getline(ss,name,'/')) {}
     19    return name;
     20  }
     21
     22
     23  void Node::print_footer(std::ostream& os) const
     24  {
     25    os << "</body>\n"
     26       << "</html>";
     27  }
     28
     29
     30  void Node::print_header(std::ostream& os) const
     31  {
     32    os << "<html>\n"
     33       << "<head>\n"
     34       << "<title> svnstat " << name() << "\n"
     35       << "</head>\n"
     36       << "<body bgcolor='FFFBFB'\n";
     37  }
    3738
    3839
  • trunk/lib/Node.h

    r20 r23  
    2727    virtual inline ~Node(void) {};
    2828
    29     ///
    30     /// Extracts information from 'svn info <node>'
    31     ///
    32     /// @note <node> must be in subversion control.
    33     ///
    34     void info(void);
    35 
    3629    ///
    3730    /// @brief parsing file using svn blame.
     
    4235    /// Function printing HTML in directory path
    4336    ///
    44     virtual void print(void)=0;
     37    virtual void print(const std::string& path) const=0;
    4538
     39    ///
     40    /// Prints a html link.
     41    ///
     42    virtual void print_link(std::ostream&) const=0;
     43
     44    ///
     45    ///
     46    ///
     47    // Peter to Jari, inlining virtual functions?
    4648    inline virtual void purge(void) { /* Nothing to be done */ };
    4749
     
    5557
    5658  protected:
    57     std::string author_;
     59    ///
     60    /// Function returning everything after the last '/'
     61    ///
     62    /// @return name of node (not full path)
     63    ///
     64    std::string name(void) const;
     65
     66    ///
     67    /// @brief print html footer of page
     68    ///
     69    void print_footer(std::ostream&) const;
     70   
     71    ///
     72    /// @brief print html header of page
     73    ///
     74    void print_header(std::ostream&) const;
     75
    5876    std::string path_;
    59     u_int revision_;
    6077    Stats stats_;
    61     std::string uuid_;
    6278
    6379  private:
  • trunk/lib/Stats.cc

    r14 r23  
    77#include <numeric>
    88#include <string>
     9#include <utility>
    910#include <vector>
     11
     12#include <iostream>
    1013
    1114namespace theplu{
     
    5962  }
    6063
     64  void Stats::print(std::ostream& os) const
     65  {
     66    std::vector<std::pair<std::string, std::vector<u_int> > > accum;
     67    for (MapConstIter_ i= map_.begin(); i != map_.end(); i++){
     68      std::pair<std::string, std::vector<u_int> > element(*i);
     69      std::partial_sum(element.second.begin(),element.second.end(),
     70                       element.second.begin());
     71      accum.push_back(element);
     72    }
     73    sort(accum.begin(),accum.end(), CodingMore());
     74    os << "<table>\n";
     75    for (size_t i = 0; i<accum.size(); i++){
     76      os << "<tr><td>\n" << accum[i].first << "</td><td>"
     77         << accum[i].second.back() << "</td></tr>";
     78    }
     79    os << "</table>\n";
     80
     81    os << "<p>[Plot to be here.]</p>\n";         
     82  }
     83
    6184  Stats& Stats::operator+=(const Stats& other)
    6285  {
    63     for (_MapConstIt o_i= other.map_.begin(); o_i != other.map_.end(); ++o_i)
     86    for (MapConstIter_ o_i= other.map_.begin(); o_i != other.map_.end(); ++o_i)
    6487    {
    65       std::pair<_MapIt,bool> result = map_.insert(*o_i);
     88      std::pair<MapIter_,bool> result = map_.insert(*o_i);
    6689      if (!result.second)
    67   map_[(*(result.first)).first] =
    68     VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
     90        map_[(*(result.first)).first] =
     91          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
    6992 
    7093    }
  • trunk/lib/Stats.h

    r14 r23  
    2222   
    2323    ///
     24    /// @brief adding a line to user from revision to the stats
     25    ///
     26    void add(const std::string& user, const u_int& revision);
     27
     28    ///
     29    /// @bief print statistics
     30    ///
     31    void print(std::ostream&) const;
     32
     33    ///
     34    /// @brief Clear all statistics
     35    ///
     36    inline void reset(void) { map_.clear(); }
     37
     38    ///
     39    /// @return resulting Stats
     40    ///
     41    Stats& operator+=(const Stats&);
     42
     43  private:
     44    ///
     45    /// Copy constructor (not implemented)
     46    ///
     47    Stats(const Stats& other);
     48
     49    ///
    2450    /// @return accumulated vector of total
    2551    ///
     
    3157    std::vector<u_int> accumulated(const std::string& user);
    3258
    33     ///
    34     /// @brief adding a line to user from revision to the stats
    35     ///
    36     void add(const std::string& user, const u_int& revision);
    37 
    38     inline void reset(void) { map_.clear(); }
    39 
    40 
    41     ///
    42     /// @return resulting Stats
    43     ///
    44     Stats& operator+=(const Stats&);
    45 
    46   private:
    47     Stats(const Stats& other);
    48 
    4959    // Peter, if the vector is sparse make it a map
    50     typedef std::map<std::string, std::vector<u_int> > _Map;
    51     typedef _Map::iterator _MapIt;
    52     typedef _Map::const_iterator _MapConstIt;
    53     _Map map_;
     60    typedef std::map<std::string, std::vector<u_int> > Map_;
     61    typedef Map_::iterator MapIter_;
     62    typedef Map_::const_iterator MapConstIter_;
     63    Map_ map_;
    5464  };
    5565}}
  • trunk/lib/utility.h

    r14 r23  
    66#include <algorithm>
    77#include <functional>
     8#include <string>
    89#include <utility>
    910#include <vector>
     
    1213namespace svnstat{
    1314
    14   ///
    15   /// Calculating sum of two vectors.
    16   ///
    17   /// @return resulting vector
    18   ///
    19   template <class T >
    20     struct VectorPlus :
    21   public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
    22     {
    23       std::vector<T> operator()(const std::vector<T>& u,
    24         const std::vector<T>& v) const
    25       {
    26   if ( u.size() > v.size() ){
    27     std::vector<T> res(u.size());
    28     transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
    29     copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
    30     return res;
    31   }
     15  ///
     16  /// Calculating sum of two vectors.
     17  ///
     18  /// @return resulting vector
     19  ///
     20  template <class T >
     21  struct VectorPlus :
     22    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
     23  {
     24    std::vector<T> operator()(const std::vector<T>& u,
     25                              const std::vector<T>& v) const
     26    {
     27      if ( u.size() > v.size() ){
     28        std::vector<T> res(u.size());
     29        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
     30        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
     31        return res;
     32      }
    3233 
    33   std::vector<T> res(v.size());
    34   transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
    35   if ( v.size() > u.size() )
    36     copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
    37   return res;
    38       }
     34      std::vector<T> res(v.size());
     35      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
     36      if ( v.size() > u.size() )
     37        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
     38      return res;
     39    }
    3940
    40     };
     41  };
    4142
    4243  ///
     
    4445  ///
    4546  template <class Key, class T>
    46     struct PairValuePlus :
    47   public std::binary_function<std::vector<T>,
    48     std::pair<const Key, std::vector<T> >,
    49     std::vector<T> >
    50     {
    51       std::vector<T> operator()(const std::vector<T>& sum,
    52         const std::pair<const Key,std::vector<T> >& p)
    53       {
    54   return VectorPlus<T>()(sum, p.second);
    55       }
    56     };
     47  struct PairValuePlus :
     48    public std::binary_function<std::vector<T>,
     49                                std::pair<const Key, std::vector<T> >,
     50                                std::vector<T> >
     51  {
     52    std::vector<T> operator()(const std::vector<T>& sum,
     53                              const std::pair<const Key,std::vector<T> >& p)
     54    {
     55      return VectorPlus<T>()(sum, p.second);
     56    }
     57  };
     58
     59  ///
     60  struct CodingMore :
     61    public std::binary_function<std::pair<std::string,std::vector<u_int> >,
     62                                std::pair<std::string,std::vector<u_int> >,
     63                                bool >
     64  {
     65    inline bool operator()
     66      (const std::pair<std::string,std::vector<u_int> >& a,
     67       const std::pair<std::string,std::vector<u_int> >& b)
     68    {
     69      if (a.second.back() > b.second.back())
     70        return true;
     71      else if (a.second.back() < b.second.back())
     72        return false;
     73      return a.first < b.first;
     74    }
     75  };
     76
    5777}}
    5878
    59 #endif // end of namespace svnstat end of namespace theplu
     79// end of namespace svnstat end of namespace theplu
     80#endif
Note: See TracChangeset for help on using the changeset viewer.