gparted r1090 - in trunk: . include src



Author: gedakc
Date: Thu Mar 12 19:37:12 2009
New Revision: 1090
URL: http://svn.gnome.org/viewvc/gparted?rev=1090&view=rev

Log:
Added tokenize() and file_exists() methods

Modified:
   trunk/ChangeLog
   trunk/include/Utils.h
   trunk/src/Utils.cc

Modified: trunk/include/Utils.h
==============================================================================
--- trunk/include/Utils.h	(original)
+++ trunk/include/Utils.h	Thu Mar 12 19:37:12 2009
@@ -31,6 +31,7 @@
 
 #include <iostream>
 #include <ctime>
+#include <vector>
 
 namespace GParted
 {
@@ -150,7 +151,11 @@
 	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<Glib::ustring>& tokens,
+	                      const Glib::ustring& delimiters ) ;
+	static bool file_exists( const char* filename ) ;
+	static bool file_exists( const Glib::ustring& filename ) ;
 };
 
 

Modified: trunk/src/Utils.cc
==============================================================================
--- trunk/src/Utils.cc	(original)
+++ trunk/src/Utils.cc	Thu Mar 12 19:37:12 2009
@@ -21,6 +21,8 @@
 #include <iomanip>
 #include <regex.h>
 #include <locale.h>
+#include <sys/stat.h>	//Used in file_exists() method
+
 
 namespace GParted
 {
@@ -418,5 +420,56 @@
 	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..



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]