From 2390671112082d0592ed9761fc3a1dba8e446a15 Mon Sep 17 00:00:00 2001 From: Curtis Gedak Date: Thu, 12 Mar 2009 19:37:12 +0000 Subject: [PATCH] Added tokenize() and file_exists() methods svn path=/trunk/; revision=1090 --- ChangeLog | 5 +++++ include/Utils.h | 7 ++++++- src/Utils.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 667e3be2..d3b401b8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2009-03-12 Curtis Gedak + + * include/Utils.h, + src/Utils.cc: Added tokenize() and file_exists() methods. + 2009-02-27 Curtis Gedak * src/GParted_Core.cc: Enabled type checking (const instead of #define) diff --git a/include/Utils.h b/include/Utils.h index 1f2b1994..7157b43b 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -31,6 +31,7 @@ #include #include +#include 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 cleanup_cursor( const Glib::ustring & text ) ; static Glib::ustring get_lang() ; - + static void tokenize( const Glib::ustring& str, + std::vector& tokens, + const Glib::ustring& delimiters ) ; + static bool file_exists( const char* filename ) ; + static bool file_exists( const Glib::ustring& filename ) ; }; diff --git a/src/Utils.cc b/src/Utils.cc index 2c2092df..2ff921eb 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -21,6 +21,8 @@ #include #include #include +#include //Used in file_exists() method + namespace GParted { @@ -418,5 +420,56 @@ Glib::ustring Utils::get_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& 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..