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

Last change on this file since 575 was 575, checked in by Peter Johansson, 13 years ago

updating copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1// $Id: Parameter.cc 575 2008-03-18 13:27:27Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of svndigest, http://trac.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
27#include "ColumnStream.h"
28#include "utility.h"
29#include <config.h> // this header file is created by configure
30
31#include <fstream>
32#include <iostream>
33#include <sstream>
34#include <stdexcept>
35#include <string>
36#include <sys/stat.h>
37
38namespace theplu {
39namespace svndigest {
40
41  Parameter::Parameter(const int argc,const char *argv[])
42  {
43    defaults();
44    for (int i=1; i<argc; ++i) {
45      bool ok=false;
46      std::stringstream ss(argv[i]);
47      std::string myargv("");
48      std::string value("");
49      getline(ss, myargv, '=');
50      getline(ss, value);
51
52      if (myargv=="--config-file"){
53        if (value.size()) {
54          config_file_= value;
55          ok=true;
56        }
57        else if (++i<argc){
58          config_file_= std::string(argv[i]);
59          ok=true;
60        }
61      }
62      else if (myargv=="--copyright"){
63          copyright_=true;
64          ok=true;
65      }
66      else if (myargv=="-f" || myargv=="--force"){
67          force_=true;
68          ok=true;
69      }
70      else if (myargv=="-g" || myargv=="--generate-config"){
71          generate_config_=true;
72          ok=true;
73      }
74      else if (myargv=="-h" || myargv=="--help"){
75        help();
76        exit(0);      // always exit after printing help
77      }
78      else if (myargv=="-r" || myargv=="--root"){
79        if (value.size()) {
80          root_= value;
81          ok=true;
82        }
83        else if (++i<argc){
84          root_= std::string(argv[i]);
85          ok=true;
86        }
87      }
88      else if (myargv=="--revisions") {
89          revisions_=true;
90          ok=true;
91      }
92      else if (myargv=="-t" || myargv=="--target"){
93        if (value.size()) {
94          targetdir_= value;
95          ok=true;
96        }
97        else if (++i<argc){
98          targetdir_= std::string(argv[i]);
99          ok=true;
100        }
101      }
102      else if (myargv=="-v" || myargv=="--verbose"){
103          verbose_=true;
104          ok=true;
105      }
106      else if (myargv=="--version"){
107          version();
108          exit(0);
109      }
110      else if (myargv=="-vf" || myargv=="-fv"){
111          verbose_=true;
112          force_=true;
113          ok=true;
114      }
115
116      if (!ok)
117        throw std::runtime_error("svndigest: invalid option: " + myargv +
118                                 "\nType `svndigest --help' for usage.");
119    }
120
121    analyse();
122  }
123
124
125  void Parameter::analyse(void)
126  {
127    using namespace std;
128
129    string workdir(pwd()); // remember current working directory (cwd).
130
131    bool root_ok = node_exist(root_) && !access_rights(root_, "r");
132   
133    if (root_ok) {
134      // Check that root_ is a directory
135      struct stat nodestat;
136      stat(root_.c_str(), &nodestat);
137      if (!S_ISDIR(nodestat.st_mode))
138        throw runtime_error(string("svndigest: accessing `") + root_ +
139                            "': Not a directory.");
140    }
141
142    // Checking that root_ exists and retrieve the absolute path to root_
143    if (!root_ok || chdir(root_.c_str()))
144      throw runtime_error(string("svndigest: Root directory (") + root_ +
145                          ") access failed.");
146    root_ = pwd();
147
148     
149
150    // need to get back to cwd if relative paths are used.
151    if (chdir(workdir.c_str()))
152      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
153
154    // Checking that targetdir_ exists and retrieve the absolute path
155    // to targetdir_
156    if (chdir(targetdir_.c_str()))
157      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
158                          ") access failed.");
159    targetdir_ = pwd();
160    // Checking write permissions for targetdir_
161    if (access_rights(targetdir_,"w"))
162      throw runtime_error(string("svndigest: No write permission on target ") +
163                                 "directory (" + targetdir_ +").");
164
165    // return back to cwd
166    if (chdir(workdir.c_str()))
167      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
168
169  }
170
171
172  std::string Parameter::config_file(void) const
173  {
174    // not default
175    if (!config_file_.empty())
176      return config_file_;
177   
178    // default behaviour
179    return root()+"/.svndigest/config";
180  }
181
182
183  void Parameter::defaults(void)
184  {
185    config_file_ = "";
186    copyright_=false;
187    force_=false;
188    generate_config_=false;
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         << "-r, --root=ROOT\tsvn controlled directory to perform "
225         << "statistics calculation on [" << root_ << "]\n"
226         << "    --revisions\tUse revision numbers as time scale "
227         << "instead of dates [dates].\n"
228         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
229         << "-v, --verbose\texplain what is being done\n"
230         << "    --version\tprint version information and exit\n";
231
232    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
233              << std::endl;
234  }
235
236
237  void Parameter::version(void) const
238  {
239    ColumnStream cs(std::cout, 1);
240    cs.width(0)=79;
241    cs << PACKAGE_STRING
242       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
243       << "This is free software; see the source for copying conditions. "
244       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
245       << "A PARTICULAR PURPOSE.\n";
246  }
247
248}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.