1 | // $Id: utility.cc 430 2007-07-06 23:33:11Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of svndigest, http://trac.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "utility.h" |
---|
26 | |
---|
27 | #include <cstdlib> |
---|
28 | #include <fstream> |
---|
29 | #include <sstream> |
---|
30 | #include <stdexcept> |
---|
31 | #include <string> |
---|
32 | #include <sys/param.h> |
---|
33 | #include <unistd.h> |
---|
34 | |
---|
35 | #include <iostream> |
---|
36 | |
---|
37 | namespace theplu{ |
---|
38 | namespace svndigest{ |
---|
39 | |
---|
40 | int access_rights(const std::string& path, const std::string& bits) |
---|
41 | { |
---|
42 | if (access(path.c_str(),F_OK)) { |
---|
43 | throw std::runtime_error(std::string("access_rights: ") + path + |
---|
44 | "' does not exist."); |
---|
45 | } |
---|
46 | int mode=0; |
---|
47 | for (u_int i=0; i<bits.length(); ++i) |
---|
48 | switch (bits[i]) { |
---|
49 | case 'r': |
---|
50 | mode|=R_OK; |
---|
51 | break; |
---|
52 | case 'w': |
---|
53 | mode|=W_OK; |
---|
54 | break; |
---|
55 | case 'x': |
---|
56 | mode|=X_OK; |
---|
57 | break; |
---|
58 | } |
---|
59 | return access(path.c_str(),mode); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | std::string file_name(const std::string& full_path) |
---|
64 | { |
---|
65 | std::stringstream ss(full_path); |
---|
66 | std::string name; |
---|
67 | while (getline(ss,name,'/')) {} |
---|
68 | return name; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | std::string getenv(const std::string& var) |
---|
73 | { |
---|
74 | char* buffer=std::getenv(var.c_str()); |
---|
75 | if (!buffer) |
---|
76 | throw std::runtime_error("Environment variable "+var+" is not set"); |
---|
77 | return std::string(buffer); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | std::string hex(int x, u_int width) |
---|
82 | { |
---|
83 | std::stringstream ss; |
---|
84 | ss << std::hex << x; |
---|
85 | if (!width) |
---|
86 | return ss.str(); |
---|
87 | if (ss.str().size()<width) |
---|
88 | return std::string(width-ss.str().size(), '0') + ss.str(); |
---|
89 | return ss.str().substr(0, width); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | std::string htrim(std::string str) |
---|
94 | { |
---|
95 | size_t length=str.size(); |
---|
96 | while(length && isspace(str[length-1])) |
---|
97 | --length; |
---|
98 | return str.substr(0,length); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | bool is_int(std::string s) |
---|
103 | { |
---|
104 | std::stringstream ss(s); |
---|
105 | int a; |
---|
106 | ss >> a; |
---|
107 | if(ss.fail()) |
---|
108 | return false; |
---|
109 | // Check that nothing is left on stream |
---|
110 | std::string b; |
---|
111 | ss >> b; |
---|
112 | return b.empty(); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | std::string ltrim(std::string str) |
---|
117 | { |
---|
118 | size_t i = 0; |
---|
119 | while(i<str.size() && isspace(str[i])) |
---|
120 | ++i; |
---|
121 | return str.substr(i); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | int percent(int a, int b) |
---|
126 | { |
---|
127 | if (b) |
---|
128 | return (100*a)/b; |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | bool node_exist(const std::string& path) |
---|
134 | { |
---|
135 | struct stat buf; |
---|
136 | return !stat(path.c_str(),&buf); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | std::string pwd(void) |
---|
141 | { |
---|
142 | char buffer[MAXPATHLEN]; |
---|
143 | if (!getcwd(buffer, MAXPATHLEN)) |
---|
144 | throw std::runtime_error("Failed to get current working directory"); |
---|
145 | return std::string(buffer); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | void touch(std::string str) |
---|
150 | { |
---|
151 | if (!node_exist(str)) { |
---|
152 | std::ofstream os(str.c_str()); |
---|
153 | os.close(); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | time_t str2time(const std::string& str) |
---|
158 | { |
---|
159 | // str in format 2006-09-09T10:55:52.132733Z |
---|
160 | std::stringstream sstream(str); |
---|
161 | time_t rawtime; |
---|
162 | struct tm * timeinfo; |
---|
163 | time ( &rawtime ); |
---|
164 | timeinfo = gmtime ( &rawtime ); |
---|
165 | |
---|
166 | u_int year, month, day, hour, minute, second; |
---|
167 | std::string tmp; |
---|
168 | getline(sstream,tmp,'-'); |
---|
169 | year=atoi(tmp.c_str()); |
---|
170 | timeinfo->tm_year = year - 1900; |
---|
171 | |
---|
172 | getline(sstream,tmp,'-'); |
---|
173 | month=atoi(tmp.c_str()); |
---|
174 | timeinfo->tm_mon = month - 1; |
---|
175 | |
---|
176 | getline(sstream,tmp,'T'); |
---|
177 | day=atoi(tmp.c_str()); |
---|
178 | timeinfo->tm_mday = day; |
---|
179 | |
---|
180 | getline(sstream,tmp,':'); |
---|
181 | hour=atoi(tmp.c_str()); |
---|
182 | timeinfo->tm_hour = hour; |
---|
183 | |
---|
184 | getline(sstream,tmp,':'); |
---|
185 | minute=atoi(tmp.c_str()); |
---|
186 | timeinfo->tm_min = minute; |
---|
187 | |
---|
188 | getline(sstream,tmp,'.'); |
---|
189 | second=atoi(tmp.c_str()); |
---|
190 | timeinfo->tm_sec = second; |
---|
191 | |
---|
192 | return mktime(timeinfo); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | std::string match(std::string::const_iterator& first, |
---|
197 | const std::string::const_iterator& last, |
---|
198 | std::string str) |
---|
199 | { |
---|
200 | if (match_begin(first, last, str)){ |
---|
201 | first+=str.size(); |
---|
202 | return str; |
---|
203 | } |
---|
204 | return std::string(); |
---|
205 | } |
---|
206 | |
---|
207 | notChar::notChar(char c) |
---|
208 | : char_(c) |
---|
209 | {} |
---|
210 | |
---|
211 | |
---|
212 | not2Char::not2Char(char c1, char c2) |
---|
213 | : char1_(c1), char2_(c2) |
---|
214 | {} |
---|
215 | |
---|
216 | |
---|
217 | not2Str::not2Str(std::string s1, std::string s2) |
---|
218 | : str1_(s1), str2_(s2) |
---|
219 | {} |
---|
220 | |
---|
221 | }} // end of namespace svndigest and namespace theplu |
---|