source: trunk/lib/utility.cc @ 198

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

refs #36 optionally a copyright information, based on Stats, can be displayed using --copyright flag. This will flag will be added to usage output when this feature is mature.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: utility.cc 198 2006-09-09 10:04:27Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  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 "utility.h"
25
26#include <cstdlib>
27#include <fstream>
28#include <sstream>
29#include <stdexcept>
30#include <string>
31#include <sys/param.h>
32#include <unistd.h>
33
34namespace theplu{
35namespace svndigest{
36
37  int access_rights(const std::string& path, const std::string& bits)
38  {
39    if (access(path.c_str(),F_OK)) {
40      throw std::runtime_error(std::string("access_rights: ") + path +
41                               "' does not exist.");
42    }
43    int mode=0;
44    for (u_int i=0; i<bits.length(); ++i)
45      switch (bits[i]) {
46          case 'r':
47            mode|=R_OK;
48            break;
49          case 'w':
50            mode|=W_OK;
51            break;
52          case 'x':
53            mode|=X_OK;
54            break;
55      }
56    return access(path.c_str(),mode);
57  }
58
59
60  std::string file_name(const std::string& full_path)
61  {
62    std::stringstream ss(full_path);
63    std::string name;
64    while (getline(ss,name,'/')) {}
65    return name;
66  }
67
68
69  std::string getenv(const std::string& var)
70  {
71    char* buffer=std::getenv(var.c_str());
72    if (!buffer)
73      throw std::runtime_error("Environment variable "+var+" is not set");
74    return std::string(buffer);
75  }
76
77
78  std::string pwd(void)
79  {
80    char buffer[MAXPATHLEN];
81    if (!getcwd(buffer, MAXPATHLEN))
82      throw std::runtime_error("Failed to get current working directory");
83    return std::string(buffer);
84  }
85
86}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.