[37] | 1 | // $Id: Parameter.cc 430 2007-07-06 23:33:11Z peter $ |
---|
| 2 | |
---|
[84] | 3 | /* |
---|
[269] | 4 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
[84] | 5 | |
---|
[430] | 6 | This file is part of svndigest, http://trac.thep.lu.se/trac/svndigest |
---|
[84] | 7 | |
---|
[149] | 8 | svndigest is free software; you can redistribute it and/or modify it |
---|
[84] | 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 | |
---|
[149] | 13 | svndigest is distributed in the hope that it will be useful, but |
---|
[84] | 14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[149] | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
[84] | 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 | |
---|
[37] | 24 | #include "Parameter.h" |
---|
[234] | 25 | |
---|
| 26 | #include "ColumnStream.h" |
---|
[116] | 27 | #include "utility.h" |
---|
[83] | 28 | #include <config.h> // this header file is created by configure |
---|
[37] | 29 | |
---|
[226] | 30 | #include <fstream> |
---|
[37] | 31 | #include <iostream> |
---|
[226] | 32 | #include <sstream> |
---|
[49] | 33 | #include <stdexcept> |
---|
[37] | 34 | #include <string> |
---|
[104] | 35 | #include <sys/stat.h> |
---|
[37] | 36 | |
---|
| 37 | namespace theplu { |
---|
[149] | 38 | namespace svndigest { |
---|
[37] | 39 | |
---|
| 40 | Parameter::Parameter(const int argc,const char *argv[]) |
---|
| 41 | { |
---|
[73] | 42 | defaults(); |
---|
[173] | 43 | for (int i=1; i<argc; ++i) { |
---|
[37] | 44 | bool ok=false; |
---|
| 45 | std::string myargv(argv[i]); |
---|
[234] | 46 | if (myargv=="--config-file"){ |
---|
[226] | 47 | if (++i<argc){ |
---|
[229] | 48 | config_file_= std::string(argv[i]); |
---|
[37] | 49 | ok=true; |
---|
[226] | 50 | } |
---|
[37] | 51 | } |
---|
[226] | 52 | else if (myargv=="--copyright"){ |
---|
[198] | 53 | copyright_=true; |
---|
| 54 | ok=true; |
---|
| 55 | } |
---|
[226] | 56 | else if (myargv=="-f" || myargv=="--force"){ |
---|
| 57 | force_=true; |
---|
| 58 | ok=true; |
---|
| 59 | } |
---|
[229] | 60 | else if (myargv=="-g" || myargv=="--generate-config"){ |
---|
| 61 | generate_config_=true; |
---|
| 62 | ok=true; |
---|
| 63 | } |
---|
[39] | 64 | else if (myargv=="-h" || myargv=="--help"){ |
---|
[37] | 65 | help(); |
---|
| 66 | exit(0); // always exit after printing help |
---|
| 67 | } |
---|
[52] | 68 | else if (myargv=="-r" || myargv=="--root"){ |
---|
| 69 | if (++i<argc){ |
---|
| 70 | root_= std::string(argv[i]); |
---|
| 71 | ok=true; |
---|
| 72 | } |
---|
| 73 | } |
---|
[240] | 74 | else if (myargv=="--revisions") { |
---|
[73] | 75 | revisions_=true; |
---|
| 76 | ok=true; |
---|
| 77 | } |
---|
[52] | 78 | else if (myargv=="-t" || myargv=="--target"){ |
---|
| 79 | if (++i<argc){ |
---|
| 80 | targetdir_= std::string(argv[i]); |
---|
| 81 | ok=true; |
---|
| 82 | } |
---|
| 83 | } |
---|
[39] | 84 | else if (myargv=="-v" || myargv=="--verbose"){ |
---|
[37] | 85 | verbose_=true; |
---|
| 86 | ok=true; |
---|
| 87 | } |
---|
[39] | 88 | else if (myargv=="--version"){ |
---|
[37] | 89 | version(); |
---|
| 90 | exit(0); |
---|
| 91 | } |
---|
[180] | 92 | else if (myargv=="-vf" || myargv=="-fv"){ |
---|
| 93 | verbose_=true; |
---|
| 94 | force_=true; |
---|
| 95 | ok=true; |
---|
| 96 | } |
---|
[49] | 97 | |
---|
| 98 | if (!ok) |
---|
[149] | 99 | throw std::runtime_error("svndigest: invalid option: " + myargv + |
---|
[280] | 100 | "\nType `svndigest --help' for usage."); |
---|
[37] | 101 | } |
---|
| 102 | |
---|
| 103 | analyse(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | void Parameter::analyse(void) |
---|
| 108 | { |
---|
[151] | 109 | using namespace std; |
---|
| 110 | |
---|
| 111 | string workdir(pwd()); // remember current working directory (cwd). |
---|
| 112 | |
---|
| 113 | // Checking that root_ exists and retrieve the absolute path to root_ |
---|
| 114 | if (chdir(root_.c_str())) |
---|
| 115 | throw runtime_error(string("svndigest: Root directory (") + root_ + |
---|
| 116 | ") access failed."); |
---|
[116] | 117 | root_ = pwd(); |
---|
| 118 | |
---|
[151] | 119 | // need to get back to cwd if relative paths are used. |
---|
| 120 | if (chdir(workdir.c_str())) |
---|
| 121 | runtime_error(string("svndigest: Failed to access cwd: ") + workdir); |
---|
| 122 | |
---|
| 123 | // Checking that targetdir_ exists and retrieve the absolute path |
---|
| 124 | // to targetdir_ |
---|
| 125 | if (chdir(targetdir_.c_str())) |
---|
| 126 | throw runtime_error(string("svndigest: Target directory (") + targetdir_ + |
---|
| 127 | ") access failed."); |
---|
[116] | 128 | targetdir_ = pwd(); |
---|
[151] | 129 | // Checking write permissions for targetdir_ |
---|
| 130 | if (access_rights(targetdir_,"w")) |
---|
| 131 | throw runtime_error(string("svndigest: No write permission on target ") + |
---|
| 132 | "directory (" + targetdir_ +")."); |
---|
[116] | 133 | |
---|
[151] | 134 | // return back to cwd |
---|
| 135 | if (chdir(workdir.c_str())) |
---|
[240] | 136 | throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir); |
---|
[229] | 137 | |
---|
[240] | 138 | } |
---|
[229] | 139 | |
---|
[240] | 140 | |
---|
| 141 | std::string Parameter::config_file(void) const |
---|
| 142 | { |
---|
| 143 | // not default |
---|
| 144 | if (!config_file_.empty()) |
---|
| 145 | return config_file_; |
---|
| 146 | |
---|
| 147 | // default behaviour |
---|
| 148 | return root()+"/.svndigest/config"; |
---|
[37] | 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
[73] | 152 | void Parameter::defaults(void) |
---|
| 153 | { |
---|
[240] | 154 | config_file_ = ""; |
---|
[198] | 155 | copyright_=false; |
---|
[73] | 156 | force_=false; |
---|
[229] | 157 | generate_config_=false; |
---|
[73] | 158 | revisions_=false; |
---|
| 159 | root_="."; |
---|
[104] | 160 | targetdir_="."; |
---|
[73] | 161 | verbose_=false; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
[37] | 165 | void Parameter::help(void) |
---|
| 166 | { |
---|
[116] | 167 | defaults(); |
---|
[234] | 168 | |
---|
| 169 | ColumnStream cs(std::cout, 1); |
---|
[280] | 170 | cs.width(0)=79; |
---|
| 171 | cs.margin(0)=0; |
---|
[241] | 172 | ColumnStream cs2(std::cout, 2); |
---|
[234] | 173 | cs2.width(0)=24; |
---|
[269] | 174 | cs2.width(1)=52; |
---|
[234] | 175 | cs2.margin(0)=2; |
---|
| 176 | |
---|
[280] | 177 | std::cout << "Usage: svndigest [OPTION]...\n" |
---|
[234] | 178 | << "\n"; |
---|
| 179 | |
---|
[280] | 180 | cs << "Generate statistical report for a subversion repository.\n"; |
---|
[234] | 181 | |
---|
[280] | 182 | cs << "\nMandatory arguments to long options are mandatory for " |
---|
| 183 | << "short options too.\n"; |
---|
| 184 | |
---|
[241] | 185 | cs2 << " --copyright\tupdate copyright statement\n" |
---|
| 186 | << " --config-file=ARG\tconfiguration file " |
---|
[240] | 187 | << "[<ROOT>/.svndigest/config]\n" |
---|
[374] | 188 | << "-f, --force\tif sub-directory named <ROOT> exists in " |
---|
| 189 | << "target directory, remove sub-directory before writing results.\n" |
---|
[241] | 190 | << "-g, --generate-config\twrite configuration file " |
---|
[234] | 191 | << "to standard output and exit\n" |
---|
[241] | 192 | << "-h, --help\tdisplay this help and exit\n" |
---|
| 193 | << "-r, --root=ROOT\tsvn controlled directory to perform " |
---|
[234] | 194 | << "statistics calculation on [" << root_ << "]\n" |
---|
[241] | 195 | << " --revisions\tUse revision numbers as time scale " |
---|
[234] | 196 | << "instead of dates [dates].\n" |
---|
[241] | 197 | << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n" |
---|
| 198 | << "-v, --verbose\texplain what is being done\n" |
---|
| 199 | << " --version\tprint version information and exit\n"; |
---|
[280] | 200 | |
---|
| 201 | std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." |
---|
| 202 | << std::endl; |
---|
[37] | 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
[98] | 206 | void Parameter::version(void) const |
---|
[37] | 207 | { |
---|
[280] | 208 | ColumnStream cs(std::cout, 1); |
---|
| 209 | cs.width(0)=79; |
---|
| 210 | cs << PACKAGE_STRING |
---|
| 211 | << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n" |
---|
[283] | 212 | << "This is free software; see the source for copying conditions. " |
---|
| 213 | << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR " |
---|
[280] | 214 | << "A PARTICULAR PURPOSE.\n"; |
---|
[37] | 215 | } |
---|
| 216 | |
---|
| 217 | }} // of namespace wenni and namespace theplu |
---|