source: trunk/lib/utility.cc @ 204

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

removed debug code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: utility.cc 204 2006-09-10 09:03:48Z 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
34#include <iostream>
35
36namespace theplu{
37namespace svndigest{
38
39  int access_rights(const std::string& path, const std::string& bits)
40  {
41    if (access(path.c_str(),F_OK)) {
42      throw std::runtime_error(std::string("access_rights: ") + path +
43                               "' does not exist.");
44    }
45    int mode=0;
46    for (u_int i=0; i<bits.length(); ++i)
47      switch (bits[i]) {
48          case 'r':
49            mode|=R_OK;
50            break;
51          case 'w':
52            mode|=W_OK;
53            break;
54          case 'x':
55            mode|=X_OK;
56            break;
57      }
58    return access(path.c_str(),mode);
59  }
60
61
62  std::string file_name(const std::string& full_path)
63  {
64    std::stringstream ss(full_path);
65    std::string name;
66    while (getline(ss,name,'/')) {}
67    return name;
68  }
69
70
71  std::string getenv(const std::string& var)
72  {
73    char* buffer=std::getenv(var.c_str());
74    if (!buffer)
75      throw std::runtime_error("Environment variable "+var+" is not set");
76    return std::string(buffer);
77  }
78
79
80  std::string pwd(void)
81  {
82    char buffer[MAXPATHLEN];
83    if (!getcwd(buffer, MAXPATHLEN))
84      throw std::runtime_error("Failed to get current working directory");
85    return std::string(buffer);
86  }
87
88  time_t str2time(const std::string& str)
89  {
90    //  str in format 2006-09-09T10:55:52.132733Z
91    std::stringstream sstream(str);
92    time_t rawtime;
93    struct tm * timeinfo;
94    time ( &rawtime );
95    timeinfo =  gmtime ( &rawtime );
96
97    u_int year, month, day, hour, minute, second;
98    std::string tmp;
99    getline(sstream,tmp,'-');
100    year=atoi(tmp.c_str());
101    timeinfo->tm_year = year - 1900;
102
103    getline(sstream,tmp,'-');
104    month=atoi(tmp.c_str());
105    timeinfo->tm_mon = month - 1;
106
107    getline(sstream,tmp,'T');
108    day=atoi(tmp.c_str());
109    timeinfo->tm_mday = day;
110
111    getline(sstream,tmp,':');
112    hour=atoi(tmp.c_str());
113    timeinfo->tm_hour = hour;
114
115    getline(sstream,tmp,':');
116    minute=atoi(tmp.c_str());
117    timeinfo->tm_min = minute;
118
119    getline(sstream,tmp,'.');
120    second=atoi(tmp.c_str());
121    timeinfo->tm_sec = second;
122
123    return mktime(timeinfo);
124  }
125
126
127}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.