source: branches/0.12-stable/yat/utility/utility.cc @ 3293

Last change on this file since 3293 was 3293, checked in by Peter, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1// $Id: utility.cc 3293 2014-07-25 03:42:58Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér
5  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2010, 2012, 2013, 2014 Peter Johansson
7
8  This file is part of the yat library, http://dev.thep.lu.se/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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include <config.h>
25
26#include "utility.h"
27
28#include "Exception.h"
29#include "FileUtil.h"
30#include "stl_utility.h"
31
32#include <cassert>
33#include <fnmatch.h>
34#include <fstream>
35#include <sstream>
36#include <string>
37#include <sys/stat.h>
38
39namespace theplu {
40namespace yat {
41namespace utility {
42
43  std::string basename(const std::string& path)
44  {
45    if (path.size()==1)
46      return path;
47    size_t pos = path.find_last_of('/');
48    if (pos==std::string::npos)
49      return path;
50    if (pos==path.size()-1)
51      return basename(path.substr(0, path.size()-1));
52    return path.substr(pos+1);
53  }
54
55
56  void chdir(const std::string& dir)
57  {
58    if (::chdir(dir.c_str()) )
59      throw errno_error("chdir: " + dir + ":");
60  }
61
62
63  void chmod(const std::string& filename, mode_t mode)
64  {
65    if (::chmod(filename.c_str(), mode))
66      throw errno_error("chmod: ");
67  }
68
69
70  void copy_file(const std::string& source, const std::string& target)
71  {
72    std::ifstream is;
73    std::ofstream os;
74
75    is.open(source.c_str(), std::ios::in | std::ios::binary);
76    os.open(target.c_str(), std::ios::out | std::ios::binary);
77    os << is.rdbuf();
78    if (!os.good()) {
79      std::ostringstream ss;
80      ss << "copy_file failed writing to '" << target << "'\n";
81      throw runtime_error(ss.str());
82    }
83    if (is.bad() || is.fail()) {
84      std::ostringstream ss;
85      ss << "copy_file failed reading from '" << source << "'\n";
86      throw runtime_error(ss.str());
87    }
88    is.close();
89    os.close();
90
91    // copy permissions
92    struct stat nodestat;
93    stat(source.c_str(), &nodestat);
94    chmod(target.c_str(), nodestat.st_mode);
95  }
96
97
98  std::string dirname(const std::string& path)
99  {
100    if (path.size()==1) {
101      if (path=="/")
102        return path;
103      else
104        return ".";
105    }
106    assert(path.size()>=2);
107    size_t pos = path.find_last_of('/', path.size()-2);
108    if (pos==std::string::npos)
109      return ".";
110    if (pos==0)
111      return "/";
112    return path.substr(0,pos);
113  }
114
115
116  bool fnmatch(const std::string& pattern, const std::string& str, int flags)
117  {
118    int res = ::fnmatch(pattern.c_str(), str.c_str(), flags);
119    if (res==0)
120      return true;
121    if (res!=FNM_NOMATCH) {
122      std::stringstream ss;
123      ss << "fnmatch(" << pattern << ", " << str << ")";
124      throw runtime_error(ss.str());
125    }
126    return false;
127  }
128
129
130  bool is_double(const std::string& s)
131  {
132    return is<double>(s);
133  }
134 
135
136  bool is_equal(std::string s, std::string other)
137  {
138    std::stringstream ss(s);
139    std::string s2;
140    ss >> s2; // to trim surrounding whitespaces
141    to_lower(s2);
142    if (s2!=other)
143      return false;
144    // Check that nothing is left on stream
145    std::string s3;
146    ss >> s3;
147    return s3.empty();
148  }
149
150
151  bool is_float(const std::string& s)
152  {
153    return is<float>(s);
154  }
155
156
157  bool is_int(const std::string& s)
158  {
159    return is<int>(s);
160  }
161
162  bool is_nan(const std::string& s)
163  {
164    return is_equal(s, "nan");
165  }
166
167
168  void mkdir(const std::string& dir, mode_t mode)
169  {
170    int code = ::mkdir(dir.c_str(), mode);
171    if (code){
172      std::stringstream ss;
173      ss << "mkdir: '" << dir << "': ";
174      throw yat::utility::errno_error(ss.str());
175    }
176  }
177
178
179  void mkdir_p(const std::string& dir, mode_t mode)
180  {
181    if (FileUtil(dir).exists())
182      return;
183    mkdir_p(dirname(dir), mode);
184    mkdir(dir, mode);
185  }
186
187
188  void remove(const std::string& fn)
189  {
190    if (::remove(fn.c_str())) {
191      std::string msg("remove: ");
192      msg += fn;
193      throw errno_error(msg);
194    }
195  }
196
197
198  void rename(const std::string& from, const std::string& to)
199  {
200    if (::rename(from.c_str(), to.c_str())) {
201      std::stringstream ss;
202      ss << "rename" << from << " to " << to << ": ";
203      throw errno_error(ss.str());
204    }
205  }
206
207
208  void replace(std::string& str, std::string old_value, std::string new_value)
209  {
210    assert(old_value.size());
211    std::string result;
212    std::back_insert_iterator<std::string> out(result);
213    std::string::iterator c = str.begin();
214    while (c!=str.end()) {
215      if (str.end() - c >= static_cast<int>(old_value.size()) &&
216          std::equal(old_value.begin(), old_value.end(), c)) {
217        std::copy(new_value.begin(), new_value.end(), out);
218        c += old_value.size();
219      }
220      else {
221        *out = *c;
222        ++c;
223      }
224    }
225    str.swap(result);
226  }
227
228
229}}} // end of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.