source: trunk/bin/AbstractParameter.cc @ 1513

Last change on this file since 1513 was 1513, checked in by Peter Johansson, 9 years ago

remove trailing whitespace

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: AbstractParameter.cc 1513 2012-09-23 04:09:08Z 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 "AbstractParameter.h"
26
27#include "lib/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  AbstractParameter::AbstractParameter(void)
51    : cmd_( std::string("Mandatory arguments to long options ") +
52            std::string("are mandatory for short options too.")),
53      help_(cmd_),
54      root_(cmd_, "r,root",
55            "svn controlled directory to perform statistics on [.]"),
56      verbose_(cmd_, "v,verbose", "explain what is being done"),
57      version_(cmd_, "version", "print version information and exit", &verbose_)
58  {
59  }
60
61
62  AbstractParameter::~AbstractParameter(void)
63  {
64  }
65
66
67  void AbstractParameter::analyse(void)
68  {
69    // call pure virtual function
70    analyse1();
71  }
72
73
74  void AbstractParameter::analyse_root(const std::string root)
75  {
76    check_existence(root);
77    check_readable(root);
78    check_is_dir(root);
79
80    std::string save_wd = pwd();
81    chdir(root);
82    root_full_ = pwd();
83    chdir(save_wd);
84
85    // take care of when root is a symlink (see ticket #477)
86    struct stat stats;
87    lstat(root.c_str(), &stats);
88    if (S_ISLNK(stats.st_mode))
89      root_basename_ = file_name(root);
90    else
91      root_basename_ = file_name(root_full_);
92  }
93
94
95  void AbstractParameter::check_existence(std::string path) const
96  {
97    if (node_exist(path))
98      return;
99    std::stringstream ss;
100    ss << cmd_.program_name() << ": cannot stat '" << path << "': "
101       << strerror(errno);
102    throw yat::utility::cmd_error(ss.str());
103  }
104
105
106  void AbstractParameter::check_is_dir(std::string path) const
107  {
108    if (node_exist(path)) {
109      struct stat buf;
110      stat(path.c_str(), &buf);
111      if (S_ISDIR(buf.st_mode))
112        return;
113      errno = ENOTDIR;
114    }
115    std::stringstream ss;
116    assert(errno);
117    ss << cmd_.program_name() << ": '" << path << "': "
118       << strerror(errno);
119    throw yat::utility::cmd_error(ss.str());
120  }
121
122
123  void AbstractParameter::check_readable(std::string path) const
124  {
125    if (!access_rights(path, "r"))
126      return;
127    std::stringstream ss;
128    ss << cmd_.program_name() << ": cannot open '" << path << "': "
129       << strerror(errno);
130    throw yat::utility::cmd_error(ss.str());
131  }
132
133
134  void AbstractParameter::init(void)
135  {
136    // we like the options sorted alphabetically
137    cmd_.sort();
138    root_.print_arg("=ROOT");
139    std::stringstream ss;
140    ss << "Report bugs to " << PACKAGE_BUGREPORT << ".\n"
141       << PACKAGE << " home page: <" << PACKAGE_URL << ">.\n";
142    help_.post_arguments() = ss.str();
143    init1();
144  }
145
146
147  void AbstractParameter::parse(int argc, char* argv[])
148  {
149    init();
150    cmd_.parse(argc, argv);
151    set_default();
152    // analyse arguments
153    analyse();
154  }
155
156
157  std::string AbstractParameter::root(void) const
158  {
159    return root_full_;
160  }
161
162
163  const std::string& AbstractParameter::root_basename(void) const
164  {
165    assert(root_basename_.size());
166    return root_basename_;
167  }
168
169
170  void AbstractParameter::set_default(void)
171  {
172    if (!root_.present())
173      root_.value(".");
174
175    set_default1();
176  }
177
178
179  bool AbstractParameter::verbose(void) const
180  {
181    return verbose_.present();
182  }
183
184
185}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.