source: trunk/bin/Parameter.cc @ 538

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

added ignore-cache option

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1// $Id: Parameter.cc 538 2007-12-27 07:25:08Z 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/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( int argc, 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=="--ignore-cache"){
78          ignore_cache_=true;
79          ok=true;
80      }
81      else if (myargv=="-r" || myargv=="--root"){
82        if (value.size()) {
83          root_= value;
84          ok=true;
85        }
86        else if (++i<argc){
87          root_= std::string(argv[i]);
88          ok=true;
89        }
90      }
91      else if (myargv=="--report") {
92          report_=true;
93          ok=true;
94      }
95      else if (myargv=="--no-report") {
96          report_=false;
97          ok=true;
98      }
99      else if (myargv=="--revisions") {
100          revisions_=true;
101          ok=true;
102      }
103      else if (myargv=="-t" || myargv=="--target"){
104        if (value.size()) {
105          targetdir_= value;
106          ok=true;
107        }
108        else if (++i<argc){
109          targetdir_= std::string(argv[i]);
110          ok=true;
111        }
112      }
113      else if (myargv=="-v" || myargv=="--verbose"){
114          verbose_=true;
115          ok=true;
116      }
117      else if (myargv=="--version"){
118          version();
119          exit(0);
120      }
121      else if (myargv=="-vf" || myargv=="-fv"){
122          verbose_=true;
123          force_=true;
124          ok=true;
125      }
126
127      if (!ok)
128        throw std::runtime_error("svndigest: invalid option: " + myargv +
129                                 "\nTry `svndigest --help' for usage.");
130    }
131
132    analyse();
133  }
134
135
136  void Parameter::analyse(void)
137  {
138    using namespace std;
139
140    string workdir(pwd()); // remember current working directory (cwd).
141
142    // Checking that root_ exists and retrieve the absolute path to root_
143    if (chdir(root_.c_str()))
144      throw runtime_error(string("svndigest: Root directory (") + root_ +
145                          ") access failed.");
146    root_ = pwd();
147
148    // need to get back to cwd if relative paths are used.
149    if (chdir(workdir.c_str()))
150      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
151
152    // Checking that targetdir_ exists and retrieve the absolute path
153    // to targetdir_
154    if (chdir(targetdir_.c_str()))
155      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
156                          ") access failed.");
157    targetdir_ = pwd();
158    // Checking write permissions for targetdir_
159    if (access_rights(targetdir_,"w"))
160      throw runtime_error(string("svndigest: No write permission on target ") +
161                                 "directory (" + targetdir_ +").");
162
163    // return back to cwd
164    if (chdir(workdir.c_str()))
165      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
166
167  }
168
169
170  std::string Parameter::config_file(void) const
171  {
172    // not default
173    if (!config_file_.empty())
174      return config_file_;
175   
176    // default behaviour
177    return root()+"/.svndigest/config";
178  }
179
180
181  void Parameter::defaults(void)
182  {
183    config_file_ = "";
184    copyright_=false;
185    force_=false;
186    generate_config_=false;
187    ignore_cache_=false;
188    report_=true;
189    revisions_=false;
190    root_=".";
191    targetdir_=".";
192    verbose_=false;
193  }
194
195
196  void Parameter::help(void)
197  {
198    defaults();
199
200    ColumnStream cs(std::cout, 1);
201    cs.width(0)=79;
202    cs.margin(0)=0;
203    ColumnStream cs2(std::cout, 2);
204    cs2.width(0)=24;
205    cs2.width(1)=52;
206    cs2.margin(0)=2;
207
208    std::cout << "Usage: svndigest [OPTION]...\n"
209              << "\n";
210
211    cs << "Generate statistical report for a subversion repository.\n";
212
213    cs << "\nMandatory arguments to long options are mandatory for "
214       << "short options too.\n";
215
216    cs2  << "    --copyright\tupdate copyright statement\n" 
217         << "    --config-file=ARG\tconfiguration file " 
218         << "[<ROOT>/.svndigest/config]\n"
219         << "-f, --force\tif sub-directory named <ROOT> exists in "
220         << "target directory, remove sub-directory before writing results\n"
221         << "-g, --generate-config\twrite configuration file " 
222         << "to standard output and exit\n"
223         << "-h, --help\tdisplay this help and exit\n"
224         << "    --ignore-cache\tIgnore cache files and analyze "
225         << "everything from repository\n"
226         << "    --no-report\tCreate no HTML report\n"
227         << "    --revisions\tUse revision numbers as time scale "
228         << "instead of dates [dates]\n"
229         << "-r, --root=ROOT\tsvn controlled directory to perform "
230         << "statistics calculation on [" << root_ << "]\n"
231         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
232         << "-v, --verbose\texplain what is being done\n"
233         << "    --version\tprint version information and exit\n";
234
235    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
236              << std::endl;
237  }
238
239
240  void Parameter::version(void) const
241  {
242    ColumnStream cs(std::cout, 1);
243    cs.width(0)=79;
244    cs << PACKAGE_STRING
245       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
246       << "This is free software; see the source for copying conditions. "
247       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
248       << "A PARTICULAR PURPOSE.\n";
249  }
250
251}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.