source: trunk/lib/Directory.cc @ 258

Last change on this file since 258 was 258, checked in by Peter Johansson, 16 years ago

moving some printing code to base class

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1// $Id: Directory.cc 258 2007-04-30 12:08:25Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Directory.h"
26#include "File.h"
27#include "html_utility.h"
28#include "Node.h"
29#include "SVN.h"
30#include "SVNlog.h"
31#include "utility.h"
32
33#include <algorithm>
34#include <cassert>
35#include <fstream>
36#include <functional>
37#include <iostream>
38#include <iterator>
39#include <list>
40#include <map>
41
42#include <cerrno> // Needed to check error state below.
43#include <dirent.h>
44#include <sys/stat.h>
45
46namespace theplu{
47namespace svndigest{
48
49
50  Directory::Directory(const u_int level, const std::string& path, 
51                       const std::string& output)
52    : Node(level,path,output)
53  {
54    using namespace std;
55    DIR* directory=opendir(path.c_str());    // C API from dirent.h
56    if (!directory)
57      throw NodeException("ERROR: opendir() failed; " + path +
58                          " is not a directory");
59    list<string> entries;
60    struct dirent* entry;
61    errno=0;  // Global variable used by C to track errors, from errno.h
62    while ((entry=readdir(directory)))       // C API from dirent.h
63      entries.push_back(string(entry->d_name));
64    if (errno)
65      throw NodeException("ERROR: readdir() failed on " + path);
66    closedir(directory);
67
68    SVN* svn=SVN::instance();
69    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
70      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
71        string fullpath(path_+'/'+(*i));
72        switch (svn->version_controlled(fullpath)) {
73        case SVN::uptodate:
74          struct stat nodestat;                // C api from sys/stat.h
75          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
76          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
77            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
78          else
79            daughters_.push_back(new File(level_,fullpath,local_path()));
80          break;
81        case SVN::unresolved:
82          throw NodeException(fullpath+" is not up to date");
83        case SVN::unversioned: ; // do nothing
84        }
85      }
86    daughters_.sort(NodePtrLess());
87  }
88
89
90  Directory::~Directory(void)
91  {
92    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
93      delete *i;
94  }
95
96  bool Directory::dir(void) const
97  {
98    return true;
99  }
100
101  std::string Directory::href(void) const
102  { 
103    return name() + "/index.html";
104  }
105
106  const Stats& Directory::parse(const bool verbose)
107  {
108    stats_.reset();
109    // Directories themselved give no contribution to statistics.
110    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
111      if (!(*i)->ignore())
112        stats_ += (*i)->parse(verbose);
113    return stats_;
114  }
115
116
117  const std::string Directory::node_type(void) const
118  {
119    return std::string("directory");
120  }
121
122
123  void Directory::print_core(const bool verbose) const
124  {
125    // print daughter nodes, i.e., this function is recursive
126    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
127      (*i)->print(verbose);
128  }
129
130
131  void Directory::print_core(const std::string& user, 
132                             const std::string& line_type,
133                             const SVNlog& log) const
134  {
135    std::string outdir = user+"/"+line_type+"/"+local_path_;
136    if (local_path_=="")
137      outdir = user+"/"+line_type;
138
139    mkdir(outdir);
140    std::string imagedir = "images/"+line_type+"/"+local_path_;
141    if (user=="all")
142      mkdir(imagedir);
143
144    std::string html_name = outdir+"/index.html";
145    std::ofstream os(html_name.c_str());
146    assert(os.good());
147    if (local_path().empty())
148      print_header(os, name(), level_+2, user, line_type, "index.html");
149    else
150      print_header(os, name(), level_+2, user, line_type, 
151                   local_path()+"/index.html");
152    path_anchor(os);
153    os << "<p align=center>\n<img src='"; 
154    for (size_t i=0; i<level_; ++i)
155      os << "../";
156    os << "../../";
157    if (user=="all")
158      os << stats_.plot(imagedir+"/index.png", line_type);
159    else
160      os << imagedir << "/index.png";
161    os << "' alt='[plot]' border=0><br>\n";
162    os << "<table class=\"listings\">\n";
163    os << "<thead>";
164    os << "<tr>\n";
165    os << "<th>Node</th>\n";
166    os << "<th>Lines</th>\n";
167    os << "<th>Code</th>\n";
168    os << "<th>Comments</th>\n";
169    os << "<th>Other</th>\n";
170    os << "<th>Revision</th>\n";
171    os << "<th>Author</th>\n";
172    os << "</tr>\n</thead>\n";
173    os << "<tbody>";
174
175    std::string color("light");
176    if (level_){
177      os << "<tr class=\"light\">\n";
178      os << "<td class=\"directory\" colspan=\"6\">";
179      os << anchor("../index.html", "../");
180      os << "</td>\n</tr>\n";
181      color = "dark";
182    }
183
184    // print html links to daughter nodes
185    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
186      (*d)->html_tablerow(os,color, user);
187      if (color=="dark")
188        color = "light";
189      else
190        color = "dark";
191    }
192    os << "<tr class=\"" << color << "\">\n";
193    os << "<td>Total</td>\n";
194    if (user=="all"){
195      os << "<td>" << stats_.lines() << "</td>\n";
196      os << "<td>" << stats_.code() << "</td>\n";
197      os << "<td>" << stats_.comments() << "</td>\n";
198      os << "<td>" << stats_.empty() << "</td>\n";
199    }
200    else {
201      os << "<td>" << stats_.lines(user) << "</td>\n";
202      os << "<td>" << stats_.code(user) << "</td>\n";
203      os << "<td>" << stats_.comments(user) << "</td>\n";
204      os << "<td>" << stats_.empty(user) << "</td>\n";
205    }
206    os << "<td>" << stats_.last_changed_rev() << "</td>\n";
207    os << "<td>" << author() << "</td>\n";
208    os << "</tr>\n";
209    os << "</table>\n";
210    print_author_summary(os, line_type, log);
211    os << "</p>\n";
212    print_footer(os);
213    os.close(); 
214  }
215
216
217  void Directory::print_copyright
218  (std::map<std::string, std::string>& alias) const {
219    if (!ignore()){
220      // print daughter nodes, i.e, this function is recursive
221      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
222        (*i)->print_copyright(alias);
223    }
224  }
225
226}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.