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

Last change on this file since 1437 was 1437, checked in by Peter, 13 years ago

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: OptionFile.cc 1437 2008-08-25 17:55:00Z peter $
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 2 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "OptionFile.h"
26#include "Exception.h"
27#include "FileUtil.h"
28
29#include <string>
30
31namespace theplu {
32namespace yat {
33namespace utility {
34
35
36  OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc, 
37                         bool required, bool exist, std::string bits)
38    : OptionArg<std::string>(cmd, flag, desc, required), exist_(exist), 
39      bits_(bits) {}
40
41
42  OptionFile::~OptionFile(void)
43  {
44  }
45
46
47  void OptionFile::do_validate2() const
48  {   
49    if (!present())
50      return;
51    FileUtil fu(value().c_str());
52    if (exist_ && !fu.exists()){
53      std::stringstream ss;
54      ss << "cannot stat `" << value() << "': No such file or directory";
55      throw cmd_error(ss.str());
56    }
57    if (fu.permissions(bits_)) {
58      // Peter, this loop is stupid but I wanna differentiate the error message
59      for (std::string::const_iterator iter; iter!=bits_.end(); ++iter){
60        if (*iter=='r' && fu.permissions("r")){
61          std::stringstream ss;
62          ss << "cannot stat `" << value() << "': No such file or directory";
63          throw cmd_error(ss.str());
64        }
65        else if (*iter=='w' && fu.permissions("w")){
66          std::stringstream ss;
67          ss << "cannot create file `" << value() 
68             << "': Permission denied";
69          throw cmd_error(ss.str());
70        }
71        else if (*iter=='d' && fu.permissions("d")){
72          std::stringstream ss;
73          ss << value() 
74             << "': Not a directory";
75          throw cmd_error(ss.str());
76        }
77      }
78      std::stringstream ss;
79      ss << value() << ": Permission denied";
80      throw cmd_error(ss.str());
81    }
82  }
83
84
85}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.