source: trunk/bin/Parameter.cc @ 229

Last change on this file since 229 was 229, checked in by Peter Johansson, 15 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1// $Id: Parameter.cc 229 2007-03-25 20:16:07Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Parameter.h"
26#include "utility.h"
27#include <config.h> // this header file is created by configure
28
29#include <fstream>
30#include <iostream>
31#include <sstream>
32#include <stdexcept>
33#include <string>
34#include <sys/stat.h>
35
36namespace theplu {
37namespace svndigest {
38
39  Parameter::Parameter(const int argc,const char *argv[])
40  {
41    defaults();
42    for (int i=1; i<argc; ++i) {
43      bool ok=false;
44      std::string myargv(argv[i]);
45      if (myargv=="-c" || myargv=="--config"){
46        if (++i<argc){
47          config_file_= 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"){
56          force_=true;
57          ok=true;
58      }
59      else if (myargv=="-g" || myargv=="--generate-config"){
60          generate_config_=true;
61          ok=true;
62      }
63      else if (myargv=="-h" || myargv=="--help"){
64        help();
65        exit(0);      // always exit after printing help
66      }
67      else if (myargv=="-r" || myargv=="--root"){
68        if (++i<argc){
69          root_= std::string(argv[i]);
70          ok=true;
71        }
72      }
73      else if (myargv=="-rev" || myargv=="--revisions") {
74          revisions_=true;
75          ok=true;
76      }
77      else if (myargv=="-t" || myargv=="--target"){
78        if (++i<argc){
79          targetdir_= std::string(argv[i]);
80          ok=true;
81        }
82      }
83      else if (myargv=="-v" || myargv=="--verbose"){
84          verbose_=true;
85          ok=true;
86      }
87      else if (myargv=="--version"){
88          version();
89          exit(0);
90      }
91      else if (myargv=="-vf" || myargv=="-fv"){
92          verbose_=true;
93          force_=true;
94          ok=true;
95      }
96
97      if (!ok)
98        throw std::runtime_error("svndigest: invalid option: " + myargv +
99                                 "\nType 'svndigest --help' for usage.");
100    }
101
102    analyse();
103  }
104
105
106  void Parameter::analyse(void)
107  {
108    using namespace std;
109
110    string workdir(pwd()); // remember current working directory (cwd).
111
112    // Checking that root_ exists and retrieve the absolute path to root_
113    if (chdir(root_.c_str()))
114      throw runtime_error(string("svndigest: Root directory (") + root_ +
115                          ") access failed.");
116    root_ = pwd();
117
118    // need to get back to cwd if relative paths are used.
119    if (chdir(workdir.c_str()))
120      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
121
122    // Checking that targetdir_ exists and retrieve the absolute path
123    // to targetdir_
124    if (chdir(targetdir_.c_str()))
125      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
126                          ") access failed.");
127    targetdir_ = pwd();
128    // Checking write permissions for targetdir_
129    if (access_rights(targetdir_,"w"))
130      throw runtime_error(string("svndigest: No write permission on target ") +
131                                 "directory (" + targetdir_ +").");
132
133    // return back to cwd
134    if (chdir(workdir.c_str()))
135      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
140  }
141
142
143  void Parameter::defaults(void)
144  {
145    config_file_ = getenv("HOME")+"/.svndigest/config";
146    copyright_=false;
147    force_=false;
148    generate_config_=false;
149    revisions_=false;
150    root_=".";
151    targetdir_=".";
152    verbose_=false;
153  }
154
155
156  void Parameter::help(void)
157  {
158    defaults();
159    std::cout << "\n"
160              << "usage: svndigest [options]\n"
161              << "\n"
162              << "svndigest traverses a directory structure (controlled by\n"
163              << "subversion) and calculates developer statistics entries.\n"
164              << "The top level directory of the directory structure to\n"
165              << "traverse is set with the -r option. The result is written to\n"
166              << "the current working directory or a directory set with the\n"
167              << "-t option.\n"
168              << "\n"
169              << "Valid options:\n"
170              << "  --copyright    : update copyright statement\n" 
171              << "  -c [--config] arg : configuration file ["
172              << config_file_ << "]\n"
173              << "  -f [--force]   : remove target directory/file if it exists\n"
174              << "                   [no force]. NOTE recursive delete.\n"
175              << "  -g [--generate-config] : write configuration file " 
176              << "to standard output and exit\n"
177              << "  -h [--help]    : display this help and exit\n"
178              << "  -r [--root] arg : svn controlled directory to perform\n"
179              << "                    statistics calculation on [" 
180              << root_ << "]\n"
181              << "  -rev [--revisions]: Use revision numbers as time scale\n"
182              << "                      instead of dates [dates].\n"
183              << "  -t [--target] arg : output directory [" 
184              << targetdir_ << "]\n"
185              << "  -v [--verbose] : explain what is being done\n"
186              << "  --version      : print version information and exit\n"
187              << std::endl;
188  }
189
190
191  void Parameter::version(void) const
192  {
193    using namespace std;
194    cout << PACKAGE_STRING
195         << "\nCopyright (C) 2006-2007 Jari Häkkinen and Peter Johansson.\n\n"
196         << "This is free software; see the source for copying conditions.\n"
197         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
198         << "A PARTICULAR PURPOSE." << endl;
199  }
200
201}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.