utility.cpp
Go to the documentation of this file.
/******************************************************
* Presage, an extensible predictive text entry system
* ---------------------------------------------------
*
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********(*)*/
#include "utility.h"
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
char* Utility::strtolower(char* str)
{
for(int i = 0; str[i] != '\0'; i++)
str[i] = tolower (str[i]);
return str;
}
std::string& Utility::strtolower(std::string& str)
{
for(std::string::iterator i = str.begin();
i != str.end();
i++ )
*i = tolower( *i );
return str;
}
std::string Utility::strtolower(const std::string& str)
{
std::string lowstr = str;
for(std::string::iterator i = lowstr.begin();
i != lowstr.end();
i++ )
*i = tolower( *i );
return lowstr;
}
std::string Utility::strtoupper(const std::string& str)
{
std::string upstr = str;
for(std::string::iterator i = upstr.begin();
i != upstr.end();
i++ )
*i = toupper( *i );
return upstr;
}
bool Utility::isTrueFalse(const char* str)
{
// TODO: manage yes/no with gettext?
return (isTrue (str) || isFalse (str));
}
bool Utility::isTrue(const char* str)
{
bool result = false;
char* workingStr = new char[strlen (str) + 1];
strcpy (workingStr, str);
std::string lowstr = strtolower (workingStr);
if( lowstr == "true" ||
lowstr == "1" )
result = true;
delete[] workingStr;
return result;
}
bool Utility::isFalse(const char* str)
{
bool result = false;
char* workingStr = new char[strlen(str) + 1];
strcpy (workingStr, str);
std::string lowstr = strtolower (workingStr);
if( lowstr == "false" ||
lowstr == "0" )
result = true;
delete[] workingStr;
return result;
}
bool Utility::isTrue(const std::string& str)
{
return isTrue( str.c_str() );
}
bool Utility::isFalse(const std::string& str)
{
return isFalse( str.c_str() );
}
bool Utility::isTrueFalse( const std::string& str )
{
return isTrueFalse( str.c_str() );
}
bool Utility::isYesNo(const char* str)
{
return (isYes (str) || isNo (str));
}
bool Utility::isYes( const char* str )
{
bool result = false;
char* workingStr = new char[strlen(str) + 1];
strcpy (workingStr, str);
std::string lowstr = strtolower (workingStr);
if( lowstr == "yes" ||
lowstr == "yeah" ||
lowstr == "ye" ||
lowstr == "true")
result = true;
delete[] workingStr;
return result;
}
bool Utility::isNo( const char* str )
{
bool result = false;
char* workingStr = new char[strlen(str) + 1];
strcpy (workingStr, str);
std::string lowstr = strtolower (workingStr);
if( lowstr == "no" ||
lowstr == "nope" ||
lowstr == "nah" ||
lowstr == "nay" ||
lowstr == "false")
result = true;
delete[] workingStr;
return result;
}
bool Utility::isYesNo(const std::string& str)
{
return isYesNo (str.c_str());
}
bool Utility::isYes( const std::string& str )
{
return isYes (str.c_str());
}
bool Utility::isNo(const std::string& str)
{
return isNo (str.c_str());
}
double Utility::toDouble(const std::string str)
{
return atof(str.c_str());
}
int Utility::toInt(const std::string str)
{
return atoi(str.c_str());
}
std::string Utility::dirname (const std::string& filepath)
{
std::string result;
std::string::size_type pos = filepath.find_last_of ("\\/");
if (std::string::npos != pos) {
result = filepath.substr (0, pos);
}
return result;
}
std::string Utility::filename (const std::string& filepath)
{
std::string result;
std::string::size_type pos = filepath.find_last_of ("\\/");
if (std::string::npos != pos) {
result = filepath.substr (pos + 1);
}
return result;
}
bool Utility::is_directory_usable (const std::string& dir)
{
DIR *dp;
#ifdef HAVE_DIRENT_H
dp = opendir (dir.c_str());
if (dp != NULL)
{
closedir (dp);
return true;
}
else
{
return false;
}
#else
return true;
#endif
}
void Utility::create_directory (const std::string& dir)
{
#ifdef HAVE_SYS_STAT_H
mkdir (dir.c_str()
#ifndef _WIN32
, S_IRWXU
#endif
);
#endif
}
Utility::create_directory
static void create_directory(const std::string &dir)
Definition: utility.cpp:329
Utility::strtoupper
static std::string strtoupper(const std::string &)
Definition: utility.cpp:81
Utility::filename
static std::string filename(const std::string &)
Definition: utility.cpp:290
Utility::is_directory_usable
static bool is_directory_usable(const std::string &dir)
Definition: utility.cpp:306
Utility::toDouble
static double toDouble(const std::string)
Definition: utility.cpp:257
utility.h
Utility::isYesNo
static bool isYesNo(const char *)
Definition: utility.cpp:175
Utility::isYes
static bool isYes(const char *)
Definition: utility.cpp:184
Utility::isTrue
static bool isTrue(const char *)
Definition: utility.cpp:106
Utility::dirname
static std::string dirname(const std::string &)
Definition: utility.cpp:274
Utility::isFalse
static bool isFalse(const char *)
Definition: utility.cpp:127
Utility::isTrueFalse
static bool isTrueFalse(const char *)
Definition: utility.cpp:96
Utility::toInt
static int toInt(const std::string)
Definition: utility.cpp:265
Utility::strtolower
static char * strtolower(char *)
Definition: utility.cpp:41
Utility::isNo
static bool isNo(const char *)
Definition: utility.cpp:207