source: branches/0.6-stable/lib/utility.cc @ 457

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

Fixes #251 and #255.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1// $Id: utility.cc 457 2007-08-21 09:28:37Z jari $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://trac.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  void copy_file(const std::string& source, const std::string& target)
63  {
64    std::ofstream o(target.c_str());
65    std::ifstream i(source.c_str());
66    while (i.good()) {
67      char ch=i.get();
68      if (i.good())
69        o.put(ch);
70      if (!o.good())
71        throw std::runtime_error(std::string("copy_file: ") +
72                                 "writing target file failed '" + target + "'");
73    }
74    if (!i.eof() && (i.bad() || i.fail()))  // fail on everything except eof
75      throw std::runtime_error(std::string("copy_file: ") +
76                               "error reading source file '" + source + "'");
77    i.close(); o.close();
78  }
79
80
81  std::string file_name(const std::string& full_path)
82  {
83    std::stringstream ss(full_path);
84    std::string name;
85    while (getline(ss,name,'/')) {}
86    return name;
87  }
88
89
90  std::string getenv(const std::string& var)
91  {
92    char* buffer=std::getenv(var.c_str());
93    if (!buffer)
94      throw std::runtime_error("Environment variable "+var+" is not set");
95    return std::string(buffer);
96  }
97
98
99  std::string hex(int x, u_int width)
100  {
101    std::stringstream ss;
102    ss << std::hex << x;
103    if (!width)
104      return ss.str();
105    if (ss.str().size()<width) 
106      return std::string(width-ss.str().size(), '0') + ss.str();
107    return ss.str().substr(0, width);
108  }
109
110
111  std::string htrim(std::string str)
112  {
113    size_t length=str.size();
114    while(length && isspace(str[length-1]))
115      --length;
116    return str.substr(0,length);
117  }
118
119
120  bool is_int(std::string s)
121  {
122    std::stringstream ss(s);
123    int a;
124    ss >> a;
125    if(ss.fail()) 
126      return false;
127    // Check that nothing is left on stream
128    std::string b;
129    ss >> b;
130    return b.empty();
131  }
132
133
134  std::string ltrim(std::string str)
135  {
136    size_t i = 0;
137    while(i<str.size() && isspace(str[i]))
138      ++i;
139    return str.substr(i);
140  }
141
142
143  int percent(int a, int b)
144  {
145    if (b)
146      return (100*a)/b;
147    return 0;
148  }
149
150
151  bool node_exist(const std::string& path)
152  {
153    struct stat buf;
154    return !stat(path.c_str(),&buf);
155  }
156
157
158  std::string pwd(void)
159  {
160    char buffer[MAXPATHLEN];
161    if (!getcwd(buffer, MAXPATHLEN))
162      throw std::runtime_error("Failed to get current working directory");
163    return std::string(buffer);
164  }
165
166
167  void touch(std::string str)
168  {
169    if (!node_exist(str)) {
170      std::ofstream os(str.c_str());
171      os.close();
172    }
173  }
174
175  time_t str2time(const std::string& str)
176  {
177    //  str in format 2006-09-09T10:55:52.132733Z
178    std::stringstream sstream(str);
179    time_t rawtime;
180    struct tm * timeinfo;
181    time ( &rawtime );
182    timeinfo =  gmtime ( &rawtime );
183
184    u_int year, month, day, hour, minute, second;
185    std::string tmp;
186    getline(sstream,tmp,'-');
187    year=atoi(tmp.c_str());
188    timeinfo->tm_year = year - 1900;
189
190    getline(sstream,tmp,'-');
191    month=atoi(tmp.c_str());
192    timeinfo->tm_mon = month - 1;
193
194    getline(sstream,tmp,'T');
195    day=atoi(tmp.c_str());
196    timeinfo->tm_mday = day;
197
198    getline(sstream,tmp,':');
199    hour=atoi(tmp.c_str());
200    timeinfo->tm_hour = hour;
201
202    getline(sstream,tmp,':');
203    minute=atoi(tmp.c_str());
204    timeinfo->tm_min = minute;
205
206    getline(sstream,tmp,'.');
207    second=atoi(tmp.c_str());
208    timeinfo->tm_sec = second;
209
210    return mktime(timeinfo);
211  }
212
213
214  std::string match(std::string::const_iterator& first,
215                    const std::string::const_iterator& last,
216                    std::string str)
217  {
218    if (match_begin(first, last, str)){
219      first+=str.size();
220      return str;
221    }
222    return std::string();
223  }
224
225  notChar::notChar(char c)
226    : char_(c)
227  {}
228
229
230  not2Char::not2Char(char c1, char c2)
231    : char1_(c1), char2_(c2)
232  {}
233   
234
235  not2Str::not2Str(std::string s1, std::string s2)
236    : str1_(s1), str2_(s2)
237  {}
238   
239}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.