source: trunk/bin/Parameter.cc @ 519

Last change on this file since 519 was 519, checked in by Jari Häkkinen, 14 years ago

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
RevLine 
[37]1// $Id: Parameter.cc 519 2007-12-23 20:14:50Z jari $
2
[84]3/*
[475]4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
[84]5
[519]6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
[84]7
[149]8  svndigest is free software; you can redistribute it and/or modify it
[84]9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
[149]13  svndigest is distributed in the hope that it will be useful, but
[84]14  WITHOUT ANY WARRANTY; without even the implied warranty of
[149]15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
[84]16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
[37]24#include "Parameter.h"
[234]25
26#include "ColumnStream.h"
[116]27#include "utility.h"
[83]28#include <config.h> // this header file is created by configure
[37]29
[226]30#include <fstream>
[37]31#include <iostream>
[226]32#include <sstream>
[49]33#include <stdexcept>
[37]34#include <string>
[104]35#include <sys/stat.h>
[37]36
37namespace theplu {
[149]38namespace svndigest {
[37]39
[518]40  Parameter::Parameter( int argc, char *argv[])
[37]41  {
[73]42    defaults();
[173]43    for (int i=1; i<argc; ++i) {
[37]44      bool ok=false;
[475]45      std::stringstream ss(argv[i]);
46      std::string myargv("");
47      std::string value("");
48      getline(ss, myargv, '=');
49      getline(ss, value);
50
[234]51      if (myargv=="--config-file"){
[475]52        if (value.size()) {
53          config_file_= value;
54          ok=true;
55        }
56        else if (++i<argc){
[229]57          config_file_= std::string(argv[i]);
[37]58          ok=true;
[226]59        }
[37]60      }
[226]61      else if (myargv=="--copyright"){
[198]62          copyright_=true;
63          ok=true;
64      }
[226]65      else if (myargv=="-f" || myargv=="--force"){
66          force_=true;
67          ok=true;
68      }
[229]69      else if (myargv=="-g" || myargv=="--generate-config"){
70          generate_config_=true;
71          ok=true;
72      }
[39]73      else if (myargv=="-h" || myargv=="--help"){
[37]74        help();
75        exit(0);      // always exit after printing help
76      }
[52]77      else if (myargv=="-r" || myargv=="--root"){
[475]78        if (value.size()) {
79          root_= value;
80          ok=true;
81        }
82        else if (++i<argc){
[52]83          root_= std::string(argv[i]);
84          ok=true;
85        }
86      }
[423]87      else if (myargv=="--report") {
88          report_=true;
89          ok=true;
90      }
91      else if (myargv=="--no-report") {
92          report_=false;
93          ok=true;
94      }
[240]95      else if (myargv=="--revisions") {
[73]96          revisions_=true;
97          ok=true;
98      }
[52]99      else if (myargv=="-t" || myargv=="--target"){
[475]100        if (value.size()) {
101          targetdir_= value;
102          ok=true;
103        }
104        else if (++i<argc){
[52]105          targetdir_= std::string(argv[i]);
106          ok=true;
107        }
108      }
[39]109      else if (myargv=="-v" || myargv=="--verbose"){
[37]110          verbose_=true;
111          ok=true;
112      }
[39]113      else if (myargv=="--version"){
[37]114          version();
115          exit(0);
116      }
[180]117      else if (myargv=="-vf" || myargv=="-fv"){
118          verbose_=true;
119          force_=true;
120          ok=true;
121      }
[49]122
123      if (!ok)
[149]124        throw std::runtime_error("svndigest: invalid option: " + myargv +
[431]125                                 "\nTry `svndigest --help' for usage.");
[37]126    }
127
128    analyse();
129  }
130
131
132  void Parameter::analyse(void)
133  {
[151]134    using namespace std;
135
136    string workdir(pwd()); // remember current working directory (cwd).
137
138    // Checking that root_ exists and retrieve the absolute path to root_
139    if (chdir(root_.c_str()))
140      throw runtime_error(string("svndigest: Root directory (") + root_ +
141                          ") access failed.");
[116]142    root_ = pwd();
143
[151]144    // need to get back to cwd if relative paths are used.
145    if (chdir(workdir.c_str()))
146      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
147
148    // Checking that targetdir_ exists and retrieve the absolute path
149    // to targetdir_
150    if (chdir(targetdir_.c_str()))
151      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
152                          ") access failed.");
[116]153    targetdir_ = pwd();
[151]154    // Checking write permissions for targetdir_
155    if (access_rights(targetdir_,"w"))
156      throw runtime_error(string("svndigest: No write permission on target ") +
157                                 "directory (" + targetdir_ +").");
[116]158
[151]159    // return back to cwd
160    if (chdir(workdir.c_str()))
[240]161      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
[229]162
[240]163  }
[229]164
[240]165
166  std::string Parameter::config_file(void) const
167  {
168    // not default
169    if (!config_file_.empty())
170      return config_file_;
171   
172    // default behaviour
173    return root()+"/.svndigest/config";
[37]174  }
175
176
[73]177  void Parameter::defaults(void)
178  {
[240]179    config_file_ = "";
[198]180    copyright_=false;
[73]181    force_=false;
[229]182    generate_config_=false;
[427]183    report_=true;
[73]184    revisions_=false;
185    root_=".";
[104]186    targetdir_=".";
[73]187    verbose_=false;
188  }
189
190
[37]191  void Parameter::help(void)
192  {
[116]193    defaults();
[234]194
195    ColumnStream cs(std::cout, 1);
[280]196    cs.width(0)=79;
197    cs.margin(0)=0;
[241]198    ColumnStream cs2(std::cout, 2);
[234]199    cs2.width(0)=24;
[269]200    cs2.width(1)=52;
[234]201    cs2.margin(0)=2;
202
[280]203    std::cout << "Usage: svndigest [OPTION]...\n"
[234]204              << "\n";
205
[280]206    cs << "Generate statistical report for a subversion repository.\n";
[234]207
[280]208    cs << "\nMandatory arguments to long options are mandatory for "
209       << "short options too.\n";
210
[241]211    cs2  << "    --copyright\tupdate copyright statement\n" 
212         << "    --config-file=ARG\tconfiguration file " 
[240]213         << "[<ROOT>/.svndigest/config]\n"
[374]214         << "-f, --force\tif sub-directory named <ROOT> exists in "
[423]215         << "target directory, remove sub-directory before writing results\n"
[241]216         << "-g, --generate-config\twrite configuration file " 
[234]217         << "to standard output and exit\n"
[241]218         << "-h, --help\tdisplay this help and exit\n"
[423]219         << "    --no-report\tCreate no HTML report\n"
220         << "    --revisions\tUse revision numbers as time scale "
221         << "instead of dates [dates]\n"
[241]222         << "-r, --root=ROOT\tsvn controlled directory to perform "
[234]223         << "statistics calculation on [" << root_ << "]\n"
[241]224         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
225         << "-v, --verbose\texplain what is being done\n"
226         << "    --version\tprint version information and exit\n";
[280]227
228    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
229              << std::endl;
[37]230  }
231
232
[98]233  void Parameter::version(void) const
[37]234  {
[280]235    ColumnStream cs(std::cout, 1);
236    cs.width(0)=79;
237    cs << PACKAGE_STRING
[475]238       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
[283]239       << "This is free software; see the source for copying conditions. "
240       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
[280]241       << "A PARTICULAR PURPOSE.\n";
[37]242  }
243
[442]244}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.