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

Last change on this file since 975 was 975, checked in by Peter, 14 years ago

adding Option class for argument associated with a file, and fixed minor issues

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: OptionFile.cc 975 2007-10-17 21:03:37Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of the yat library, http://trac.thep.lu.se/trac/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 "FileUtil.h"
26
27#include <string>
28
29namespace theplu {
30namespace yat {
31namespace utility {
32
33
34  OptionFile::OptionFile(CommandLine& cmd, std::string flag, 
35                         std::string desc, bool exist, std::string bits)
36    : OptionArg<char*>(cmd, flag, desc), exist_(exist), bits_(bits)
37  {
38  }
39
40
41  void OptionFile::do_validate() const
42  {   
43    FileUtil fu(value());
44    if (exist_ && !fu.exists()){
45      std::stringstream ss;
46      ss << "cannot stat `" << value() << "': No such file or directory";
47      throw std::runtime_error(ss.str());
48    }
49    // Peter, this loop is stupid but I wanna differentiate the error message
50    for (size_t i=0; i<bits_.length(); ++i)
51      switch (bits_[i]) {
52      case 'r':
53        if (fu.permissions("r")){
54          std::stringstream ss;
55          ss << "cannot stat `" << value() << "': No such file or directory";
56          throw std::runtime_error(ss.str());
57        }
58        break;
59      case 'w':
60        if (fu.permissions("w")){
61          std::stringstream ss;
62          ss << "cannot create file `" << value() 
63             << "': Permission denied";
64          throw std::runtime_error(ss.str());
65        }
66        break;
67      case 'x':
68        if (fu.permissions("w")){
69          std::stringstream ss;
70          ss << value() << ": Permission denied";
71          throw std::runtime_error(ss.str());
72        }
73        break;
74      }
75  }
76
77
78}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.