Added tokenize() and file_exists() methods

svn path=/trunk/; revision=1090
This commit is contained in:
Curtis Gedak 2009-03-12 19:37:12 +00:00
parent e7f4081f26
commit 2390671112
3 changed files with 64 additions and 1 deletions

View File

@ -1,3 +1,8 @@
2009-03-12 Curtis Gedak <gedakc@gmail.com>
* include/Utils.h,
src/Utils.cc: Added tokenize() and file_exists() methods.
2009-02-27 Curtis Gedak <gedakc@gmail.com> 2009-02-27 Curtis Gedak <gedakc@gmail.com>
* src/GParted_Core.cc: Enabled type checking (const instead of #define) * src/GParted_Core.cc: Enabled type checking (const instead of #define)

View File

@ -31,6 +31,7 @@
#include <iostream> #include <iostream>
#include <ctime> #include <ctime>
#include <vector>
namespace GParted namespace GParted
{ {
@ -150,7 +151,11 @@ public:
static Glib::ustring trim( const Glib::ustring & src, const Glib::ustring & c = " \t\r\n" ) ; static Glib::ustring trim( const Glib::ustring & src, const Glib::ustring & c = " \t\r\n" ) ;
static Glib::ustring cleanup_cursor( const Glib::ustring & text ) ; static Glib::ustring cleanup_cursor( const Glib::ustring & text ) ;
static Glib::ustring get_lang() ; static Glib::ustring get_lang() ;
static void tokenize( const Glib::ustring& str,
std::vector<Glib::ustring>& tokens,
const Glib::ustring& delimiters ) ;
static bool file_exists( const char* filename ) ;
static bool file_exists( const Glib::ustring& filename ) ;
}; };

View File

@ -21,6 +21,8 @@
#include <iomanip> #include <iomanip>
#include <regex.h> #include <regex.h>
#include <locale.h> #include <locale.h>
#include <sys/stat.h> //Used in file_exists() method
namespace GParted namespace GParted
{ {
@ -418,5 +420,56 @@ Glib::ustring Utils::get_lang()
return lang ; return lang ;
} }
//The tokenize method copied and adapted from:
// http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-7.html
void Utils::tokenize( const Glib::ustring& str,
std::vector<Glib::ustring>& tokens,
const Glib::ustring& delimiters = " " )
{
// Skip delimiters at beginning.
Glib::ustring::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
Glib::ustring::size_type pos = str.find_first_of(delimiters, lastPos);
while (Glib::ustring::npos != pos || Glib::ustring::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
//The file_exists method copied and adapted from:
// http://wiki.forum.nokia.com/index.php/CS001101_-_Checking_if_a_file_exists_in_C_and_C%2B%2B
bool Utils::file_exists( const char* filename )
{
struct stat info ;
int ret = -1 ;
//get the file attributes
ret = stat(filename, &info) ;
if(ret == 0)
{
//stat() is able to get the file attributes,
//so the file obviously exists
return true ;
}
else
{
//stat() is not able to get the file attributes,
//so the file obviously does not exist or
//more capabilities is required
return false ;
}
}
bool Utils::file_exists( const Glib::ustring& filename )
{
return file_exists( filename .c_str() ) ;
}
} //GParted.. } //GParted..