37 |
13 Jan 06 |
peter |
// $Id$ |
37 |
13 Jan 06 |
peter |
2 |
|
84 |
13 Mar 06 |
jari |
3 |
/* |
978 |
12 Dec 09 |
peter |
Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2009, 2010, 2011 Peter Johansson |
84 |
13 Mar 06 |
jari |
6 |
|
687 |
04 Aug 08 |
peter |
This file is part of svndigest, http://dev.thep.lu.se/svndigest |
84 |
13 Mar 06 |
jari |
8 |
|
149 |
12 Aug 06 |
jari |
svndigest is free software; you can redistribute it and/or modify it |
84 |
13 Mar 06 |
jari |
under the terms of the GNU General Public License as published by |
693 |
11 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
84 |
13 Mar 06 |
jari |
(at your option) any later version. |
84 |
13 Mar 06 |
jari |
13 |
|
149 |
12 Aug 06 |
jari |
svndigest is distributed in the hope that it will be useful, but |
84 |
13 Mar 06 |
jari |
WITHOUT ANY WARRANTY; without even the implied warranty of |
149 |
12 Aug 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
84 |
13 Mar 06 |
jari |
General Public License for more details. |
84 |
13 Mar 06 |
jari |
18 |
|
84 |
13 Mar 06 |
jari |
You should have received a copy of the GNU General Public License |
693 |
11 Sep 08 |
jari |
along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
84 |
13 Mar 06 |
jari |
21 |
*/ |
84 |
13 Mar 06 |
jari |
22 |
|
960 |
07 Dec 09 |
peter |
23 |
#include <config.h> // this header file is created by configure |
960 |
07 Dec 09 |
peter |
24 |
|
1293 |
14 Nov 10 |
peter |
25 |
#include "AbstractParameter.h" |
234 |
09 Apr 07 |
peter |
26 |
|
1119 |
04 Jul 10 |
peter |
27 |
#include "lib/OptionVersion.h" |
1119 |
04 Jul 10 |
peter |
28 |
#include "lib/utility.h" // to avoid inclusion of yat file |
795 |
30 Jun 09 |
peter |
29 |
|
795 |
30 Jun 09 |
peter |
30 |
#include "yat/ColumnStream.h" |
795 |
30 Jun 09 |
peter |
31 |
#include "yat/Exception.h" |
795 |
30 Jun 09 |
peter |
32 |
#include "yat/OptionArg.h" |
795 |
30 Jun 09 |
peter |
33 |
#include "yat/OptionHelp.h" |
795 |
30 Jun 09 |
peter |
34 |
#include "yat/OptionSwitch.h" |
795 |
30 Jun 09 |
peter |
35 |
|
705 |
25 Nov 08 |
peter |
36 |
#include <cassert> |
705 |
25 Nov 08 |
peter |
37 |
#include <cerrno> |
540 |
29 Dec 07 |
peter |
38 |
#include <cstddef> |
727 |
11 Dec 08 |
jari |
39 |
#include <cstring> |
226 |
11 Mar 07 |
peter |
40 |
#include <fstream> |
37 |
13 Jan 06 |
peter |
41 |
#include <iostream> |
226 |
11 Mar 07 |
peter |
42 |
#include <sstream> |
49 |
14 Jan 06 |
jari |
43 |
#include <stdexcept> |
37 |
13 Jan 06 |
peter |
44 |
#include <string> |
104 |
27 Jun 06 |
peter |
45 |
#include <sys/stat.h> |
37 |
13 Jan 06 |
peter |
46 |
|
37 |
13 Jan 06 |
peter |
47 |
namespace theplu { |
149 |
12 Aug 06 |
jari |
48 |
namespace svndigest { |
37 |
13 Jan 06 |
peter |
49 |
|
1293 |
14 Nov 10 |
peter |
50 |
AbstractParameter::AbstractParameter(void) |
1513 |
23 Sep 12 |
peter |
51 |
: cmd_( std::string("Mandatory arguments to long options ") + |
1513 |
23 Sep 12 |
peter |
52 |
std::string("are mandatory for short options too.")), |
820 |
12 Sep 09 |
peter |
53 |
help_(cmd_), |
1513 |
23 Sep 12 |
peter |
54 |
root_(cmd_, "r,root", |
820 |
12 Sep 09 |
peter |
55 |
"svn controlled directory to perform statistics on [.]"), |
820 |
12 Sep 09 |
peter |
56 |
verbose_(cmd_, "v,verbose", "explain what is being done"), |
820 |
12 Sep 09 |
peter |
57 |
version_(cmd_, "version", "print version information and exit", &verbose_) |
37 |
13 Jan 06 |
peter |
58 |
{ |
1060 |
02 Jun 10 |
peter |
59 |
} |
1060 |
02 Jun 10 |
peter |
60 |
|
1241 |
24 Oct 10 |
peter |
61 |
|
1293 |
14 Nov 10 |
peter |
62 |
AbstractParameter::~AbstractParameter(void) |
1060 |
02 Jun 10 |
peter |
63 |
{ |
705 |
25 Nov 08 |
peter |
64 |
} |
705 |
25 Nov 08 |
peter |
65 |
|
705 |
25 Nov 08 |
peter |
66 |
|
1293 |
14 Nov 10 |
peter |
67 |
void AbstractParameter::analyse(void) |
705 |
25 Nov 08 |
peter |
68 |
{ |
1293 |
14 Nov 10 |
peter |
// call pure virtual function |
1293 |
14 Nov 10 |
peter |
70 |
analyse1(); |
705 |
25 Nov 08 |
peter |
71 |
} |
705 |
25 Nov 08 |
peter |
72 |
|
705 |
25 Nov 08 |
peter |
73 |
|
1293 |
14 Nov 10 |
peter |
74 |
void AbstractParameter::analyse_root(const std::string root) |
705 |
25 Nov 08 |
peter |
75 |
{ |
1293 |
14 Nov 10 |
peter |
76 |
check_existence(root); |
1293 |
14 Nov 10 |
peter |
77 |
check_readable(root); |
1293 |
14 Nov 10 |
peter |
78 |
check_is_dir(root); |
1513 |
23 Sep 12 |
peter |
79 |
|
705 |
25 Nov 08 |
peter |
80 |
std::string save_wd = pwd(); |
1293 |
14 Nov 10 |
peter |
81 |
chdir(root); |
1293 |
14 Nov 10 |
peter |
82 |
root_full_ = pwd(); |
1293 |
14 Nov 10 |
peter |
83 |
chdir(save_wd); |
1513 |
23 Sep 12 |
peter |
84 |
|
1293 |
14 Nov 10 |
peter |
// take care of when root is a symlink (see ticket #477) |
1293 |
14 Nov 10 |
peter |
86 |
struct stat stats; |
1293 |
14 Nov 10 |
peter |
87 |
lstat(root.c_str(), &stats); |
1293 |
14 Nov 10 |
peter |
88 |
if (S_ISLNK(stats.st_mode)) |
1293 |
14 Nov 10 |
peter |
89 |
root_basename_ = file_name(root); |
1293 |
14 Nov 10 |
peter |
90 |
else |
1293 |
14 Nov 10 |
peter |
91 |
root_basename_ = file_name(root_full_); |
626 |
23 Apr 08 |
peter |
92 |
} |
37 |
13 Jan 06 |
peter |
93 |
|
705 |
25 Nov 08 |
peter |
94 |
|
1293 |
14 Nov 10 |
peter |
95 |
void AbstractParameter::check_existence(std::string path) const |
705 |
25 Nov 08 |
peter |
96 |
{ |
705 |
25 Nov 08 |
peter |
97 |
if (node_exist(path)) |
705 |
25 Nov 08 |
peter |
98 |
return; |
705 |
25 Nov 08 |
peter |
99 |
std::stringstream ss; |
1513 |
23 Sep 12 |
peter |
100 |
ss << cmd_.program_name() << ": cannot stat '" << path << "': " |
1066 |
06 Jun 10 |
peter |
101 |
<< strerror(errno); |
795 |
30 Jun 09 |
peter |
102 |
throw yat::utility::cmd_error(ss.str()); |
37 |
13 Jan 06 |
peter |
103 |
} |
37 |
13 Jan 06 |
peter |
104 |
|
1513 |
23 Sep 12 |
peter |
105 |
|
1293 |
14 Nov 10 |
peter |
106 |
void AbstractParameter::check_is_dir(std::string path) const |
1220 |
13 Oct 10 |
peter |
107 |
{ |
1220 |
13 Oct 10 |
peter |
108 |
if (node_exist(path)) { |
1220 |
13 Oct 10 |
peter |
109 |
struct stat buf; |
1220 |
13 Oct 10 |
peter |
110 |
stat(path.c_str(), &buf); |
1220 |
13 Oct 10 |
peter |
111 |
if (S_ISDIR(buf.st_mode)) |
1220 |
13 Oct 10 |
peter |
112 |
return; |
1373 |
10 Jun 11 |
peter |
113 |
errno = ENOTDIR; |
1220 |
13 Oct 10 |
peter |
114 |
} |
1220 |
13 Oct 10 |
peter |
115 |
std::stringstream ss; |
1373 |
10 Jun 11 |
peter |
116 |
assert(errno); |
1513 |
23 Sep 12 |
peter |
117 |
ss << cmd_.program_name() << ": '" << path << "': " |
1220 |
13 Oct 10 |
peter |
118 |
<< strerror(errno); |
1220 |
13 Oct 10 |
peter |
119 |
throw yat::utility::cmd_error(ss.str()); |
1220 |
13 Oct 10 |
peter |
120 |
} |
1220 |
13 Oct 10 |
peter |
121 |
|
1513 |
23 Sep 12 |
peter |
122 |
|
1293 |
14 Nov 10 |
peter |
123 |
void AbstractParameter::check_readable(std::string path) const |
705 |
25 Nov 08 |
peter |
124 |
{ |
705 |
25 Nov 08 |
peter |
125 |
if (!access_rights(path, "r")) |
705 |
25 Nov 08 |
peter |
126 |
return; |
705 |
25 Nov 08 |
peter |
127 |
std::stringstream ss; |
1513 |
23 Sep 12 |
peter |
128 |
ss << cmd_.program_name() << ": cannot open '" << path << "': " |
1066 |
06 Jun 10 |
peter |
129 |
<< strerror(errno); |
795 |
30 Jun 09 |
peter |
130 |
throw yat::utility::cmd_error(ss.str()); |
705 |
25 Nov 08 |
peter |
131 |
} |
37 |
13 Jan 06 |
peter |
132 |
|
1513 |
23 Sep 12 |
peter |
133 |
|
1293 |
14 Nov 10 |
peter |
134 |
void AbstractParameter::init(void) |
37 |
13 Jan 06 |
peter |
135 |
{ |
1064 |
05 Jun 10 |
peter |
// we like the options sorted alphabetically |
1064 |
05 Jun 10 |
peter |
137 |
cmd_.sort(); |
820 |
12 Sep 09 |
peter |
138 |
root_.print_arg("=ROOT"); |
705 |
25 Nov 08 |
peter |
139 |
std::stringstream ss; |
829 |
27 Oct 09 |
peter |
140 |
ss << "Report bugs to " << PACKAGE_BUGREPORT << ".\n" |
829 |
27 Oct 09 |
peter |
141 |
<< PACKAGE << " home page: <" << PACKAGE_URL << ">.\n"; |
820 |
12 Sep 09 |
peter |
142 |
help_.post_arguments() = ss.str(); |
1293 |
14 Nov 10 |
peter |
143 |
init1(); |
37 |
13 Jan 06 |
peter |
144 |
} |
37 |
13 Jan 06 |
peter |
145 |
|
37 |
13 Jan 06 |
peter |
146 |
|
1293 |
14 Nov 10 |
peter |
147 |
void AbstractParameter::parse(int argc, char* argv[]) |
73 |
04 Mar 06 |
jari |
148 |
{ |
1293 |
14 Nov 10 |
peter |
149 |
init(); |
1293 |
14 Nov 10 |
peter |
150 |
cmd_.parse(argc, argv); |
1293 |
14 Nov 10 |
peter |
151 |
set_default(); |
1293 |
14 Nov 10 |
peter |
// analyse arguments |
1293 |
14 Nov 10 |
peter |
153 |
analyse(); |
1293 |
14 Nov 10 |
peter |
154 |
} |
1293 |
14 Nov 10 |
peter |
155 |
|
1293 |
14 Nov 10 |
peter |
156 |
|
1293 |
14 Nov 10 |
peter |
157 |
std::string AbstractParameter::root(void) const |
1293 |
14 Nov 10 |
peter |
158 |
{ |
1264 |
02 Nov 10 |
peter |
159 |
return root_full_; |
705 |
25 Nov 08 |
peter |
160 |
} |
234 |
09 Apr 07 |
peter |
161 |
|
1264 |
02 Nov 10 |
peter |
162 |
|
1293 |
14 Nov 10 |
peter |
163 |
const std::string& AbstractParameter::root_basename(void) const |
1264 |
02 Nov 10 |
peter |
164 |
{ |
1264 |
02 Nov 10 |
peter |
165 |
assert(root_basename_.size()); |
1264 |
02 Nov 10 |
peter |
166 |
return root_basename_; |
1264 |
02 Nov 10 |
peter |
167 |
} |
1264 |
02 Nov 10 |
peter |
168 |
|
1264 |
02 Nov 10 |
peter |
169 |
|
1293 |
14 Nov 10 |
peter |
170 |
void AbstractParameter::set_default(void) |
705 |
25 Nov 08 |
peter |
171 |
{ |
1060 |
02 Jun 10 |
peter |
172 |
if (!root_.present()) |
1060 |
02 Jun 10 |
peter |
173 |
root_.value("."); |
234 |
09 Apr 07 |
peter |
174 |
|
1293 |
14 Nov 10 |
peter |
175 |
set_default1(); |
37 |
13 Jan 06 |
peter |
176 |
} |
37 |
13 Jan 06 |
peter |
177 |
|
626 |
23 Apr 08 |
peter |
178 |
|
1293 |
14 Nov 10 |
peter |
179 |
bool AbstractParameter::verbose(void) const |
37 |
13 Jan 06 |
peter |
180 |
{ |
820 |
12 Sep 09 |
peter |
181 |
return verbose_.present(); |
37 |
13 Jan 06 |
peter |
182 |
} |
37 |
13 Jan 06 |
peter |
183 |
|
705 |
25 Nov 08 |
peter |
184 |
|
442 |
16 Aug 07 |
peter |
185 |
}} // of namespace svndigest and namespace theplu |