Changeset 226 for trunk/bin


Ignore:
Timestamp:
Mar 11, 2007, 8:32:41 PM (17 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/bin
Files:
3 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) {
Note: See TracChangeset for help on using the changeset viewer.