source: trunk/lib/Directory.cc @ 1290

Last change on this file since 1290 was 1290, checked in by Peter Johansson, 11 years ago

closes #457. Lifting out plotting functions in Stats into a new class
StatsPlotter?. In same manner lifting out print functions from
Node/Directory/File? into a new hierarchy
NodePrinter/DirectoryPrinter/FilePrinter?. To accomplish these splits
without to much recoding some friendships are introduced. These new
classes are only needed for svndigest (not svncopyright) and are
therefore placed in a library named libsvndigest together with Graph
class and first_page functions. Remaining functionality, shared
between svncopyright and svndigest, are located in libsvndigest_core
and linked into both binaries.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: Directory.cc 1290 2010-11-12 04:01:35Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://dev.thep.lu.se/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 3 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 svndigest. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Directory.h"
23
24#include "Alias.h"
25#include "Configuration.h"
26#include "File.h"
27#include "html_utility.h"
28#include "Node.h"
29#include "NodeVisitor.h"
30#include "SVN.h"
31#include "SVNlog.h"
32#include "TinyStats.h"
33#include "utility.h"
34
35#include <algorithm>
36#include <cassert>
37#include <fstream>
38#include <functional>
39#include <iostream>
40#include <iterator>
41#include <list>
42#include <map>
43#include <sstream>
44
45#include <cerrno> // Needed to check error state below.
46#include <dirent.h>
47#include <sys/stat.h>
48
49namespace theplu{
50namespace svndigest{
51
52
53  Directory::Directory(const unsigned int level, const std::string& path, 
54                       const std::string& output, const std::string& project)
55    : Node(level,path,output,project)
56  {
57    output_dir_=local_path();
58    if (!output_dir_.empty())
59      output_dir_+='/';
60
61    using namespace std;
62    DIR* directory=opendir(path.c_str());    // C API from dirent.h
63    if (!directory)
64      throw NodeException("ERROR: opendir() failed; " + path +
65                          " is not a directory");
66    list<string> entries;
67    struct dirent* entry;
68    errno=0;  // Global variable used by C to track errors, from errno.h
69    while ((entry=readdir(directory)))       // C API from dirent.h
70      entries.push_back(string(entry->d_name));
71    if (errno)
72      throw NodeException("ERROR: readdir() failed on " + path);
73    closedir(directory);
74
75    SVN* svn=SVN::instance();
76    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
77      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
78        string fullpath(path_+'/'+(*i));
79        switch (svn->version_controlled(fullpath)) {
80        case SVN::uptodate:
81          struct stat nodestat;                // C api from sys/stat.h
82          lstat(fullpath,&nodestat);   // C api from sys/stat.h
83          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
84            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
85          else
86            daughters_.push_back(new File(level_,fullpath,local_path()));
87          break;
88        case SVN::unresolved:
89          throw NodeException(fullpath+" is not up to date");
90        case SVN::unversioned: ; // do nothing
91        }
92      }
93    std::sort(daughters_.begin(), daughters_.end(), NodePtrLess());
94  }
95
96
97  Directory::~Directory(void)
98  {
99    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
100      delete *i;
101  }
102
103
104  void Directory::collect_stats(void)
105  {
106    stats_.reset();
107    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
108      if (!(*i)->ignore()) {
109        stats_ += (*i)->stats();
110        (*i)->stats().reset();
111      }
112  }
113
114
115  bool Directory::dir(void) const
116  {
117    return true;
118  }
119
120
121  std::string Directory::href(void) const
122  { 
123    return name() + "/index.html";
124  }
125
126
127  svn_revnum_t Directory::last_changed_rev(void) const
128  {
129    svn_revnum_t res = svn_info().last_changed_rev();
130    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
131      res = std::max(res, (*i)->last_changed_rev());
132    return res;
133  }
134
135
136  void Directory::log_core(SVNlog& log) const
137  {
138    for (NodeConstIterator i(daughters_.begin()); i != daughters_.end(); ++i)
139      log += (*i)->log();
140  }
141
142  std::string Directory::node_type(void) const
143  {
144    return std::string("directory");
145  }
146
147
148  void Directory::traverse(NodeVisitor& visitor)
149  {
150    if (visitor.enter(*this))
151      for (NodeIterator first=daughters_.begin(), end=daughters_.end();
152           first!=end; ++first)
153        (*first)->traverse(visitor);
154    visitor.leave(*this);
155  }
156
157
158}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.