source: trunk/yat/utility/FileIO.cc @ 709

Last change on this file since 709 was 709, checked in by Jari Häkkinen, 16 years ago

Addresses #1 and #80. Issues fixed, but bogus params to functions exists. cleanup needed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1// $Id: FileIO.cc 709 2006-12-20 21:49:02Z jari $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7
8  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "FileIO.h"
27
28#include <cerrno>
29#include <iostream>
30#include <string>
31
32#include <sys/stat.h>
33#include <unistd.h>
34
35
36namespace theplu {
37namespace yat {
38namespace utility {
39
40
41  FileIO::FileIO(const std::string& path)
42    : no_file_(false), path_(path)
43  {
44    if ( stat(path_.c_str(),&stat_) && !(no_file_=(errno==ENOENT)) )
45        throw IO_error(std::string("stat(2) call failed with errno: "+errno));
46  }
47
48
49  int FileIO::access_rights(const std::string& path,
50                            const std::string& bits) const
51  {
52    std::string tryme=path_;
53    if (no_file_) {
54      std::string::size_type pos=path_.find_last_of('/');
55      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
56    }
57
58    int mode=0;
59    for (u_int i=0; i<bits.length(); i++)
60      switch (bits[i]) {
61          case 'r':
62            mode|=R_OK;
63            break;
64          case 'w':
65            mode|=W_OK;
66            break;
67          case 'x':
68            mode|=X_OK;
69            break;
70      }
71
72    return access(tryme.c_str(),mode);
73  }
74
75
76  bool FileIO::file_exists(const std::string& file) const
77  {
78    return !no_file_;
79  }
80
81
82  const std::string& FileIO::path(void) const
83  {
84    return path_;
85  }
86
87}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.