975 |
17 Oct 07 |
peter |
// $Id$ |
975 |
17 Oct 07 |
peter |
2 |
|
975 |
17 Oct 07 |
peter |
3 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008, 2009 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2010, 2012, 2013 Peter Johansson |
975 |
17 Oct 07 |
peter |
7 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
975 |
17 Oct 07 |
peter |
9 |
|
975 |
17 Oct 07 |
peter |
The yat library is free software; you can redistribute it and/or |
975 |
17 Oct 07 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
975 |
17 Oct 07 |
peter |
License, or (at your option) any later version. |
975 |
17 Oct 07 |
peter |
14 |
|
975 |
17 Oct 07 |
peter |
The yat library is distributed in the hope that it will be useful, |
975 |
17 Oct 07 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
975 |
17 Oct 07 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
975 |
17 Oct 07 |
peter |
General Public License for more details. |
975 |
17 Oct 07 |
peter |
19 |
|
975 |
17 Oct 07 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
975 |
17 Oct 07 |
peter |
22 |
*/ |
975 |
17 Oct 07 |
peter |
23 |
|
2881 |
18 Nov 12 |
peter |
24 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
25 |
|
975 |
17 Oct 07 |
peter |
26 |
#include "OptionFile.h" |
1178 |
27 Feb 08 |
peter |
27 |
#include "Exception.h" |
975 |
17 Oct 07 |
peter |
28 |
#include "FileUtil.h" |
975 |
17 Oct 07 |
peter |
29 |
|
2208 |
02 Mar 10 |
peter |
30 |
#include <cerrno> |
2055 |
08 Sep 09 |
peter |
31 |
#include <cstring> |
975 |
17 Oct 07 |
peter |
32 |
#include <string> |
975 |
17 Oct 07 |
peter |
33 |
|
975 |
17 Oct 07 |
peter |
34 |
namespace theplu { |
975 |
17 Oct 07 |
peter |
35 |
namespace yat { |
975 |
17 Oct 07 |
peter |
36 |
namespace utility { |
975 |
17 Oct 07 |
peter |
37 |
|
975 |
17 Oct 07 |
peter |
38 |
|
4200 |
19 Aug 22 |
peter |
39 |
OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc, |
980 |
22 Oct 07 |
peter |
40 |
bool required, bool exist, std::string bits) |
4200 |
19 Aug 22 |
peter |
41 |
: OptionArg<std::string>(cmd, flag, desc, required), exist_(exist), |
980 |
22 Oct 07 |
peter |
42 |
bits_(bits) {} |
975 |
17 Oct 07 |
peter |
43 |
|
975 |
17 Oct 07 |
peter |
44 |
|
1014 |
01 Feb 08 |
peter |
45 |
OptionFile::~OptionFile(void) |
1014 |
01 Feb 08 |
peter |
46 |
{ |
1014 |
01 Feb 08 |
peter |
47 |
} |
1014 |
01 Feb 08 |
peter |
48 |
|
1014 |
01 Feb 08 |
peter |
49 |
|
980 |
22 Oct 07 |
peter |
50 |
void OptionFile::do_validate2() const |
2964 |
22 Jan 13 |
peter |
51 |
{ |
979 |
21 Oct 07 |
peter |
52 |
if (!present()) |
979 |
21 Oct 07 |
peter |
53 |
return; |
979 |
21 Oct 07 |
peter |
54 |
FileUtil fu(value().c_str()); |
1628 |
17 Nov 08 |
peter |
55 |
bool exists=false; |
1628 |
17 Nov 08 |
peter |
56 |
try { |
1628 |
17 Nov 08 |
peter |
57 |
exists = fu.exists(); |
1628 |
17 Nov 08 |
peter |
58 |
} |
1628 |
17 Nov 08 |
peter |
59 |
catch (IO_error& e) { |
975 |
17 Oct 07 |
peter |
60 |
std::stringstream ss; |
2964 |
22 Jan 13 |
peter |
61 |
ss << "cannot stat '" << value() << "': " << strerror(errno); |
1628 |
17 Nov 08 |
peter |
62 |
errno = 0; |
1628 |
17 Nov 08 |
peter |
63 |
throw cmd_error(ss.str()); |
1628 |
17 Nov 08 |
peter |
64 |
} |
1628 |
17 Nov 08 |
peter |
65 |
if (exist_ && !exists){ |
1628 |
17 Nov 08 |
peter |
66 |
std::stringstream ss; |
2964 |
22 Jan 13 |
peter |
67 |
ss << "cannot stat '" << value() << "': No such file or directory"; |
1178 |
27 Feb 08 |
peter |
68 |
throw cmd_error(ss.str()); |
975 |
17 Oct 07 |
peter |
69 |
} |
979 |
21 Oct 07 |
peter |
70 |
if (fu.permissions(bits_)) { |
979 |
21 Oct 07 |
peter |
// Peter, this loop is stupid but I wanna differentiate the error message |
2964 |
22 Jan 13 |
peter |
72 |
for (std::string::const_iterator iter=bits_.begin(); |
1954 |
07 May 09 |
jari |
73 |
iter!=bits_.end(); ++iter) { |
979 |
21 Oct 07 |
peter |
74 |
if (*iter=='r' && fu.permissions("r")){ |
975 |
17 Oct 07 |
peter |
75 |
std::stringstream ss; |
2964 |
22 Jan 13 |
peter |
76 |
ss << "cannot open '" << value() << "' for reading: " |
2049 |
04 Sep 09 |
peter |
77 |
<< strerror(errno); |
1178 |
27 Feb 08 |
peter |
78 |
throw cmd_error(ss.str()); |
975 |
17 Oct 07 |
peter |
79 |
} |
979 |
21 Oct 07 |
peter |
80 |
else if (*iter=='w' && fu.permissions("w")){ |
975 |
17 Oct 07 |
peter |
81 |
std::stringstream ss; |
2964 |
22 Jan 13 |
peter |
82 |
ss << "cannot create file '" << value() |
2049 |
04 Sep 09 |
peter |
83 |
<< "': " << strerror(errno); |
1178 |
27 Feb 08 |
peter |
84 |
throw cmd_error(ss.str()); |
975 |
17 Oct 07 |
peter |
85 |
} |
975 |
17 Oct 07 |
peter |
86 |
} |
979 |
21 Oct 07 |
peter |
87 |
std::stringstream ss; |
2964 |
22 Jan 13 |
peter |
88 |
ss << "'" << value() << "': " << strerror(errno); |
1178 |
27 Feb 08 |
peter |
89 |
throw cmd_error(ss.str()); |
979 |
21 Oct 07 |
peter |
90 |
} |
975 |
17 Oct 07 |
peter |
91 |
} |
975 |
17 Oct 07 |
peter |
92 |
|
975 |
17 Oct 07 |
peter |
93 |
|
975 |
17 Oct 07 |
peter |
94 |
}}} // of namespace utility, yat, and theplu |