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
Line 
1// $Id: Parameter.cc 519 2007-12-23 20:14:50Z jari $
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=="-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=="--report") {
88          report_=true;
89          ok=true;
90      }
91      else if (myargv=="--no-report") {
92          report_=false;
93          ok=true;
94      }
95      else if (myargv=="--revisions") {
96          revisions_=true;
97          ok=true;
98      }
99      else if (myargv=="-t" || myargv=="--target"){
100        if (value.size()) {
101          targetdir_= value;
102          ok=true;
103        }
104        else if (++i<argc){
105          targetdir_= std::string(argv[i]);
106          ok=true;
107        }
108      }
109      else if (myargv=="-v" || myargv=="--verbose"){
110          verbose_=true;
111          ok=true;
112      }
113      else if (myargv=="--version"){
114          version();
115          exit(0);
116      }
117      else if (myargv=="-vf" || myargv=="-fv"){
118          verbose_=true;
119          force_=true;
120          ok=true;
121      }
122
123      if (!ok)
124        throw std::runtime_error("svndigest: invalid option: " + myargv +
125                                 "\nTry `svndigest --help' for usage.");
126    }
127
128    analyse();
129  }
130
131
132  void Parameter::analyse(void)
133  {
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.");
142    root_ = pwd();
143
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.");
153    targetdir_ = pwd();
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_ +").");
158
159    // return back to cwd
160    if (chdir(workdir.c_str()))
161      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
162
163  }
164
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";
174  }
175
176
177  void Parameter::defaults(void)
178  {
179    config_file_ = "";
180    copyright_=false;
181    force_=false;
182    generate_config_=false;
183    report_=true;
184    revisions_=false;
185    root_=".";
186    targetdir_=".";
187    verbose_=false;
188  }
189
190
191  void Parameter::help(void)
192  {
193    defaults();
194
195    ColumnStream cs(std::cout, 1);
196    cs.width(0)=79;
197    cs.margin(0)=0;
198    ColumnStream cs2(std::cout, 2);
199    cs2.width(0)=24;
200    cs2.width(1)=52;
201    cs2.margin(0)=2;
202
203    std::cout << "Usage: svndigest [OPTION]...\n"
204              << "\n";
205
206    cs << "Generate statistical report for a subversion repository.\n";
207
208    cs << "\nMandatory arguments to long options are mandatory for "
209       << "short options too.\n";
210
211    cs2  << "    --copyright\tupdate copyright statement\n" 
212         << "    --config-file=ARG\tconfiguration file " 
213         << "[<ROOT>/.svndigest/config]\n"
214         << "-f, --force\tif sub-directory named <ROOT> exists in "
215         << "target directory, remove sub-directory before writing results\n"
216         << "-g, --generate-config\twrite configuration file " 
217         << "to standard output and exit\n"
218         << "-h, --help\tdisplay this help and exit\n"
219         << "    --no-report\tCreate no HTML report\n"
220         << "    --revisions\tUse revision numbers as time scale "
221         << "instead of dates [dates]\n"
222         << "-r, --root=ROOT\tsvn controlled directory to perform "
223         << "statistics calculation on [" << root_ << "]\n"
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";
227
228    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
229              << std::endl;
230  }
231
232
233  void Parameter::version(void) const
234  {
235    ColumnStream cs(std::cout, 1);
236    cs.width(0)=79;
237    cs << PACKAGE_STRING
238       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
239       << "This is free software; see the source for copying conditions. "
240       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
241       << "A PARTICULAR PURPOSE.\n";
242  }
243
244}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.