source: trunk/lib/utility.cc @ 1132

Last change on this file since 1132 was 1132, checked in by Peter Johansson, 13 years ago

when updating copyright, write to #file# and then rename that file rather than writing in /tmp which might be a a different device and rename doesn't work. fixes #285

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1// $Id: utility.cc 1132 2010-07-16 03:24:35Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "utility.h"
24
25#include "subversion_info.h"
26#include "config.h"
27
28#include "yat/Exception.h"
29
30#include <cassert>
31#include <cerrno> 
32#include <cstdio>
33#include <cstdlib>
34#include <cstring>
35#include <fnmatch.h>
36#include <fstream>
37#include <sstream>
38#include <stdexcept>
39#include <string>
40#include <sys/param.h>
41#include <sys/stat.h>
42#include <unistd.h>
43
44#include <iostream>
45
46namespace theplu{
47namespace svndigest{
48
49  int access_rights(const std::string& path, const std::string& bits)
50  {
51    if (access(path.c_str(),F_OK)) {
52      throw std::runtime_error(std::string("access_rights: ") + path +
53                               "' does not exist.");
54    }
55    int mode=0;
56    for (unsigned int i=0; i<bits.length(); ++i)
57      switch (bits[i]) {
58          case 'r':
59            mode|=R_OK;
60            break;
61          case 'w':
62            mode|=W_OK;
63            break;
64          case 'x':
65            mode|=X_OK;
66            break;
67      }
68    return access(path.c_str(),mode);
69  }
70
71
72  void chdir(const std::string& dir)
73  {
74    if (::chdir(dir.c_str()) )
75      throw yat::utility::errno_error("chdir: " + dir + ":");
76  }
77
78
79  std::string concatenate_path(std::string dir, std::string base)
80  {
81    if (dir.empty() || dir==".")
82      return base;
83    if (dir[dir.size()-1]!='/')
84      dir.append("/");
85    return dir+base;
86  }
87
88
89  void copy_file(const std::string& source, const std::string& target)
90  {
91    std::ofstream o(target.c_str());
92    std::ifstream i(source.c_str());
93    while (i.good()) {
94      char ch=i.get();
95      if (i.good())
96        o.put(ch);
97      if (!o.good())
98        throw std::runtime_error(std::string("copy_file: ") +
99                                 "writing target file failed '" + target + "'");
100    }
101    if (!i.eof() && (i.bad() || i.fail()))  // fail on everything except eof
102      throw std::runtime_error(std::string("copy_file: ") +
103                               "error reading source file '" + source + "'");
104    i.close(); o.close();
105  }
106
107
108  std::string directory_name(std::string path)
109  {
110    size_t pos = path.find_last_of("/");
111    if (pos==std::string::npos)
112      return ".";
113    if (pos==path.size()-1)
114      return directory_name(path.substr(0,path.size()-2));
115    return path.substr(0,pos+1);
116  }
117
118
119  std::string file_name(const std::string& full_path)
120  {
121    std::stringstream ss(full_path);
122    std::string name;
123    while (getline(ss,name,'/')) {}
124    return name;
125  }
126
127
128  std::string getenv(const std::string& var)
129  {
130    char* buffer=std::getenv(var.c_str());
131    if (!buffer)
132      throw std::runtime_error("Environment variable "+var+" is not set");
133    return std::string(buffer);
134  }
135
136
137  std::string hex(int x, unsigned int width)
138  {
139    std::stringstream ss;
140    ss << std::hex << x;
141    if (!width)
142      return ss.str();
143    if (ss.str().size()<width) 
144      return std::string(width-ss.str().size(), '0') + ss.str();
145    return ss.str().substr(0, width);
146  }
147
148
149  std::string htrim(std::string str)
150  {
151    size_t length=str.size();
152    while(length && isspace(str[length-1]))
153      --length;
154    return str.substr(0,length);
155  }
156
157
158  bool fnmatch(const std::string& pattern, const std::string& str)
159  {
160    int res = ::fnmatch(pattern.c_str(), str.c_str(), 0);
161    if (res==0)
162      return true;
163    if (res!=FNM_NOMATCH) {
164      std::stringstream ss;
165      ss << "fnmatch with args: " << pattern << ", " << str;
166      throw std::runtime_error(ss.str());
167    }                     
168    return false;
169  }
170
171
172  std::string ltrim(std::string str)
173  {
174    size_t i = 0;
175    while(i<str.size() && isspace(str[i]))
176      ++i;
177    return str.substr(i);
178  }
179
180  void mkdir(const std::string& dir)
181  { 
182    int code = ::mkdir(dir.c_str(),0777);
183    if (code){
184      std::stringstream ss;
185      ss << "mkdir(" << dir << "): failed\n" << strerror(errno);
186      throw std::runtime_error(ss.str());
187    }
188  }
189
190
191  void mkdir_p(const std::string& dir)
192  { 
193    if (node_exist(dir))
194      return;
195    std::string mother = directory_name(dir);
196    mkdir_p(mother);
197    mkdir(dir);
198  }
199
200
201  bool node_exist(const std::string& path)
202  {
203    struct stat buf;
204    return !stat(path.c_str(),&buf);
205  }
206
207
208  int percent(int a, int b)
209  {
210    if (b)
211      return (100*a)/b;
212    return 0;
213  }
214
215
216  std::string pwd(void)
217  {
218    char buffer[MAXPATHLEN];
219    if (!getcwd(buffer, MAXPATHLEN))
220      throw std::runtime_error("Failed to get current working directory");
221    return std::string(buffer);
222  }
223
224
225  bool regexp(const std::string& pattern, const std::string& str,
226              std::vector<std::string>& vec)
227  {
228    bool regexp__(std::string::const_iterator first1, 
229                  std::string::const_iterator last1,
230                  std::string::const_iterator first2,
231                  std::string::const_iterator last2,
232                  std::vector<std::string>::iterator item);
233   
234    // find number of special chars
235    size_t count=0;
236    for (std::string::const_iterator i=pattern.begin(); i!=pattern.end(); ++i)
237      if (*i=='*' || *i=='?' || *i=='[')
238        ++count;
239    vec.resize(count);
240    return regexp__(pattern.begin(), pattern.end(), str.begin(), str.end(),
241                    vec.begin());
242  }
243
244  bool regexp__(std::string::const_iterator first1, 
245                std::string::const_iterator last1,
246                std::string::const_iterator first2,
247                std::string::const_iterator last2,
248                std::vector<std::string>::iterator item)
249  {
250    if (first1==last1) {
251      return first2==last2;
252    }
253    if (*first1 == '*') {
254      if (first2<last2) {
255        item->push_back(*first2);
256        if (regexp__(first1, last1, first2+1, last2, item))
257          return true;
258        item->resize(item->size()-1);
259      }
260      return regexp__(first1+1, last1, first2, last2, item+1);
261    }
262    if (*first1 == '?') {
263      if (first2==last2)
264        return false;
265      *item = *first2;
266      return regexp__(first1+1, last1, first2+1, last2, item+1);
267    }
268    if (*first1 == '[') {
269      if (first2==last2)
270        return false;
271      bool found = false;
272      while (*first1 != ']') {
273        if (*first1 == *first2) {
274          found = true;
275          *item = *first2;
276        }
277        ++first1;
278        assert(first1!=last1);
279      }
280      return regexp__(first1+1, last1, first2+1, last2, item+1);
281    }
282
283    if (first2==last2)
284      return false;
285    if (*first1 != *first2)
286      return false;
287    return regexp__(first1+1, last1, first2+1, last2, item);
288  }
289
290
291  void rename(const std::string& from, const std::string to)
292  {
293    int code = ::rename(from.c_str(), to.c_str());
294    if (code){
295      std::stringstream ss;
296      ss << "rename(" << from << ", " << to << "): failed\n" << strerror(errno);
297      throw std::runtime_error(ss.str());
298    }
299  }
300
301
302  void replace(std::string& str, std::string old_str, std::string new_str)
303  {
304    std::string::iterator iter(str.begin());
305    while ((iter=search(iter, str.end(), old_str)) != str.end()) {
306      size_t i = iter-str.begin();
307      str = std::string(str.begin(), iter) + new_str + 
308        std::string(iter+old_str.size(), str.end());
309      // pointing to char after substr we just inserted
310      iter = str.begin() + (i+new_str.size()); 
311    }
312  }
313
314
315  void touch(std::string str)
316  {
317    if (!node_exist(str)) {
318      std::ofstream os(str.c_str());
319      os.close();
320    }
321  }
322
323  time_t str2time(const std::string& str)
324  {
325    //  str in format 2006-09-09T10:55:52.132733Z
326    std::stringstream sstream(str);
327    time_t rawtime;
328    struct tm * timeinfo;
329    time ( &rawtime );
330    timeinfo =  gmtime ( &rawtime );
331
332    unsigned int year, month, day, hour, minute, second;
333    std::string tmp;
334    getline(sstream,tmp,'-');
335    year=atoi(tmp.c_str());
336    timeinfo->tm_year = year - 1900;
337
338    getline(sstream,tmp,'-');
339    month=atoi(tmp.c_str());
340    timeinfo->tm_mon = month - 1;
341
342    getline(sstream,tmp,'T');
343    day=atoi(tmp.c_str());
344    timeinfo->tm_mday = day;
345
346    getline(sstream,tmp,':');
347    hour=atoi(tmp.c_str());
348    timeinfo->tm_hour = hour;
349
350    getline(sstream,tmp,':');
351    minute=atoi(tmp.c_str());
352    timeinfo->tm_min = minute;
353
354    getline(sstream,tmp,'.');
355    second=atoi(tmp.c_str());
356    timeinfo->tm_sec = second;
357
358    return mktime(timeinfo);
359  }
360
361
362  std::string match(std::string::const_iterator& first,
363                    const std::string::const_iterator& last,
364                    std::string str)
365  {
366    if (match_begin(first, last, str)){
367      first+=str.size();
368      return str;
369    }
370    return std::string();
371  }
372
373}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.