source: trunk/bin/Parameter.cc @ 629

Last change on this file since 629 was 629, checked in by Peter Johansson, 15 years ago

typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1// $Id: Parameter.cc 629 2008-04-24 14:07:40Z 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/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 "subversion_info.h"
29#include "utility.h"
30#include <config.h> // this header file is created by configure
31
32#include <cstddef>
33#include <fstream>
34#include <iostream>
35#include <sstream>
36#include <stdexcept>
37#include <string>
38#include <sys/stat.h>
39
40namespace theplu {
41namespace svndigest {
42
43  Parameter::Parameter( int argc, char *argv[])
44    : config_file_(""), copyright_(false), force_(false), 
45      generate_config_(false), ignore_cache_(false), report_(true),
46      revisions_(false), root_("."), targetdir_("."), verbose_(false)
47  {
48    for (int i=1; i<argc; ++i) {
49      std::stringstream ss(argv[i]);
50      std::string myargv("");
51      std::string value("");
52      getline(ss, myargv, '=');
53      getline(ss, value);
54
55      if (myargv=="--config-file"){
56        if (value.size()) {
57          config_file_.value()= value;
58        }
59        else if (++i<argc){
60          config_file_.value()= std::string(argv[i]);
61        }
62        else {
63          missing_argument(myargv);
64        }
65      }
66      else if (myargv=="--copyright"){
67          copyright_.value()=true;
68      }
69      else if (myargv=="-f" || myargv=="--force"){
70          force_.value()=true;
71      }
72      else if (myargv=="-g" || myargv=="--generate-config"){
73          generate_config_.value()=true;
74      }
75      else if (myargv=="-h" || myargv=="--help"){
76        help();
77        exit(0);      // always exit after printing help
78      }
79      else if (myargv=="--ignore-cache"){
80          ignore_cache_.value()=true;
81      }
82      else if (myargv=="-r" || myargv=="--root"){
83        if (value.size()) {
84          root_.value()= value;
85        }
86        else if (++i<argc){
87          root_.value()= std::string(argv[i]);
88        }
89        else {
90          missing_argument(myargv);
91        }
92      }
93      else if (myargv=="--report") {
94          report_.value()=true;
95      }
96      else if (myargv=="--no-report") {
97          report_.value()=false;
98      }
99      else if (myargv=="--revisions") {
100          revisions_.value()=true;
101      }
102      else if (myargv=="-t" || myargv=="--target"){
103        if (value.size()) {
104          targetdir_.value()= value;
105        }
106        else if (++i<argc){
107          targetdir_.value()= std::string(argv[i]);
108        }
109        else {
110          missing_argument(myargv);
111        }
112      }
113      else if (myargv=="-v" || myargv=="--verbose"){
114          verbose_.value()=true;
115      }
116      else if (myargv=="--version"){
117        version();
118        exit(0);
119      }
120      else if (myargv=="-vf" || myargv=="-fv"){
121          verbose_.value()=true;
122          force_.value()=true;
123      }
124      else {
125        throw std::runtime_error("svndigest: invalid option: " + myargv +
126                                 "\nTry `svndigest --help' for usage.");
127      } 
128  }
129
130    analyse();
131  }
132
133
134  void Parameter::analyse(void)
135  {
136    using namespace std;
137
138    string workdir(pwd()); // remember current working directory (cwd).
139
140    bool root_ok = ( node_exist(root_.value()) && 
141                     !access_rights(root_.value(), "r") );
142   
143    if (root_ok) {
144      // Check that root_ is a directory
145      struct stat nodestat;
146      stat(root_.value().c_str(), &nodestat);
147      if (!S_ISDIR(nodestat.st_mode))
148        throw runtime_error(string("svndigest: accessing `") + root_.value() +
149                            "': Not a directory.");
150    }
151
152    // Checking that root_ exists and retrieve the absolute path to root_
153    if (!root_ok || chdir(root_.value().c_str()))
154      throw runtime_error(string("svndigest: Root directory (") + root_.value() +
155                          ") access failed.");
156    root_.value() = pwd();
157
158     
159
160    // need to get back to cwd if relative paths are used.
161    if (chdir(workdir.c_str()))
162      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
163
164    // Checking that targetdir_ exists and retrieve the absolute path
165    // to targetdir_
166    if (chdir(targetdir_.value().c_str()))
167      throw runtime_error(string("svndigest: Target directory (") + 
168                          targetdir_.value() + ") access failed.");
169    targetdir_.value() = pwd();
170    // Checking write permissions for targetdir_
171    if (access_rights(targetdir_.value(),"w"))
172      throw runtime_error(string("svndigest: No write permission on target ") +
173                                 "directory (" + targetdir_.value() +").");
174
175    // return back to cwd
176    if (chdir(workdir.c_str()))
177      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
178
179  }
180
181
182  std::string Parameter::config_file(void) const
183  {
184    // not default
185    if (!config_file_.value().empty())
186      return config_file_.value();
187   
188    // default behaviour
189    return root()+"/.svndigest/config";
190  }
191
192
193  void Parameter::help(void) const
194  {
195    ColumnStream cs(std::cout, 1);
196    cs.width(0)=79;
197    cs.margin(0)=0;
198    ColumnStream cs2(std::cout, 2);
199    // Widest line should fit exactly
200    cs2.width(0)=21;
201    cs2.width(1)=52;
202    cs2.margin(0)=2;
203    // Gnits standard suggest three characters gap between option and description
204    cs2.margin(1)=3;
205
206    std::cout << "Usage: svndigest [OPTION]...\n"
207              << "\n";
208
209    cs << "Generate statistical report for a subversion repository.\n";
210
211    cs << "\nMandatory arguments to long options are mandatory for "
212       << "short options too.\n";
213
214    cs2  << "    --config-file=ARG\tconfiguration file " 
215         << "[<ROOT>/.svndigest/config]\n"
216         << "    --copyright\tupdate copyright statement\n" 
217         << "-f, --force\tif sub-directory named <ROOT> exists in "
218         << "target directory, remove sub-directory before writing results\n"
219         << "-g, --generate-config\twrite configuration file " 
220         << "to standard output and exit\n"
221         << "-h, --help\tdisplay this help and exit\n"
222         << "    --ignore-cache\tIgnore cache files and analyze "
223         << "everything from repository\n"
224         << "    --no-report\tCreate no HTML report\n"
225         << "    --revisions\tUse revision numbers as time scale "
226         << "instead of dates [dates]\n"
227         << "-r, --root=ROOT\tsvn controlled directory to perform "
228         << "statistics calculation on [" << root_.default_value() << "]\n"
229         << "-t, --target=TARGET\toutput directory [" 
230         << targetdir_.default_value() << "]\n"
231         << "-v, --verbose\texplain what is being done\n"
232         << "    --version\tprint version information and exit\n";
233
234    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
235              << std::endl;
236  }
237
238
239  void Parameter::missing_argument(std::string opt) const
240  {
241    if (opt.size()>0 && opt[0]=='-')
242      opt = opt.substr(1);
243    if (opt.size()>0 && opt[0]=='-')
244      opt = opt.substr(1);
245    std::stringstream ss;
246    ss << "svndigest: option requires an argument -- " << opt << "\n"
247       << "Try `svndigest --help' for usage."; 
248    throw std::runtime_error(ss.str());
249  }
250
251  void Parameter::version(bool verbose) const
252  {
253    ColumnStream cs(std::cout, 1);
254    cs.width(0)=79;
255    cs << PACKAGE_STRING << version_string() << "\n";
256    cs << "\nCopyright (C) " << svn_year() 
257       << " Jari H\u00E4kkinen and Peter Johansson.\n"
258       << "This is free software. You may redistribute copies of it under "
259       << "the terms of the GNU General Public License "
260       << "<http://www.gnu.org/licenses/gpl.html>.\n"
261       << "There is NO WARRANTY; to the extent permitted by law.\n";
262  }
263
264}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.