Changeset 10 for trunk/lib/Directory.cc


Ignore:
Timestamp:
Dec 30, 2005, 12:57:57 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Reading directory structure.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Directory.cc

    r9 r10  
    22
    33#include "Directory.h"
     4#include "File.h"
    45#include "Node.h"
    56
     
    1617    : Node(path)
    1718  {
     19    using namespace std;
    1820    DIR* directory=opendir(path.c_str());
    1921    if (!directory) {
    2022      // Jari, throw exception
    21       std::cerr << "ERROR: opendir() failed; terminating" << std::endl;
     23      cerr << "ERROR: opendir() failed; terminating" << endl;
    2224      exit(1);
    2325    }
     26    vector<string> entries;
    2427    struct dirent* entry;
    25     errno=0;
    26     std::vector<std::string> entries;
     28    errno=0;  // Global variable used by C to track errors.
    2729    while ((entry=readdir(directory)))
    28       entries.push_back(std::string(entry->d_name)); // Jari, push backing
     30      entries.push_back(string(entry->d_name)); // Jari, push backing
    2931    if (errno) {
    3032      // Jari, throw exception
    31       std::cerr << "ERROR: readdir() failure; terminating" << std::endl;
     33      cerr << "ERROR: readdir() failure; terminating" << endl;
    3234      exit(1);
    3335    }
    3436    closedir(directory);
    3537
    36     copy(entries.begin(),entries.end(),
    37          std::ostream_iterator<std::string>(std::cout,"\n"));
     38    // Jari, change this to some STL algo?
     39    for (vector<string>::iterator i=entries.begin(); i!=entries.end(); i++)
     40      if ((*i)!=string(".") && (*i)!=string("..")) {
     41        /// Jari, should test if directory in some other way
     42        DIR* dirtest=opendir(i->c_str());
     43        if (!dirtest)
     44          daughters_.push_back(new File(*i));
     45        else
     46          daughters_.push_back(new Directory(*i));
     47      }
    3848  }
     49
     50
     51  Directory::~Directory(void)
     52  {
     53    // Jari, change this to some STL algo?
     54    for (std::vector<Node*>::iterator i=daughters_.begin();
     55         i!=daughters_.end(); i++)
     56      delete *i;
     57  }
     58
    3959
    4060  bool Directory::parse(void)
    4161  {
    4262    // Jari, where is the include for for_each?
    43     for_each(nn_.begin(),nn_.end(), std::mem_fun(&Node::parse));
     63    for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::parse));
    4464    return true;
    4565  }
    4666
     67
     68  void Directory::print(void) {
     69    // Jari, temporary using this to print directory tree
     70    std::cout << "-------------- Directory '" << path_ << "'" << std::endl;
     71    for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::print));
     72  }
     73
    4774}} // end of namespace svnstat and namespace theplu
Note: See TracChangeset for help on using the changeset viewer.