source: trunk/bin/Parameter.cc @ 1064

Last change on this file since 1064 was 1064, checked in by Peter Johansson, 11 years ago

sort options alphabetically. refs #307

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1// $Id: Parameter.cc 1064 2010-06-05 23:24:05Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.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 3 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 svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include <config.h> // this header file is created by configure
24
25#include "Parameter.h"
26
27#include "OptionVersion.h"
28#include "../lib/utility.h" // to avoid inclusion of yat file
29
30#include "yat/ColumnStream.h"
31#include "yat/Exception.h"
32#include "yat/OptionArg.h"
33#include "yat/OptionHelp.h"
34#include "yat/OptionSwitch.h"
35
36#include <cassert>
37#include <cerrno>
38#include <cstddef>
39#include <cstring>
40#include <fstream>
41#include <iostream>
42#include <sstream>
43#include <stdexcept>
44#include <string>
45#include <sys/stat.h>
46
47namespace theplu {
48namespace svndigest {
49
50  Parameter::Parameter(void)
51    : cmd_( std::string("Mandatory arguments to long options ") + 
52            std::string("are mandatory for short options too.")), 
53      config_file_(cmd_, "config-file", 
54                   "configuration file [<ROOT>/.svndigest/config]"),
55      force_(cmd_, "f,force", 
56             std::string("if sub-directory named <ROOT> exists in target dire")+
57             std::string("ctory, remove sub-directory before writing results")),
58      generate_config_(cmd_, "g,generate-config", 
59                       "write configuration to standard output"),
60      help_(cmd_),
61      ignore_cache_(cmd_, "ignore-cache", 
62                    std::string("ignore cache files and analyze ") +
63                    std::string("everything from repository")),
64      root_(cmd_, "r,root", 
65            "svn controlled directory to perform statistics on [.]"),
66      verbose_(cmd_, "v,verbose", "explain what is being done"),
67      version_(cmd_, "version", "print version information and exit", &verbose_)
68  {
69  }
70
71  void Parameter::parse(int argc, char* argv[])
72  {
73    init();
74    try {
75      cmd_.parse(argc, argv);
76    }
77    catch (yat::utility::cmd_error& e) {
78      std::cerr << e.what() << std::endl;
79      exit (-1);
80    }
81
82    set_default();
83
84    // analyse arguments
85    analyse();
86  }
87
88
89  Parameter::~Parameter(void)
90  {
91  }
92
93
94  void Parameter::analyse(void)
95  {
96    std::string save_wd = pwd();
97
98    // check root but not if -g option given
99    if (!generate_config()) {
100      check_existence(root_.value());
101      check_readable(root_.value());
102      if (chdir(root_.value().c_str())) {
103        std::stringstream ss;
104        ss << "svndigest: cannot read `" << root_.value() << "': " 
105           << strerror(errno);
106        throw yat::utility::cmd_error(ss.str());
107      }
108      root_.value(pwd());
109      chdir(save_wd.c_str());
110
111    }
112
113    // check config file
114    struct stat nodestat;
115    // true also if there is a broken symlink named...
116    bool config_exists = !lstat(config_file_.value().c_str(), &nodestat);
117    // the latter case in order to catch broken symlink
118    if (config_file_.present() || config_exists)
119      // throws if file does not exists
120      check_existence(config_file_.value());
121    if (config_exists) {
122      // throws if file is not readable
123      check_readable(config_file_.value());
124      stat(config_file_.value().c_str(), &nodestat);
125      if (!S_ISREG(nodestat.st_mode)) {
126        std::stringstream ss;
127        ss << "svndigest: `" << config_file_.value() 
128           << "' is not a regular file";
129        throw yat::utility::cmd_error(ss.str());
130      }
131    }
132    analyse2();
133  }
134
135
136  void Parameter::check_existence(std::string path) const
137  {
138    if (node_exist(path))
139      return;
140    std::stringstream ss;
141    ss << "svndigest: cannot stat `" << path << "': " << strerror(errno);
142    throw yat::utility::cmd_error(ss.str());
143  }
144
145 
146  void Parameter::check_readable(std::string path) const
147  {
148    if (!access_rights(path, "r"))
149      return;
150    std::stringstream ss;
151    ss << "svndigest: cannot open `" << path << "': " << strerror(errno);
152    throw yat::utility::cmd_error(ss.str());
153  }
154 
155
156  std::string Parameter::config_file(void) const
157  {
158    return config_file_.value();
159  }
160
161
162  bool Parameter::force(void) const
163  {
164    return force_.present();
165  }
166
167
168  bool Parameter::generate_config(void) const
169  {
170    return generate_config_.present();
171  }
172
173
174  bool Parameter::ignore_cache(void) const
175  {
176    return ignore_cache_.present();
177  }
178
179
180  void Parameter::init(void)
181  {
182    // we like the options sorted alphabetically
183    cmd_.sort();
184    config_file_.print_arg("=FILE");
185    root_.print_arg("=ROOT");
186    std::stringstream ss;
187    ss << "Report bugs to " << PACKAGE_BUGREPORT << ".\n"
188       << PACKAGE << " home page: <" << PACKAGE_URL << ">.\n";
189    help_.post_arguments() = ss.str();
190    init2();
191  }
192
193
194  std::string Parameter::root(void) const
195  {
196    return root_.value();
197  }
198
199  void Parameter::set_default(void)
200  {
201    if (!root_.present())
202      root_.value(".");
203
204    if (!config_file_.present())
205      config_file_.value(concatenate_path(root_.value(),".svndigest/config"));
206
207    set_default2();
208  }
209
210
211  bool Parameter::verbose(void) const
212  {
213    return verbose_.present();
214  }
215
216
217}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.