source: trunk/lib/Directory.cc @ 100

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

closes #9 and move create function from svnstat.cc to lib/utility and also move rmdirhier to lib

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: Directory.cc 100 2006-06-19 09:53:16Z 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  }
81
82
83  Directory::~Directory(void)
84  {
85    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
86      delete *i;
87  }
88
89  std::string Directory::html_link(void) const
90  { 
91    return "<a href=\"" + name() + "/index.html\">" + name() + "</a>"; 
92  }
93
94  const Stats& Directory::parse(const bool verbose)
95  {
96    stats_.reset();
97
98    // Directories give no contribution to statistics.
99    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
100      stats_ += (*i)->parse(verbose);
101    return stats_;
102  }
103
104
105  void Directory::print(const bool verbose) const
106  {
107    createdir(output_name());
108    std::string output(output_name() + "/index.html");
109    if (verbose)
110      std::cout << "Printing output for " << path_ << std::endl;
111    std::ofstream os(output.c_str());
112    print_header(os);
113    os << "<p align=center>\n<img src='" 
114       << file_name(stats_.plot(output_name()+"/index.png"))
115       << "' alt='[plot]' border=0><br>\n";
116    os << "<table>\n";
117    os << "<tr><td><strong>Node</strong></td>\n";
118    os << "<td><strong>Count</strong></td></tr>\n";
119    os << "<tr><td>Total</td>\n";
120    os << "<td align=right>" << stats_.rows() << "</td></tr>\n";
121    // print html links to daughter nodes
122    transform(daughters_.begin(), daughters_.end(), 
123              std::ostream_iterator<std::string>(os," "), 
124              std::mem_fun(&Node::html_tablerow));
125    os << "</table>\n";
126    os << "</p>";
127    print_footer(os);
128    os.close(); 
129
130    // print daughter nodes, i.e, this function is recursive
131    std::for_each(daughters_.begin(), daughters_.end(),
132                  std::bind2nd(std::mem_fun(&Node::print),verbose));
133  }
134
135}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.