source: trunk/yat/utility/utility.cc @ 2943

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

merge release 0.10 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1// $Id: utility.cc 2943 2013-01-04 06:38:38Z 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 Peter Johansson
7  Copyright (C) 2012 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include <config.h>
26
27#include "utility.h"
28
29#include "Exception.h"
30#include "FileUtil.h"
31#include "stl_utility.h"
32
33#include <cassert>
34#include <fnmatch.h>
35#include <fstream>
36#include <sstream>
37#include <string>
38#include <sys/stat.h>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44  std::string basename(const std::string& path)
45  {
46    if (path.size()==1)
47      return path;
48    size_t pos = path.find_last_of('/');
49    if (pos==std::string::npos)
50      return path;
51    if (pos==path.size()-1)
52      return basename(path.substr(0, path.size()-1));
53    return path.substr(pos+1);
54  }
55
56
57  void chdir(const std::string& dir)
58  {
59    if (::chdir(dir.c_str()) )
60      throw errno_error("chdir: " + dir + ":");
61  }
62
63
64  void chmod(const std::string& filename, mode_t mode)
65  {
66    if (::chmod(filename.c_str(), mode))
67      throw errno_error("chmod: ");
68  }
69
70
71  void copy_file(const std::string& source, const std::string& target)
72  {
73    std::ifstream is;
74    std::ofstream os;
75
76    is.open(source.c_str(), std::ios::in | std::ios::binary);
77    os.open(target.c_str(), std::ios::out | std::ios::binary);
78    os << is.rdbuf();
79    if (!os.good()) {
80      std::ostringstream ss;
81      ss << "copy_file failed writing to '" << target << "'\n";
82      throw runtime_error(ss.str());
83    }
84    if (is.bad() || is.fail()) {
85      std::ostringstream ss;
86      ss << "copy_file failed reading from '" << source << "'\n";
87      throw runtime_error(ss.str());
88    }
89    is.close();
90    os.close();
91
92    // copy permissions
93    struct stat nodestat;
94    stat(source.c_str(), &nodestat);
95    chmod(target.c_str(), nodestat.st_mode);
96  }
97
98
99  std::string dirname(const std::string& path)
100  {
101    if (path.size()==1) {
102      if (path=="/")
103        return path;
104      else
105        return ".";
106    }
107    assert(path.size()>=2);
108    size_t pos = path.find_last_of('/', path.size()-2);
109    if (pos==std::string::npos)
110      return ".";
111    if (pos==0)
112      return "/";
113    return path.substr(0,pos);
114  }
115
116
117  bool fnmatch(const std::string& pattern, const std::string& str, int flags)
118  {
119    int res = ::fnmatch(pattern.c_str(), str.c_str(), flags);
120    if (res==0)
121      return true;
122    if (res!=FNM_NOMATCH) {
123      std::stringstream ss;
124      ss << "fnmatch(" << pattern << ", " << str << ")";
125      throw runtime_error(ss.str());
126    }
127    return false;
128  }
129
130
131  bool is_double(const std::string& s)
132  {
133    return is<double>(s);
134  }
135 
136
137  bool is_equal(std::string s, std::string other)
138  {
139    std::stringstream ss(s);
140    std::string s2;
141    ss >> s2; // to trim surrounding whitespaces
142    to_lower(s2);
143    if (s2!=other)
144      return false;
145    // Check that nothing is left on stream
146    std::string s3;
147    ss >> s3;
148    return s3.empty();
149  }
150
151
152  bool is_float(const std::string& s)
153  {
154    return is<float>(s);
155  }
156
157
158  bool is_int(const std::string& s)
159  {
160    return is<int>(s);
161  }
162
163  bool is_nan(const std::string& s)
164  {
165    return is_equal(s, "nan");
166  }
167
168
169  void mkdir(const std::string& dir, mode_t mode)
170  {
171    int code = ::mkdir(dir.c_str(), mode);
172    if (code){
173      std::stringstream ss;
174      ss << "mkdir: '" << dir << "': ";
175      throw yat::utility::errno_error(ss.str());
176    }
177  }
178
179
180  void mkdir_p(const std::string& dir, mode_t mode)
181  {
182    if (FileUtil(dir).exists())
183      return;
184    mkdir_p(dirname(dir), mode);
185    mkdir(dir, mode);
186  }
187
188
189}}} // end of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.