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

Last change on this file since 1178 was 1178, checked in by Peter, 15 years ago

fixes #344

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