source: trunk/bin/AbstractParameter.cc @ 1293

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

rewrite svndigest-copy-cache in C++. closes #463

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: AbstractParameter.cc 1293 2010-11-14 05:34:10Z 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    }
114    std::stringstream ss;
115    ss << cmd_.program_name() << ": '" << path << "': " 
116       << strerror(errno);
117    throw yat::utility::cmd_error(ss.str());
118  }
119
120 
121  void AbstractParameter::check_readable(std::string path) const
122  {
123    if (!access_rights(path, "r"))
124      return;
125    std::stringstream ss;
126    ss << cmd_.program_name() << ": cannot open '" << path << "': " 
127       << strerror(errno);
128    throw yat::utility::cmd_error(ss.str());
129  }
130 
131
132  void AbstractParameter::init(void)
133  {
134    // we like the options sorted alphabetically
135    cmd_.sort();
136    root_.print_arg("=ROOT");
137    std::stringstream ss;
138    ss << "Report bugs to " << PACKAGE_BUGREPORT << ".\n"
139       << PACKAGE << " home page: <" << PACKAGE_URL << ">.\n";
140    help_.post_arguments() = ss.str();
141    init1();
142  }
143
144
145  void AbstractParameter::parse(int argc, char* argv[])
146  {
147    init();
148    cmd_.parse(argc, argv);
149    set_default();
150    // analyse arguments
151    analyse();
152  }
153
154
155  std::string AbstractParameter::root(void) const
156  {
157    return root_full_;
158  }
159
160
161  const std::string& AbstractParameter::root_basename(void) const
162  {
163    assert(root_basename_.size());
164    return root_basename_;
165  }
166
167
168  void AbstractParameter::set_default(void)
169  {
170    if (!root_.present())
171      root_.value(".");
172
173    set_default1();
174  }
175
176
177  bool AbstractParameter::verbose(void) const
178  {
179    return verbose_.present();
180  }
181
182
183}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.