diff mbox

[Branch,~jesse-barker/libmatrix/trunk] Rev 38: Util: Update dependencies on util.h (shader-source). Make Util::split() support

Message ID 20120502213914.17063.81695.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Jesse Barker May 2, 2012, 9:39 p.m. UTC
------------------------------------------------------------
revno: 38
committer: Jesse Barker <jesse.barker@linaro.org>
branch nick: trunk
timestamp: Wed 2012-05-02 14:36:04 -0700
message:
  Util: Update dependencies on util.h (shader-source).  Make Util::split() support
  both explicit and "fuzzy" delimiters when parsing a string.  Add API
  documentation for all of Util.
modified:
  Makefile
  util.cc
  util.h


--
lp:libmatrix
https://code.launchpad.net/~jesse-barker/libmatrix/trunk

You are subscribed to branch lp:libmatrix.
To unsubscribe from this branch go to https://code.launchpad.net/~jesse-barker/libmatrix/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'Makefile'
--- Makefile	2012-01-23 19:18:34 +0000
+++ Makefile	2012-05-02 21:36:04 +0000
@@ -21,7 +21,7 @@ 
 program.o: program.cc program.h mat.h vec.h
 log.o: log.cc log.h
 util.o: util.cc util.h
-shader-source.o: shader-source.cc shader-source.h mat.h vec.h
+shader-source.o: shader-source.cc shader-source.h mat.h vec.h util.h
 libmatrix.a : mat.o stack.h program.o log.o util.o shader-source.o
 	$(AR) -r $@  $(LIBOBJS)
 

=== modified file 'util.cc'
--- util.cc	2012-05-01 22:58:28 +0000
+++ util.cc	2012-05-02 21:36:04 +0000
@@ -26,14 +26,26 @@ 
 using std::vector;
 
 void
-Util::split(const string& src, char delim, vector<string>& elementVec)
+Util::split(const string& src, char delim, vector<string>& elementVec, bool fuzzy)
 {
+    // Trivial rejection
     if (src.empty())
     {
         return;
     }
-    // Initialize our delimiter string based upon the caller's plus a space
-    // to allow for more flexibility.
+
+    // Simple case: we want to enforce the value of 'delim' strictly 
+    if (!fuzzy)
+    {
+        std::stringstream ss(src);
+        string item;
+        while(std::getline(ss, item, delim))
+            elementVec.push_back(item);
+        return;
+    }
+
+    // Fuzzy case: Initialize our delimiter string based upon the caller's plus
+    // a space to allow for more flexibility.
     string delimiter(" ");
     delimiter += delim;
     // Starting index into the string of the first token (by definition, if

=== modified file 'util.h'
--- util.h	2012-05-01 22:58:28 +0000
+++ util.h	2012-05-02 21:36:04 +0000
@@ -25,25 +25,52 @@ 
 
 struct Util {
     /**
-     * split() - Splits a string into elements separated by a delimiter
-     *
-     * @s:     the string to split
-     * @delim: the delimiter to use
-     * @elems: the string vector to populate
-     *
-     * Splits @s into a vector of string elements, delimited by @delim, that
-     * get returned in @elems.  The handling of @delim allows for spaces on
-     * either side of the delimiter or even multiple consecutive delimiters
-     * with no elements between.  As long as @s is non-empty, there will be
-     * at least one element in @elems.
+     * split() - Splits a string into elements using a provided delimiter
+     *
+     * @s:          the string to split
+     * @delim:      the delimiter to use
+     * @elems:      the string vector to populate
+     * @fuzzy:      (optional) enable/disable strict handling of @delim
+     *
+     * Using @delim to determine field boundaries, splits @s into separate
+     * string elements.  These elements are returned in the string vector
+     * @elems.  If @fuzzy is true, then the handling of @delim allows for
+     * spaces and multiple consecutive occurences of @delim in determining
+     * field boundaries.  As long as @s is non-empty, there will be at least
+     * one element in @elems.
      */
-    static void split(const std::string &s, char delim, std::vector<std::string> &elems);
+    static void split(const std::string &s, char delim, std::vector<std::string> &elems, bool fuzzy = false);
     /**
      * get_timestamp_us() - Returns the current time in microseconds
      */
     static uint64_t get_timestamp_us();
+    /**
+     * get_resource() - Gets an input filestream for a given file.
+     *
+     * @path:       the path to the file
+     *
+     * Returns a pointer to an input stream, which must be deleted when no
+     * longer in use.
+     */
     static std::istream *get_resource(const std::string &path);
+    /**
+     * list_files() - Get a list of the files in a given directory.
+     *
+     * @dirName:    the directory path to be listed.
+     * @fileVec:    the string vector to populate.
+     *
+     * Obtains a list of the files in @dirName, and returns them in the string
+     * vector @fileVec.
+     */
     static void list_files(const std::string& dirName, std::vector<std::string>& fileVec);
+    /**
+     * dispose_pointer_vector() - cleans up a vector of pointers
+     *
+     * @vec:        vector of pointers to objects or plain-old-data
+     *
+     * Iterates across @vec and deletes the data pointed to by each of the
+     * elements.  Clears the vector, resetting it for reuse.
+     */
     template <class T> static void dispose_pointer_vector(std::vector<T*> &vec)
     {
         for (typename std::vector<T*>::const_iterator iter = vec.begin();
@@ -55,6 +82,11 @@ 
 
         vec.clear();
     }
+    /**
+     * toString() - Converts a string to a plain-old-data type.
+     *
+     * @asString:   a string representation of plain-old-data.
+     */
     template<typename T>
     static T
     fromString(const std::string& asString)
@@ -64,7 +96,11 @@ 
         ss >> retVal;
         return retVal;
     }
-
+    /**
+     * toString() - Converts a plain-old-data type to a string.
+     *
+     * @t:      a simple value to be converted to a string
+     */
     template<typename T>
     static std::string
     toString(const T t)
@@ -73,6 +109,13 @@ 
         ss << t;
         return ss.str();
     }
+    /**
+     * appname_from_path() - get the name of an executable from an absolute path
+     *
+     * @path:   absolute path of the running application (argv[0])
+     *
+     * Returns the last portion of @path (everything after the final '/').
+     */
     static std::string
     appname_from_path(const std::string& path);