source: trunk/lib/Directory.cc @ 207

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

refs #41 and #44. Splitted print function in File and Directory to 2 functions. One public and one private. The private print a page for a specific author (or all) and specific code (or total), and the public calls this private function. Also added total number of commits #refs 87. NOTE the change of file structure forced all local paths to be relative from target/projectname rather than target as before.

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