source: trunk/yat/utility/OptionFile.cc

Last change on this file was 4207, checked in by Peter, 3 months ago

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: OptionFile.cc 4207 2022-08-26 04:36:28Z peter $
2
3/*
4  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010, 2012, 2013, 2022 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 <config.h>
24
25#include "OptionFile.h"
26#include "Exception.h"
27#include "FileUtil.h"
28
29#include <cerrno>
30#include <cstring>
31#include <string>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37
38  OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc,
39                         bool required, bool exist, std::string bits)
40    : OptionArg<std::string>(cmd, flag, desc, required), exist_(exist),
41      bits_(bits) {}
42
43
44  OptionFile::~OptionFile(void)
45  {
46  }
47
48
49  void OptionFile::do_validate2() const
50  {
51    if (!present())
52      return;
53    FileUtil fu(value().c_str());
54    bool exists=false;
55    try {
56      exists = fu.exists();
57    }
58    catch (IO_error& e) {
59      std::stringstream ss;
60      ss << "cannot stat '" << value() << "': " << strerror(errno);
61      errno = 0;
62      throw cmd_error(ss.str());
63    }
64    if (exist_ && !exists){
65      std::stringstream ss;
66      ss << "cannot stat '" << value() << "': No such file or directory";
67      throw cmd_error(ss.str());
68    }
69    if (fu.permissions(bits_)) {
70      // Peter, this loop is stupid but I wanna differentiate the error message
71      for (std::string::const_iterator iter=bits_.begin();
72           iter!=bits_.end(); ++iter) {
73        if (*iter=='r' && fu.permissions("r")){
74          std::stringstream ss;
75          ss << "cannot open '" << value() << "' for reading: "
76             << strerror(errno);
77          throw cmd_error(ss.str());
78        }
79        else if (*iter=='w' && fu.permissions("w")){
80          std::stringstream ss;
81          ss << "cannot create file '" << value()
82             << "': " << strerror(errno);
83          throw cmd_error(ss.str());
84        }
85      }
86      std::stringstream ss;
87      ss << "'" << value() << "': " << strerror(errno);
88      throw cmd_error(ss.str());
89    }
90  }
91
92
93}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.