source: branches/0.6-stable/bin/Parameter.cc @ 469

Last change on this file since 469 was 469, checked in by Peter Johansson, 14 years ago

fixes #259

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