source: trunk/bin/Parameter.cc @ 159

Last change on this file since 159 was 151, checked in by Jari Häkkinen, 15 years ago

Fixes #50 and #58. Added checks that dirs exists and have proper permissions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1// $Id: Parameter.cc 151 2006-08-13 20:30:02Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://lev.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#include "utility.h"
26#include <config.h> // this header file is created by configure
27
28#include <iostream>
29#include <stdexcept>
30#include <string>
31#include <sys/stat.h>
32
33namespace theplu {
34namespace svndigest {
35
36  Parameter::Parameter(const int argc,const char *argv[])
37  {
38    defaults();
39    for (int i=1; i<argc; i++) {
40      bool ok=false;
41      std::string myargv(argv[i]);
42      if (myargv=="-f" || myargv=="--force"){
43          force_=true;
44          ok=true;
45      }
46      else if (myargv=="-h" || myargv=="--help"){
47        help();
48        exit(0);      // always exit after printing help
49      }
50      else if (myargv=="-r" || myargv=="--root"){
51        if (++i<argc){
52          root_= std::string(argv[i]);
53          ok=true;
54        }
55      }
56      else if (myargv=="-rev" || myargv=="--revisions") {
57          revisions_=true;
58          ok=true;
59      }
60      else if (myargv=="-t" || myargv=="--target"){
61        if (++i<argc){
62          targetdir_= std::string(argv[i]);
63          ok=true;
64        }
65      }
66      else if (myargv=="-v" || myargv=="--verbose"){
67          verbose_=true;
68          ok=true;
69      }
70      else if (myargv=="--version"){
71          version();
72          exit(0);
73      }
74
75      if (!ok)
76        throw std::runtime_error("svndigest: invalid option: " + myargv +
77                                 "\nType 'svndigest --help' for usage.");
78    }
79
80    analyse();
81  }
82
83
84  void Parameter::analyse(void)
85  {
86    using namespace std;
87
88    string workdir(pwd()); // remember current working directory (cwd).
89
90    // Checking that root_ exists and retrieve the absolute path to root_
91    if (chdir(root_.c_str()))
92      throw runtime_error(string("svndigest: Root directory (") + root_ +
93                          ") access failed.");
94    root_ = pwd();
95
96    // need to get back to cwd if relative paths are used.
97    if (chdir(workdir.c_str()))
98      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
99
100    // Checking that targetdir_ exists and retrieve the absolute path
101    // to targetdir_
102    if (chdir(targetdir_.c_str()))
103      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
104                          ") access failed.");
105    targetdir_ = pwd();
106    // Checking write permissions for targetdir_
107    if (access_rights(targetdir_,"w"))
108      throw runtime_error(string("svndigest: No write permission on target ") +
109                                 "directory (" + targetdir_ +").");
110
111    // return back to cwd
112    if (chdir(workdir.c_str()))
113      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
114  }
115
116
117  void Parameter::defaults(void)
118  {
119    force_=false;
120    revisions_=false;
121    root_=".";
122    targetdir_=".";
123    verbose_=false;
124  }
125
126
127  void Parameter::help(void)
128  {
129    defaults();
130    std::cout << "\n"
131              << "usage: svndigest [options]\n"
132              << "\n"
133              << "svndigest traverses a directory structure (controlled by\n"
134              << "subversion) and calculates developer statistics entries.\n"
135              << "The top level directory of the directory structure to\n"
136              << "traverse is set with the -r option. The result is written to\n"
137              << "the current working directory or a directory set with the\n"
138              << "-t option.\n"
139              << "\n"
140              << "Valid options:\n"
141              << "  -f [--force]   : remove target directory/file if it exists\n"
142              << "                   [no force]. NOTE recursive delete.\n"
143              << "  -h [--help]    : display this help and exit\n"
144              << "  -r [--root] arg : svn controlled directory to perform\n"
145              << "                    statistics calculation on [" 
146              << root_ << "]\n"
147              << "  -rev [--revisions]: Use revision numbers as time scale\n"
148              << "                      instead of dates [dates].\n"
149              << "  -t [--target] arg : output directory [" 
150              << targetdir_ << "]\n"
151              << "  -v [--verbose] : explain what is being done\n"
152              << "  --version      : print version information and exit\n"
153              << std::endl;
154  }
155
156
157  void Parameter::version(void) const
158  {
159    using namespace std;
160    cout << PACKAGE_STRING
161         << "\nCopyright (C) 2006 Jari Häkkinen and Peter Johansson.\n\n"
162         << "This is free software; see the source for copying conditions.\n"
163         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
164         << "A PARTICULAR PURPOSE." << endl;
165  }
166
167}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.