source: trunk/yat/utility/OptionFile.cc @ 1566

Last change on this file since 1566 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// $Id: OptionFile.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but 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 yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "OptionFile.h"
24#include "Exception.h"
25#include "FileUtil.h"
26
27#include <string>
28
29namespace theplu {
30namespace yat {
31namespace utility {
32
33
34  OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc, 
35                         bool required, bool exist, std::string bits)
36    : OptionArg<std::string>(cmd, flag, desc, required), exist_(exist), 
37      bits_(bits) {}
38
39
40  OptionFile::~OptionFile(void)
41  {
42  }
43
44
45  void OptionFile::do_validate2() const
46  {   
47    if (!present())
48      return;
49    FileUtil fu(value().c_str());
50    if (exist_ && !fu.exists()){
51      std::stringstream ss;
52      ss << "cannot stat `" << value() << "': No such file or directory";
53      throw cmd_error(ss.str());
54    }
55    if (fu.permissions(bits_)) {
56      // Peter, this loop is stupid but I wanna differentiate the error message
57      for (std::string::const_iterator iter; iter!=bits_.end(); ++iter){
58        if (*iter=='r' && fu.permissions("r")){
59          std::stringstream ss;
60          ss << "cannot stat `" << value() << "': No such file or directory";
61          throw cmd_error(ss.str());
62        }
63        else if (*iter=='w' && fu.permissions("w")){
64          std::stringstream ss;
65          ss << "cannot create file `" << value() 
66             << "': Permission denied";
67          throw cmd_error(ss.str());
68        }
69        else if (*iter=='d' && fu.permissions("d")){
70          std::stringstream ss;
71          ss << value() 
72             << "': Not a directory";
73          throw cmd_error(ss.str());
74        }
75      }
76      std::stringstream ss;
77      ss << value() << ": Permission denied";
78      throw cmd_error(ss.str());
79    }
80  }
81
82
83}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.