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

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

add functions mkdir, mkdir_p, basename, dirname. refs #717

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: utility.cc 2892 2012-12-09 07:15:18Z 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
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 "FileUtil.h"
29#include "stl_utility.h"
30
31#include <sstream>
32#include <string>
33#include <sys/stat.h>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39  std::string basename(const std::string& path)
40  {
41    if (path.size()==1)
42      return path;
43    size_t pos = path.find_last_of('/');
44    if (pos==std::string::npos)
45      return path;
46    if (pos==path.size()-1)
47      return basename(path.substr(0, path.size()-1));
48    return path.substr(pos+1);
49  }
50
51
52  std::string dirname(const std::string& path)
53  {
54    if (path.size()==1) {
55      if (path=="/")
56        return path;
57      else
58        return ".";
59    }
60    assert(path.size()>=2);
61    size_t pos = path.find_last_of('/', path.size()-2);
62    if (pos==std::string::npos)
63      return ".";
64    if (pos==0)
65      return "/";
66    return path.substr(0,pos);
67  }
68
69
70  bool is_double(const std::string& s)
71  {
72    return is<double>(s);
73  }
74 
75
76  bool is_equal(std::string s, std::string other)
77  {
78    std::stringstream ss(s);
79    std::string s2;
80    ss >> s2; // to trim surrounding whitespaces
81    to_lower(s2);
82    if (s2!=other)
83      return false;
84    // Check that nothing is left on stream
85    std::string s3;
86    ss >> s3;
87    return s3.empty();
88  }
89
90
91  bool is_float(const std::string& s)
92  {
93    return is<float>(s);
94  }
95
96
97  bool is_int(const std::string& s)
98  {
99    return is<int>(s);
100  }
101
102  bool is_nan(const std::string& s)
103  {
104    return is_equal(s, "nan");
105  }
106
107
108  void mkdir(const std::string& dir)
109  {
110    int code = ::mkdir(dir.c_str(),0777);
111    if (code){
112      std::stringstream ss;
113      ss << "mkdir: '" << dir << "': ";
114      throw yat::utility::errno_error(ss.str());
115    }
116  }
117
118
119  void mkdir_p(const std::string& dir)
120  {
121    if (FileUtil(dir).exists())
122      return;
123    mkdir_p(dirname(dir));
124    mkdir(dir);
125  }
126
127
128}}} // end of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.