Changeset 229


Ignore:
Timestamp:
Mar 25, 2007, 10:16:07 PM (14 years ago)
Author:
Peter Johansson
Message:

Modified behaviour of Configuration. Fixes #114, #115, #116

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/Parameter.cc

    r226 r229  
    4545      if (myargv=="-c" || myargv=="--config"){
    4646        if (++i<argc){
    47           config_path_= std::string(argv[i]);
     47          config_file_= std::string(argv[i]);
    4848          ok=true;
    4949        }
     
    5555      else if (myargv=="-f" || myargv=="--force"){
    5656          force_=true;
     57          ok=true;
     58      }
     59      else if (myargv=="-g" || myargv=="--generate-config"){
     60          generate_config_=true;
    5761          ok=true;
    5862      }
     
    9599                                 "\nType 'svndigest --help' for usage.");
    96100    }
    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_);
    104101
    105102    analyse();
     
    137134    if (chdir(workdir.c_str()))
    138135      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
     136
     137    if (config_file_.empty())
     138      runtime_error(string("svndigest: Parameter --config requires argument."));
     139
    139140  }
    140141
     
    142143  void Parameter::defaults(void)
    143144  {
    144     config_path_ = getenv("HOME")+"/.svndigest/config";
     145    config_file_ = getenv("HOME")+"/.svndigest/config";
    145146    copyright_=false;
    146147    force_=false;
     148    generate_config_=false;
    147149    revisions_=false;
    148150    root_=".";
     
    166168              << "\n"
    167169              << "Valid options:\n"
    168               << "  -c [--config] arg : path to config file ["
    169               << config_path_ << "]\n"
     170              << "  --copyright    : update copyright statement\n"
     171              << "  -c [--config] arg : configuration file ["
     172              << config_file_ << "]\n"
    170173              << "  -f [--force]   : remove target directory/file if it exists\n"
    171174              << "                   [no force]. NOTE recursive delete.\n"
     175              << "  -g [--generate-config] : write configuration file "
     176              << "to standard output and exit\n"
    172177              << "  -h [--help]    : display this help and exit\n"
    173178              << "  -r [--root] arg : svn controlled directory to perform\n"
     
    184189
    185190
    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 
    241191  void Parameter::version(void) const
    242192  {
  • trunk/bin/Parameter.h

    r226 r229  
    3636  public:
    3737    Parameter(const int argc,const char *argv[]);
     38    inline std::string config_file(void) const { return config_file_; }
    3839    inline bool copyright(void) const { return copyright_; }
    3940    /// @todo
    40     inline const std::map<std::string, std::string>
    41     copyright_alias(void) const { return copyright_alias_; }
    4241    inline bool force(void) const { return force_; }
     42    inline bool generate_config(void) const { return generate_config_; }
    4343    inline bool revisions(void) const { return revisions_; }
    4444    /// @return absolute path to root directory
     
    5252    void defaults(void);
    5353    void help(void);
    54     void print_config(std::string) const;
    55     void read_config(std::string);
    5654    void version(void) const;
    5755
    58     std::string config_path_;
     56    std::string config_file_;
    5957    bool copyright_;
    60     std::map<std::string, std::string> copyright_alias_;
    6158    bool force_;
     59    bool generate_config_;
    6260    bool revisions_;
    6361    std::string root_;
  • trunk/bin/svndigest.cc

    r227 r229  
    2525#include "Parameter.h"
    2626
     27#include "Configuration.h"
    2728#include "Directory.h"
    2829#include "GnuplotFE.h"
     
    4344{
    4445  using namespace theplu::svndigest;
     46
     47  // Reading commandline options
    4548  Parameter* option=NULL;
    4649  try {
     
    5255    std::cerr << e.what() << std::endl;
    5356    exit(-1);
     57  }
     58  assert(option);
     59
     60  // Reading configuration file
     61  Configuration* config = NULL;
     62  if (node_exist(option->config_file())) {
     63    std::ifstream is(option->config_file().c_str());
     64    if (!is.good()){
     65      is.close();
     66      std::cerr << "\nsvndigest: Cannot open config file "
     67                << option->config_file() << std::endl;
     68      exit(-1);
     69    }
     70    config = new Configuration(is);
     71    is.close();
     72  }
     73  else
     74    config = new Configuration;
     75  assert(config);
     76 
     77  // write configuration
     78  if (option->generate_config()) {
     79    std::cout << *config;
     80    exit(0);
    5481  }
    5582
     
    147174    std::cerr << "svndigest: " << x.what() << std::endl;
    148175  }
    149  
     176
    150177  if (option->copyright()){
    151178    try {
    152179      if (option->verbose())
    153180        std::cout << "Updating copyright statements" << std::endl;
    154       tree.print_copyright(commit_dates, authors, option->copyright_alias());
     181      tree.print_copyright(commit_dates, authors, config->copyright_alias());
    155182    }
    156183    catch (const std::runtime_error& x) {
  • trunk/lib/Makefile.am

    r193 r229  
    55# Copyright (C) 2005 Jari Häkkinen
    66# Copyright (C) 2006 Jari Häkkinen, Peter Johansson
     7# Copyright (C) 2007 Peter Johansson
    78#
    89# This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
     
    2526noinst_LTLIBRARIES = libsvndigest.la
    2627
    27 noinst_HEADERS = Directory.h File.h Gnuplot.h GnuplotFE.h             \
     28noinst_HEADERS = Configuration.h Directory.h File.h Gnuplot.h GnuplotFE.h \
    2829  html_utility.h Node.h Parser.h rmdirhier.h Stats.h SVN.h SVNblame.h \
    2930  SVNinfo.h SVNproperty.h utility.h
    3031
    31 libsvndigest_la_SOURCES = Directory.cc File.cc Gnuplot.cc GnuplotFE.cc  \
     32libsvndigest_la_SOURCES = Configuration.cc Directory.cc File.cc \
     33  Gnuplot.cc GnuplotFE.cc \
    3234  html_utility.cc Node.cc Parser.cc rmdirhier.cc Stats.cc SVN.cc        \
    3335  SVNblame.cc SVNinfo.cc SVNproperty.cc utility.cc
Note: See TracChangeset for help on using the changeset viewer.