Ignore:
Timestamp:
Nov 12, 2010, 5:01:35 AM (11 years ago)
Author:
Peter Johansson
Message:

closes #457. Lifting out plotting functions in Stats into a new class
StatsPlotter?. In same manner lifting out print functions from
Node/Directory/File? into a new hierarchy
NodePrinter/DirectoryPrinter/FilePrinter?. To accomplish these splits
without to much recoding some friendships are introduced. These new
classes are only needed for svndigest (not svncopyright) and are
therefore placed in a library named libsvndigest together with Graph
class and first_page functions. Remaining functionality, shared
between svncopyright and svndigest, are located in libsvndigest_core
and linked into both binaries.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/lib/DirectoryPrinter.cc

    r1289 r1290  
    2020*/
    2121
    22 #include "Directory.h"
     22#include "DirectoryPrinter.h"
    2323
    2424#include "Alias.h"
    2525#include "Configuration.h"
     26#include "Directory.h"
    2627#include "File.h"
    2728#include "html_utility.h"
    2829#include "Node.h"
    2930#include "NodeVisitor.h"
     31#include "StatsPlotter.h"
    3032#include "SVN.h"
    3133#include "SVNlog.h"
     
    5153
    5254
    53   Directory::Directory(const unsigned int level, const std::string& path,
    54                        const std::string& output, const std::string& project)
    55     : Node(level,path,output,project)
     55  DirectoryPrinter::DirectoryPrinter(const Directory& dir)
     56    : NodePrinter(), directory_(dir)
    5657  {
    57     output_dir_=local_path();
    58     if (!output_dir_.empty())
    59       output_dir_+='/';
    60 
    61     using namespace std;
    62     DIR* directory=opendir(path.c_str());    // C API from dirent.h
    63     if (!directory)
    64       throw NodeException("ERROR: opendir() failed; " + path +
    65                           " is not a directory");
    66     list<string> entries;
    67     struct dirent* entry;
    68     errno=0;  // Global variable used by C to track errors, from errno.h
    69     while ((entry=readdir(directory)))       // C API from dirent.h
    70       entries.push_back(string(entry->d_name));
    71     if (errno)
    72       throw NodeException("ERROR: readdir() failed on " + path);
    73     closedir(directory);
    74 
    75     SVN* svn=SVN::instance();
    76     for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
    77       if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
    78         string fullpath(path_+'/'+(*i));
    79         switch (svn->version_controlled(fullpath)) {
    80         case SVN::uptodate:
    81           struct stat nodestat;                // C api from sys/stat.h
    82           lstat(fullpath,&nodestat);   // C api from sys/stat.h
    83           if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
    84             daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
    85           else
    86             daughters_.push_back(new File(level_,fullpath,local_path()));
    87           break;
    88         case SVN::unresolved:
    89           throw NodeException(fullpath+" is not up to date");
    90         case SVN::unversioned: ; // do nothing
    91         }
    92       }
    93     std::sort(daughters_.begin(), daughters_.end(), NodePtrLess());
    9458  }
    9559
    9660
    97   Directory::~Directory(void)
     61  DirectoryPrinter::~DirectoryPrinter(void)
    9862  {
    99     for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
    100       delete *i;
    10163  }
    10264
    10365
    104   void Directory::collect_stats(void)
     66  const Node& DirectoryPrinter::node(void) const
    10567  {
    106     stats_.reset();
    107     for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
    108       if (!(*i)->ignore()) {
    109         stats_ += (*i)->stats();
    110         (*i)->stats().reset();
    111       }
     68    return directory_;
    11269  }
    11370
    11471
    115   bool Directory::dir(void) const
    116   {
    117     return true;
    118   }
    119 
    120 
    121   std::string Directory::href(void) const
    122   {
    123     return name() + "/index.html";
    124   }
    125 
    126 
    127   svn_revnum_t Directory::last_changed_rev(void) const
    128   {
    129     svn_revnum_t res = svn_info().last_changed_rev();
    130     for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
    131       res = std::max(res, (*i)->last_changed_rev());
    132     return res;
    133   }
    134 
    135 
    136   void Directory::log_core(SVNlog& log) const
    137   {
    138     for (NodeConstIterator i(daughters_.begin()); i != daughters_.end(); ++i)
    139       log += (*i)->log();
    140   }
    141 
    142   std::string Directory::node_type(void) const
    143   {
    144     return std::string("directory");
    145   }
    146 
    147 
    148   std::string Directory::output_path(void) const
     72  std::string DirectoryPrinter::output_path(void) const
    14973  {
    15074    return output_dir()+"index.html";
     
    15276
    15377
    154   void Directory::print_core(const bool verbose) const
     78  void DirectoryPrinter::print_core(const bool verbose) const
    15579  {
    15680  }
    15781
    15882
    159   void Directory::print_core(const std::string& stats_type,
     83  void DirectoryPrinter::print_core(const std::string& stats_type,
    16084                             const std::string& user,
    16185                             const std::string& line_type,
     
    16387  {
    16488
    165     const Stats& stats = stats_[stats_type];
     89    const Stats& stats = directory_.stats_[stats_type];
    16690    std::string imagedir = stats_type+"/"+"images/"+line_type;
    16791    std::string outdir   = stats_type+"/"+user+"/" +line_type;
    168     if (local_path()!="") {
    169       imagedir += "/"+local_path();
    170       outdir   += "/"+local_path();
     92    if (node().local_path()!="") {
     93      imagedir += "/"+node().local_path();
     94      outdir   += "/"+node().local_path();
    17195    }
    17296    mkdir_p(outdir);
     
    176100    std::ofstream os(html_name.c_str());
    177101    assert(os.good());
    178     if (local_path().empty())
    179       print_header(os, name(), level_+3, user, line_type, "index.html",
     102    if (node().local_path().empty())
     103      print_header(os, node().name(), directory_.level_+3, user, line_type, "index.html",
    180104                   stats_type);
    181105    else
    182       print_header(os, name(), level_+3, user, line_type,
    183                    local_path()+"/index.html", stats_type);
     106      print_header(os, node().name(), directory_.level_+3, user, line_type,
     107                   node().local_path()+"/index.html", stats_type);
    184108    path_anchor(os);
    185109
    186110    std::stringstream ss;
    187     for (size_t i=0; i<level_; ++i)
     111    for (size_t i=0; i<directory_.level_; ++i)
    188112      ss << "../";
    189113    ss << "../../../";
    190114    if (user=="all")
    191       ss << stats.plot(imagedir+"/index", line_type);
     115      ss << StatsPlotter(stats).plot(imagedir+"/index", line_type);
    192116    else
    193117      ss << imagedir << "/index";
     
    214138
    215139    std::string color("light");
    216     if (level_){
     140    if (directory_.level_){
    217141      os << "<tr class=\"light\">\n";
    218142      os << "<td class=\"directory\" colspan=\"7\">";
     
    223147
    224148    // print html links to daughter nodes
    225     for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
    226       (*d)->html_tablerow(os,stats_type, color, user);
     149    for (NodeConstIterator d = directory_.daughters_.begin();
     150         d!=directory_.daughters_.end(); ++d) {
     151
     152      (*d)->html_tablerow(os, stats_type, color, user);
    227153      if (color=="dark")
    228154        color = "light";
     
    256182      os << "</td>\n";
    257183    }
    258     os << "<td>" << trac_revision(last_changed_rev()) << "</td>\n";
    259     os << "<td>" << author() << "</td>\n";
     184    os << "<td>" << trac_revision(node().last_changed_rev()) << "</td>\n";
     185    os << "<td>" << node().author() << "</td>\n";
    260186    os << "</tr>\n";
    261187    os << "</tbody>\n";
     
    268194
    269195
    270   void Directory::traverse(NodeVisitor& visitor)
    271   {
    272     if (visitor.enter(*this))
    273       for (NodeIterator first=daughters_.begin(), end=daughters_.end();
    274            first!=end; ++first)
    275         (*first)->traverse(visitor);
    276     visitor.leave(*this);
    277   }
    278 
    279 
    280196}} // end of namespace svndigest and namespace theplu
Note: See TracChangeset for help on using the changeset viewer.