source: branches/peter-dev/bin/Parameter.cc @ 456

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

fixes #87 and #95 and #103 and #104 and #80 and #108, and refs #69

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1// $Id: Parameter.cc 234 2007-04-09 12:08:26Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://lev.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::string myargv(argv[i]);
47      if (myargv=="--config-file"){
48        if (++i<argc){
49          config_file_= std::string(argv[i]);
50          ok=true;
51        }
52      }
53      else if (myargv=="--copyright"){
54          copyright_=true;
55          ok=true;
56      }
57      else if (myargv=="-f" || myargv=="--force"){
58          force_=true;
59          ok=true;
60      }
61      else if (myargv=="-g" || myargv=="--generate-config"){
62          generate_config_=true;
63          ok=true;
64      }
65      else if (myargv=="-h" || myargv=="--help"){
66        help();
67        exit(0);      // always exit after printing help
68      }
69      else if (myargv=="-r" || myargv=="--root"){
70        if (++i<argc){
71          root_= std::string(argv[i]);
72          ok=true;
73        }
74      }
75      else if (myargv=="-rev" || myargv=="--revisions") {
76          revisions_=true;
77          ok=true;
78      }
79      else if (myargv=="-t" || myargv=="--target"){
80        if (++i<argc){
81          targetdir_= std::string(argv[i]);
82          ok=true;
83        }
84      }
85      else if (myargv=="-v" || myargv=="--verbose"){
86          verbose_=true;
87          ok=true;
88      }
89      else if (myargv=="--version"){
90          version();
91          exit(0);
92      }
93      else if (myargv=="-vf" || myargv=="-fv"){
94          verbose_=true;
95          force_=true;
96          ok=true;
97      }
98
99      if (!ok)
100        throw std::runtime_error("svndigest: invalid option: " + myargv +
101                                 "\nType 'svndigest --help' for usage.");
102    }
103
104    analyse();
105  }
106
107
108  void Parameter::analyse(void)
109  {
110    using namespace std;
111
112    string workdir(pwd()); // remember current working directory (cwd).
113
114    // Checking that root_ exists and retrieve the absolute path to root_
115    if (chdir(root_.c_str()))
116      throw runtime_error(string("svndigest: Root directory (") + root_ +
117                          ") access failed.");
118    root_ = pwd();
119
120    // need to get back to cwd if relative paths are used.
121    if (chdir(workdir.c_str()))
122      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
123
124    // Checking that targetdir_ exists and retrieve the absolute path
125    // to targetdir_
126    if (chdir(targetdir_.c_str()))
127      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
128                          ") access failed.");
129    targetdir_ = pwd();
130    // Checking write permissions for targetdir_
131    if (access_rights(targetdir_,"w"))
132      throw runtime_error(string("svndigest: No write permission on target ") +
133                                 "directory (" + targetdir_ +").");
134
135    // return back to cwd
136    if (chdir(workdir.c_str()))
137      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
138
139    if (config_file_.empty())
140      runtime_error(string("svndigest: Parameter --config-file requires argument."));
141
142  }
143
144
145  void Parameter::defaults(void)
146  {
147    config_file_ = getenv("HOME")+"/.svndigest/config";
148    copyright_=false;
149    force_=false;
150    generate_config_=false;
151    revisions_=false;
152    root_=".";
153    targetdir_=".";
154    verbose_=false;
155  }
156
157
158  void Parameter::help(void)
159  {
160    defaults();
161
162    ColumnStream cs(std::cout, 1);
163    cs.width(0)=68;
164    cs.margin(0)=2;
165    ColumnStream cs2(std::cout, 3);
166    cs2.width(0)=24;
167    cs2.width(1)=2;
168    cs2.width(2)=50;
169    cs2.margin(0)=2;
170
171    std::cout << "usage: svndigest [options]\n"
172              << "\n";
173
174    cs << "svndigest traverses a directory structure (controlled by "
175       << "subversion) and calculates developer statistics entries. "
176       << "The top level directory of the directory structure to "
177       << "traverse is set with the -r option. The result is written to "
178       << "the current working directory or a directory set with the "
179       << "-t option.\n";
180    std::cout << "\nValid options:\n";
181
182    cs2  << "--copyright\t:\tupdate copyright statement\n" 
183         << "--config-file arg\t:\tconfiguration file [" 
184         << config_file_ << "]\n"
185         << "-f [--force]\t:\tremove target directory/file if it exists "
186         << "[no force]. NOTE recursive delete.\n"
187         << "-g [--generate-config]\t:\twrite configuration file " 
188         << "to standard output and exit\n"
189         << "-h [--help]\t:\tdisplay this help and exit\n"
190         << "-r [--root] arg\t:\tsvn controlled directory to perform "
191         << "statistics calculation on [" << root_ << "]\n"
192         << "-rev [--revisions]\t:\tUse revision numbers as time scale "
193         << "instead of dates [dates].\n"
194         << "-t [--target] arg\t:\toutput directory [" << targetdir_ << "]\n"
195         << "-v [--verbose]\t:\texplain what is being done\n"
196         << "--version\t:\tprint version information and exit\n";
197    std::cout << std::endl;
198  }
199
200
201  void Parameter::version(void) const
202  {
203    using namespace std;
204    cout << PACKAGE_STRING
205         << "\nCopyright (C) 2006-2007 Jari Häkkinen and Peter Johansson.\n\n"
206         << "This is free software; see the source for copying conditions.\n"
207         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
208         << "A PARTICULAR PURPOSE." << endl;
209  }
210
211}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.