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

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

more specific error message when root is not a directory - see ticket:311

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1// $Id: Parameter.cc 571 2008-03-14 22:00:26Z 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    bool root_ok = node_exist(root_) && !access_rights(root_, "r");
131   
132    if (root_ok) {
133      // Check that root_ is a directory
134      struct stat nodestat;
135      stat(root_.c_str(), &nodestat);
136      if (!S_ISDIR(nodestat.st_mode))
137        throw runtime_error(string("svndigest: accessing `") + root_ +
138                            "': Not a directory.");
139    }
140
141    // Checking that root_ exists and retrieve the absolute path to root_
142    if (!root_ok || chdir(root_.c_str()))
143      throw runtime_error(string("svndigest: Root directory (") + root_ +
144                          ") access failed.");
145    root_ = pwd();
146
147     
148
149    // need to get back to cwd if relative paths are used.
150    if (chdir(workdir.c_str()))
151      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
152
153    // Checking that targetdir_ exists and retrieve the absolute path
154    // to targetdir_
155    if (chdir(targetdir_.c_str()))
156      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
157                          ") access failed.");
158    targetdir_ = pwd();
159    // Checking write permissions for targetdir_
160    if (access_rights(targetdir_,"w"))
161      throw runtime_error(string("svndigest: No write permission on target ") +
162                                 "directory (" + targetdir_ +").");
163
164    // return back to cwd
165    if (chdir(workdir.c_str()))
166      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
167
168  }
169
170
171  std::string Parameter::config_file(void) const
172  {
173    // not default
174    if (!config_file_.empty())
175      return config_file_;
176   
177    // default behaviour
178    return root()+"/.svndigest/config";
179  }
180
181
182  void Parameter::defaults(void)
183  {
184    config_file_ = "";
185    copyright_=false;
186    force_=false;
187    generate_config_=false;
188    revisions_=false;
189    root_=".";
190    targetdir_=".";
191    verbose_=false;
192  }
193
194
195  void Parameter::help(void)
196  {
197    defaults();
198
199    ColumnStream cs(std::cout, 1);
200    cs.width(0)=79;
201    cs.margin(0)=0;
202    ColumnStream cs2(std::cout, 2);
203    cs2.width(0)=24;
204    cs2.width(1)=52;
205    cs2.margin(0)=2;
206
207    std::cout << "Usage: svndigest [OPTION]...\n"
208              << "\n";
209
210    cs << "Generate statistical report for a subversion repository.\n";
211
212    cs << "\nMandatory arguments to long options are mandatory for "
213       << "short options too.\n";
214
215    cs2  << "    --copyright\tupdate copyright statement\n" 
216         << "    --config-file=ARG\tconfiguration file " 
217         << "[<ROOT>/.svndigest/config]\n"
218         << "-f, --force\tif sub-directory named <ROOT> exists in "
219         << "target directory, remove sub-directory before writing results.\n"
220         << "-g, --generate-config\twrite configuration file " 
221         << "to standard output and exit\n"
222         << "-h, --help\tdisplay this help and exit\n"
223         << "-r, --root=ROOT\tsvn controlled directory to perform "
224         << "statistics calculation on [" << root_ << "]\n"
225         << "    --revisions\tUse revision numbers as time scale "
226         << "instead of dates [dates].\n"
227         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
228         << "-v, --verbose\texplain what is being done\n"
229         << "    --version\tprint version information and exit\n";
230
231    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
232              << std::endl;
233  }
234
235
236  void Parameter::version(void) const
237  {
238    ColumnStream cs(std::cout, 1);
239    cs.width(0)=79;
240    cs << PACKAGE_STRING
241       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
242       << "This is free software; see the source for copying conditions. "
243       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
244       << "A PARTICULAR PURPOSE.\n";
245  }
246
247}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.