presage  0.9.2~beta
utility.cpp
Go to the documentation of this file.
1 
2 /******************************************************
3  * Presage, an extensible predictive text entry system
4  * ---------------------------------------------------
5  *
6  * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  **********(*)*/
23 
24 
25 #include "utility.h"
26 #ifdef HAVE_STRING_H
27 # include <string.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif
32 #ifdef HAVE_DIRENT_H
33 # include <dirent.h>
34 #endif
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 
42 char* Utility::strtolower(char* str)
43 {
44  for(int i = 0; str[i] != '\0'; i++)
45  str[i] = tolower (str[i]);
46 
47  return str;
48 }
49 
50 
54 std::string& Utility::strtolower(std::string& str)
55 {
56  for(std::string::iterator i = str.begin();
57  i != str.end();
58  i++ )
59  *i = tolower( *i );
60 
61  return str;
62 }
63 
67 std::string Utility::strtolower(const std::string& str)
68 {
69  std::string lowstr = str;
70  for(std::string::iterator i = lowstr.begin();
71  i != lowstr.end();
72  i++ )
73  *i = tolower( *i );
74 
75  return lowstr;
76 }
77 
78 
82 std::string Utility::strtoupper(const std::string& str)
83 {
84  std::string upstr = str;
85  for(std::string::iterator i = upstr.begin();
86  i != upstr.end();
87  i++ )
88  *i = toupper( *i );
89 
90  return upstr;
91 }
92 
93 
97 bool Utility::isTrueFalse(const char* str)
98 {
99  // TODO: manage yes/no with gettext?
100 
101  return (isTrue (str) || isFalse (str));
102 }
103 
107 bool Utility::isTrue(const char* str)
108 {
109  bool result = false;
110 
111  char* workingStr = new char[strlen (str) + 1];
112  strcpy (workingStr, str);
113 
114  std::string lowstr = strtolower (workingStr);
115  if( lowstr == "true" ||
116  lowstr == "1" )
117  result = true;
118 
119  delete[] workingStr;
120 
121  return result;
122 }
123 
124 
128 bool Utility::isFalse(const char* str)
129 {
130  bool result = false;
131 
132  char* workingStr = new char[strlen(str) + 1];
133  strcpy (workingStr, str);
134 
135  std::string lowstr = strtolower (workingStr);
136  if( lowstr == "false" ||
137  lowstr == "0" )
138  result = true;
139 
140  delete[] workingStr;
141 
142  return result;
143 }
144 
145 
149 bool Utility::isTrue(const std::string& str)
150 {
151  return isTrue( str.c_str() );
152 }
153 
154 
158 bool Utility::isFalse(const std::string& str)
159 {
160  return isFalse( str.c_str() );
161 }
162 
163 
167 bool Utility::isTrueFalse( const std::string& str )
168 {
169  return isTrueFalse( str.c_str() );
170 }
171 
172 
176 bool Utility::isYesNo(const char* str)
177 {
178  return (isYes (str) || isNo (str));
179 }
180 
181 
185 bool Utility::isYes( const char* str )
186 {
187  bool result = false;
188 
189  char* workingStr = new char[strlen(str) + 1];
190  strcpy (workingStr, str);
191 
192  std::string lowstr = strtolower (workingStr);
193  if( lowstr == "yes" ||
194  lowstr == "yeah" ||
195  lowstr == "ye" ||
196  lowstr == "true")
197  result = true;
198 
199  delete[] workingStr;
200 
201  return result;
202 }
203 
204 
208 bool Utility::isNo( const char* str )
209 {
210  bool result = false;
211 
212  char* workingStr = new char[strlen(str) + 1];
213  strcpy (workingStr, str);
214 
215  std::string lowstr = strtolower (workingStr);
216  if( lowstr == "no" ||
217  lowstr == "nope" ||
218  lowstr == "nah" ||
219  lowstr == "nay" ||
220  lowstr == "false")
221  result = true;
222 
223  delete[] workingStr;
224 
225  return result;
226 }
227 
228 
232 bool Utility::isYesNo(const std::string& str)
233 {
234  return isYesNo (str.c_str());
235 }
236 
237 
241 bool Utility::isYes( const std::string& str )
242 {
243  return isYes (str.c_str());
244 }
245 
246 
250 bool Utility::isNo(const std::string& str)
251 {
252  return isNo (str.c_str());
253 }
254 
258 double Utility::toDouble(const std::string str)
259 {
260  return atof(str.c_str());
261 }
262 
266 int Utility::toInt(const std::string str)
267 {
268  return atoi(str.c_str());
269 }
270 
271 
275 std::string Utility::dirname (const std::string& filepath)
276 {
277  std::string result;
278 
279  std::string::size_type pos = filepath.find_last_of ("\\/");
280  if (std::string::npos != pos) {
281  result = filepath.substr (0, pos);
282  }
283 
284  return result;
285 }
286 
287 
291 std::string Utility::filename (const std::string& filepath)
292 {
293  std::string result;
294 
295  std::string::size_type pos = filepath.find_last_of ("\\/");
296  if (std::string::npos != pos) {
297  result = filepath.substr (pos + 1);
298  }
299 
300  return result;
301 }
302 
303 
307 bool Utility::is_directory_usable (const std::string& dir)
308 {
309  DIR *dp;
310 #ifdef HAVE_DIRENT_H
311  dp = opendir (dir.c_str());
312  if (dp != NULL)
313  {
314  closedir (dp);
315  return true;
316  }
317  else
318  {
319  return false;
320  }
321 #else
322  return true;
323 #endif
324 }
325 
326 
330 void Utility::create_directory (const std::string& dir)
331 {
332 #ifdef HAVE_SYS_STAT_H
333  mkdir (dir.c_str()
334 #ifndef _WIN32
335  , S_IRWXU
336 #endif
337  );
338 #endif
339 }
static int toInt(const std::string)
Definition: utility.cpp:266
static std::string strtoupper(const std::string &)
Definition: utility.cpp:82
static bool isYesNo(const char *)
Definition: utility.cpp:176
static void create_directory(const std::string &dir)
Definition: utility.cpp:330
static double toDouble(const std::string)
Definition: utility.cpp:258
static char * strtolower(char *)
Definition: utility.cpp:42
static bool isFalse(const char *)
Definition: utility.cpp:128
static bool isNo(const char *)
Definition: utility.cpp:208
static bool isYes(const char *)
Definition: utility.cpp:185
static bool isTrueFalse(const char *)
Definition: utility.cpp:97
static bool isTrue(const char *)
Definition: utility.cpp:107
static std::string dirname(const std::string &)
Definition: utility.cpp:275
static std::string filename(const std::string &)
Definition: utility.cpp:291
static bool is_directory_usable(const std::string &dir)
Definition: utility.cpp:307