1 | // $Id: Directory.cc 185 2006-09-06 02:39:18Z jari $ |
---|
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 | |
---|
42 | namespace theplu{ |
---|
43 | namespace 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, |
---|
74 | output_name()+"/")); |
---|
75 | else |
---|
76 | daughters_.push_back(new File(level_,fullpath,output_name()+"/")); |
---|
77 | break; |
---|
78 | case SVN::unresolved: |
---|
79 | throw NodeException(fullpath+" is not up to date"); |
---|
80 | case SVN::unversioned: ; // do nothing |
---|
81 | } |
---|
82 | } |
---|
83 | daughters_.sort(NodePtrLess()); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | Directory::~Directory(void) |
---|
88 | { |
---|
89 | for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i) |
---|
90 | delete *i; |
---|
91 | } |
---|
92 | |
---|
93 | bool Directory::dir(void) const |
---|
94 | { |
---|
95 | return true; |
---|
96 | } |
---|
97 | |
---|
98 | std::string Directory::href(void) const |
---|
99 | { |
---|
100 | return name() + "/index.html"; |
---|
101 | } |
---|
102 | |
---|
103 | const Stats& Directory::parse(const bool verbose) |
---|
104 | { |
---|
105 | stats_.reset(); |
---|
106 | // Directories themselved give no contribution to statistics. |
---|
107 | for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i) |
---|
108 | if (!(*i)->ignore()) |
---|
109 | stats_ += (*i)->parse(verbose); |
---|
110 | return stats_; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | const std::string Directory::node_type(void) const |
---|
115 | { |
---|
116 | return std::string("directory"); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void Directory::print(const bool verbose) const |
---|
121 | { |
---|
122 | mkdir(output_name()); |
---|
123 | std::string output(output_name() + "/index.html"); |
---|
124 | if (verbose) |
---|
125 | std::cout << "Printing output for " << path_ << std::endl; |
---|
126 | std::ofstream os(output.c_str()); |
---|
127 | print_header(os); |
---|
128 | os << "<p align=center>\n<img src='" |
---|
129 | << file_name(stats_.plot(output_name()+"/index.png", output_name())) |
---|
130 | << "' alt='[plot]' border=0><br>\n"; |
---|
131 | os << "<table class=\"listings\">\n"; |
---|
132 | os << "<thead>"; |
---|
133 | os << "<tr>\n"; |
---|
134 | os << "<th>Node</th>\n"; |
---|
135 | os << "<th>Lines</th>\n"; |
---|
136 | os << "<th>Code</th>\n"; |
---|
137 | os << "<th>Comments</th>\n"; |
---|
138 | os << "<th>Revision</th>\n"; |
---|
139 | os << "<th>Author</th>\n"; |
---|
140 | os << "</tr>\n</thead>\n"; |
---|
141 | os << "<tbody>"; |
---|
142 | |
---|
143 | bool dark=false; |
---|
144 | if (level_){ |
---|
145 | os << "<tr class=\"light\">\n"; |
---|
146 | os << "<td class=\"directory\" colspan=\"6\">"; |
---|
147 | anchor(os, "../index.html", "../"); |
---|
148 | os << "</td>\n</tr>\n"; |
---|
149 | dark=!dark; |
---|
150 | } |
---|
151 | |
---|
152 | // print html links to daughter nodes |
---|
153 | for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) { |
---|
154 | if (dark) |
---|
155 | (*d)->html_tablerow(os,"dark"); |
---|
156 | else |
---|
157 | (*d)->html_tablerow(os,"light"); |
---|
158 | dark = !dark; |
---|
159 | } |
---|
160 | if (dark) |
---|
161 | os << "<tr class=\"dark\">\n"; |
---|
162 | else |
---|
163 | os << "<tr class=\"light\">\n"; |
---|
164 | os << "<td>Total</td>\n"; |
---|
165 | os << "<td>" << stats_.lines() << "</td>\n"; |
---|
166 | os << "<td>" << stats_.code() << "</td>\n"; |
---|
167 | os << "<td>" << stats_.comments() << "</td>\n"; |
---|
168 | os << "<td>" << stats_.last_changed_rev() << "</td>\n"; |
---|
169 | os << "<td>" << author() << "</td>\n"; |
---|
170 | os << "</tr>\n"; |
---|
171 | os << "</table>\n"; |
---|
172 | os << "</p>\n"; |
---|
173 | print_footer(os); |
---|
174 | os.close(); |
---|
175 | |
---|
176 | // print daughter nodes, i.e, this function is recursive |
---|
177 | std::for_each(daughters_.begin(), daughters_.end(), |
---|
178 | std::bind2nd(std::mem_fun(&Node::print),verbose)); |
---|
179 | } |
---|
180 | |
---|
181 | }} // end of namespace svndigest and namespace theplu |
---|