Changeset 229 for trunk/bin/Parameter.cc


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

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

File:
1 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  {
Note: See TracChangeset for help on using the changeset viewer.