Changeset 226


Ignore:
Timestamp:
Mar 11, 2007, 8:32:41 PM (14 years ago)
Author:
Peter Johansson
Message:

added reading of config file in Parameter closes ##106 and #98. Move function check_target from svndigest.cc to utility.cc and changed name to node_exist. added support for aliases in Copyright update refs #36

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/Parameter.cc

    r198 r226  
    33/*
    44  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
     5  Copyright (C) 2007 Peter Johansson
    56
    67  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
     
    2627#include <config.h> // this header file is created by configure
    2728
     29#include <fstream>
    2830#include <iostream>
     31#include <sstream>
    2932#include <stdexcept>
    3033#include <string>
     
    4043      bool ok=false;
    4144      std::string myargv(argv[i]);
    42       if (myargv=="-f" || myargv=="--force"){
     45      if (myargv=="-c" || myargv=="--config"){
     46        if (++i<argc){
     47          config_path_= std::string(argv[i]);
     48          ok=true;
     49        }
     50      }
     51      else if (myargv=="--copyright"){
     52          copyright_=true;
     53          ok=true;
     54      }
     55      else if (myargv=="-f" || myargv=="--force"){
    4356          force_=true;
    44           ok=true;
    45       }
    46       if (myargv=="--copyright"){
    47           copyright_=true;
    4857          ok=true;
    4958      }
     
    8695                                 "\nType 'svndigest --help' for usage.");
    8796    }
     97
     98    std::string home_path = getenv("HOME") + "/.svndigest";
     99    if (!node_exist(home_path))
     100      mkdir(home_path);
     101    if (!node_exist(home_path+"/config"))
     102      print_config(home_path+"/config");
     103    read_config(config_path_);
    88104
    89105    analyse();
     
    126142  void Parameter::defaults(void)
    127143  {
     144    config_path_ = getenv("HOME")+"/.svndigest/config";
    128145    copyright_=false;
    129146    force_=false;
     
    149166              << "\n"
    150167              << "Valid options:\n"
     168              << "  -c [--config] arg : path to config file ["
     169              << config_path_ << "]\n"
    151170              << "  -f [--force]   : remove target directory/file if it exists\n"
    152171              << "                   [no force]. NOTE recursive delete.\n"
     
    165184
    166185
     186  void Parameter::print_config(std::string path) const
     187  {
     188    std::ofstream os(path.c_str());
     189    if (!os.good()){
     190      os.close();
     191      throw std::runtime_error("svndigest: error: cannot create file '" +
     192                               path + "': permission denied");
     193    }
     194    os << "### This file configures various behaviors for svndigest\n"
     195       << "### The commented-out below are intended to demonstrate how to use\n"
     196       << "### this file.\n"
     197       << "\n"
     198       << "### Section for setting aliases used in copyright update\n"
     199       << "#[copyright-alias]\n"
     200       << "# jdoe = John Doe\n"
     201       << std::endl;
     202    os.close();
     203  }
     204
     205
     206  void Parameter::read_config(std::string path)
     207  {
     208    if (!node_exist(path))
     209      throw std::runtime_error("svndigest: error: configuration file '" +
     210                               path + "' not found");
     211    std::ifstream is(path.c_str());
     212    if (!is.good()){
     213      is.close();
     214      throw std::runtime_error("svndigest: error: cannot open file '" + path +
     215                               "' for reading");
     216    }
     217    std::string line;
     218    std::string section;
     219    std::string tmp;
     220    while (getline(is, line)) {
     221      line = ltrim(line);
     222      if (line.empty() || line[0]=='#')
     223        continue;
     224      std::stringstream ss(line);
     225      if (line[0] == '[') {
     226        getline(ss, tmp, '[');
     227        getline(ss, section, ']');
     228      }
     229      else if (section == "copyright-alias"){
     230        getline(ss, tmp, '=');
     231        std::string key = trim(tmp);
     232        getline(ss, tmp);
     233        std::string name = trim(tmp);
     234        copyright_alias_[key] = name;
     235      }
     236    }
     237    is.close();
     238  }
     239
     240
    167241  void Parameter::version(void) const
    168242  {
    169243    using namespace std;
    170244    cout << PACKAGE_STRING
    171          << "\nCopyright (C) 2006 Jari Häkkinen and Peter Johansson.\n\n"
     245         << "\nCopyright (C) 2006-2007 Jari Häkkinen and Peter Johansson.\n\n"
    172246         << "This is free software; see the source for copying conditions.\n"
    173247         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
  • trunk/bin/Parameter.h

    r225 r226  
    5252    void defaults(void);
    5353    void help(void);
     54    void print_config(std::string) const;
     55    void read_config(std::string);
    5456    void version(void) const;
    5557
     58    std::string config_path_;
    5659    bool copyright_;
    5760    std::map<std::string, std::string> copyright_alias_;
  • trunk/bin/svndigest.cc

    r225 r226  
    4040#include <unistd.h>
    4141
    42 ///
    43 /// @brief Check whether \a path already exists or not.
    44 ///
    45 /// @return True if \a path exists, false otherwise.
    46 ///
    47 bool check_target(const std::string& path)
    48 {
    49   struct stat buf;
    50   return !stat(path.c_str(),&buf);
    51 }
    52 
    53 
    5442int main(const int argc,const char* argv[])
    5543{
     
    8371    std::cout << "Checking target directory" << std::endl;
    8472  std::string target_path=option->targetdir() + '/' + file_name(option->root());
    85   bool need_to_erase_target = check_target(target_path);
     73  bool need_to_erase_target = node_exist(target_path);
    8674  if (need_to_erase_target && !option->force())
    8775    throw std::runtime_error(std::string("svndigest: directory (") +
     
    162150  if (option->copyright()){
    163151    try {
    164       tree.print_copyright(commit_dates, authors);
     152      tree.print_copyright(commit_dates, authors, option->copyright_alias());
    165153    }
    166154    catch (const std::runtime_error& x) {
  • trunk/lib/Directory.cc

    r225 r226  
    3737#include <iterator>
    3838#include <list>
     39#include <map>
    3940
    4041#include <cerrno> // Needed to check error state below.
     
    223224
    224225
    225   void Directory::print_copyright(const std::vector<std::string>& dates,
    226                                   const std::vector<std::string>& authors) const
     226  void Directory::print_copyright
     227  (const std::vector<std::string>& dates,
     228   const std::vector<std::string>& authors,
     229   const std::map<std::string, std::string>& alias) const
    227230  {
    228231    if (!ignore()){
    229232      // print daughter nodes, i.e, this function is recursive
    230233      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
    231         (*i)->print_copyright(dates, authors);
     234        (*i)->print_copyright(dates, authors, alias);
    232235    }
    233236  }
  • trunk/lib/Directory.h

    r225 r226  
    2828
    2929#include <list>
     30#include <map>
    3031#include <string>
    3132
     
    8081
    8182    void print_copyright(const std::vector<std::string>& dates,
    82                          const std::vector<std::string>& author) const;
     83                         const std::vector<std::string>& author,
     84                         const std::map<std::string, std::string>&) const;
     85
    8386  private:
    8487    ///
  • trunk/lib/File.cc

    r225 r226  
    3232#include <fstream>
    3333#include <iostream>
     34#include <map>
    3435#include <string>
    3536
     
    153154
    154155
    155   void File::print_copyright(const std::vector<std::string>& dates,
    156                              const std::vector<std::string>& authors) const
     156  void File::print_copyright
     157  (const std::vector<std::string>& dates,
     158   const std::vector<std::string>& authors,
     159   const std::map<std::string, std::string>& alias) const
    157160  {
    158161    if (ignore())
     
    160163    assert(dates.size()==authors.size());
    161164    using namespace std;
     165    cout << "Copyright: " << local_path() << std::endl;
    162166    SVN* svn=SVN::instance();
    163167    vector<size_t> rev=svn->revisions(path());
     
    169173      time_t sec = str2time(dates[*i]);
    170174      tm* timeinfo = gmtime(&sec);
    171       copyright[timeinfo->tm_year].insert(authors[*i]);
     175
     176      // find username in map of aliases
     177      map<string, string>::const_iterator name = alias.find(authors[*i]);
     178      // if alias exist insert alias
     179      if (name != alias.begin() )
     180        copyright[timeinfo->tm_year].insert(name->second);
     181      else
     182        // else insert user name
     183        copyright[timeinfo->tm_year].insert(authors[*i]);
    172184    }
    173185   
  • trunk/lib/File.h

    r225 r226  
    2828#include "Node.h"
    2929
     30#include <map>
    3031#include <string>
    3132
     
    7071    void print_blame(std::ofstream&, const std::string line_type) const;
    7172
    72     void print_copyright(const std::vector<std::string>&,
    73                          const std::vector<std::string>&) const;
     73    void print_copyright(const std::vector<std::string>& dates,
     74                         const std::vector<std::string>& author,
     75                         const std::map<std::string, std::string>&) const;
     76
    7477  private:
    7578
  • trunk/lib/Node.h

    r225 r226  
    3131#include "utility.h"
    3232
     33#include <map>
    3334#include <ostream>
    3435#include <stdexcept>
     
    141142    virtual void print(const bool verbose=false) const=0;
    142143
    143     virtual void print_copyright(const std::vector<std::string>& dates,
    144                                  const std::vector<std::string>& author) const=0;
     144    virtual void
     145    print_copyright(const std::vector<std::string>& dates,
     146                    const std::vector<std::string>& author,
     147                    const std::map<std::string, std::string>&) const=0;
    145148
    146149    /**
  • trunk/lib/utility.cc

    r204 r226  
    33/*
    44  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
     5  Copyright (C) 2007 Peter Johansson
    56
    67  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
     
    7879
    7980
     81  std::string htrim(std::string str)
     82  {
     83    size_t length=str.size();
     84    while(length && isspace(str[length-1]))
     85      --length;
     86    return str.substr(0,length);
     87  }
     88
     89
     90  std::string ltrim(std::string str)
     91  {
     92    size_t i = 0;
     93    while(i<str.size() && isspace(str[i]))
     94      ++i;
     95    return str.substr(i);
     96  }
     97
     98
     99  bool node_exist(const std::string& path)
     100  {
     101    struct stat buf;
     102    return !stat(path.c_str(),&buf);
     103  }
     104
     105
    80106  std::string pwd(void)
    81107  {
  • trunk/lib/utility.h

    r217 r226  
    6363  std::string getenv(const std::string& var);
    6464
     65  ///
     66  /// @brief remove trailing whitespaces
     67  ///
     68  std::string htrim(std::string str);
     69
     70  ///
     71  /// @brief remove leading whitespaces
     72  ///
     73  std::string ltrim(std::string str);
     74
    6575  inline bool match_begin(std::string::const_iterator first,
    6676                          std::string::const_iterator last,
     
    8494
    8595  ///
     96  /// @brief Check whether \a path already exists or not.
     97  ///
     98  /// @return True if \a path exists, false otherwise.
     99  ///
     100  bool node_exist(const std::string& path);
     101
     102  ///
    86103  /// @return the current working directory.
    87104  ///
     
    97114  ///
    98115  time_t str2time(const std::string&);
     116
     117  ///
     118  /// remove leading and trailing whitespaces
     119  ///
     120  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
    99121
    100122  ///
Note: See TracChangeset for help on using the changeset viewer.