source: trunk/lib/Directory.cc @ 101

Last change on this file since 101 was 101, checked in by Peter Johansson, 15 years ago

closes #28 #18 and added some lippstick to the node daughters table output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1// $Id: Directory.cc 101 2006-06-22 09:00:06Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat 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  svnstat 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 "Node.h"
27#include "SVN.h"
28#include "utility.h"
29
30#include <algorithm>
31#include <fstream>
32#include <functional>
33#include <iostream>
34#include <iterator>
35#include <list>
36
37#include <cerrno> // Needed to check error state below.
38#include <dirent.h>
39#include <sys/stat.h>
40
41namespace theplu{
42namespace svnstat{
43
44
45  Directory::Directory(const std::string& path, const std::string& output)
46    : Node(path,output)
47  {
48    using namespace std;
49    DIR* directory=opendir(path.c_str());    // C API from dirent.h
50    if (!directory)
51      throw NodeException("ERROR: opendir() failed; " + path +
52                          " is not a directory");
53    list<string> entries;
54    struct dirent* entry;
55    errno=0;  // Global variable used by C to track errors, from errno.h
56    while ((entry=readdir(directory)))       // C API from dirent.h
57      entries.push_back(string(entry->d_name));
58    if (errno)
59      throw NodeException("ERROR: readdir() failed on " + path);
60    closedir(directory);
61
62    SVN* svn=SVN::instance();
63    for (list<string>::iterator i=entries.begin(); i!=entries.end(); i++)
64      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
65        string fullpath(path_+'/'+(*i));
66        switch (svn->version_controlled(fullpath)) {
67        case SVN::uptodate:
68          struct stat nodestat;                // C api from sys/stat.h
69          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
70          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
71            daughters_.push_back(new Directory(fullpath, output_name()+"/"));
72          else
73            daughters_.push_back(new File(fullpath,output_name()+"/"));
74          break;
75        case SVN::unresolved:
76          throw NodeException(fullpath+" is not up to date");
77        case SVN::unversioned: ; // do nothing
78        }
79      }
80    daughters_.sort(NodePtrLess());
81  }
82
83
84  Directory::~Directory(void)
85  {
86    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
87      delete *i;
88  }
89
90  bool Directory::dir(void) const
91  {
92    return true;
93  }
94
95  std::string Directory::html_link(void) const
96  { 
97    return "<a href=\"" + name() + "/index.html\">" + name() + "</a>"; 
98  }
99
100  const Stats& Directory::parse(const bool verbose)
101  {
102    stats_.reset();
103
104    // Directories give no contribution to statistics.
105    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
106      stats_ += (*i)->parse(verbose);
107    return stats_;
108  }
109
110
111  void Directory::print(const bool verbose) const
112  {
113    createdir(output_name());
114    std::string output(output_name() + "/index.html");
115    if (verbose)
116      std::cout << "Printing output for " << path_ << std::endl;
117    std::ofstream os(output.c_str());
118    print_header(os);
119    os << "<p align=center>\n<img src='" 
120       << file_name(stats_.plot(output_name()+"/index.png", output_name()))
121       << "' alt='[plot]' border=0><br>\n";
122    os << "<table>\n";
123    os << "<tr>\n<td><strong>Node</strong></td>\n";
124    os << "<td><strong>Count</strong></td>\n</tr>\n";
125    os << "<tr>\n<td bgcolor=#dddddd>Total</td>\n";
126    os << "<td align=right bgcolor=#dddddd>" << stats_.rows() 
127       << "</td>\n</tr>\n";
128
129    // print html links to daughter nodes
130    bool dark=true;
131    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); d++){
132      if (dark)
133        os << (*d)->html_tablerow("#eeeeee");
134      else
135        os << (*d)->html_tablerow("#dddddd");
136      dark = !dark;
137    }
138    os << "</table>\n";
139    os << "</p>\n";
140    print_footer(os);
141    os.close(); 
142
143    // print daughter nodes, i.e, this function is recursive
144    std::for_each(daughters_.begin(), daughters_.end(),
145                  std::bind2nd(std::mem_fun(&Node::print),verbose));
146  }
147
148}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.