Browse files

stringlist tokenizer

  • Loading branch information...
1 parent 54b8f23 commit a3ff7719ce0a5a8597ec329457d0c90d9f7b1c56 @dyfet committed May 21, 2012
Showing with 38 additions and 6 deletions.
  1. +17 −0 corelib/memory.cpp
  2. +12 −0 inc/ucommon/memory.h
  3. +9 −6 inc/ucommon/string.h
View
17 corelib/memory.cpp
@@ -257,6 +257,23 @@ memalloc(size)
add(list);
}
+unsigned stringpager::token(const char *text, const char *list, const char *quote, const char *end)
+{
+ unsigned count = 0;
+ const char *result;
+ char *last = NULL;
+
+ if(!text || !*text)
+ return 0;
+
+ strdup_t tmp = strdup(text);
+ while(NULL != (result = String::token(tmp, &last, list, quote, end))) {
+ ++count;
+ add(result);
+ }
+ return count;
+}
+
const char *stringpager::get(unsigned index)
{
linked_pointer<member> list = root;
View
12 inc/ucommon/memory.h
@@ -386,6 +386,18 @@ class __EXPORT stringpager : protected memalloc
*/
char **list(void);
+ /**
+ * Tokenize a string and add each token to the stringpager.
+ * @param text to tokenize.
+ * @param list of characters to use as token separators.
+ * @param quote pairs of characters for quoted text or NULL if not used.
+ * @param end of line marker characters or NULL if not used.
+ * @return number of tokens parsed.
+ */
+ unsigned token(const char *text, const char *list, const char *quote = NULL, const char *end = NULL);
+
+
+
inline operator bool()
{return members > 0;}
View
15 inc/ucommon/string.h
@@ -1772,30 +1772,33 @@ inline void swap<string_t>(string_t& s1, string_t& s2)
class __EXPORT strdup_t
{
private:
- const char *data;
+ char *data;
public:
inline strdup_t()
{data = NULL;}
- inline strdup_t(const char *str)
+ inline strdup_t(char *str)
{data = str;}
inline ~strdup_t()
- {if(data) ::free((char *)data);}
+ {if(data) ::free(data);}
- inline strdup_t& operator=(const char *str)
- {if(data) ::free((char *)data); data = str; return *this;}
+ inline strdup_t& operator=(char *str)
+ {if(data) ::free(data); data = str; return *this;}
inline operator bool() const
{return data != NULL;}
inline bool operator!() const
{return data == NULL;}
- inline operator const char*() const
+ inline operator char*() const
{return data;}
+ inline const char *c_str(void) const
+ {return data;};
+
inline const char *operator*() const
{return data;}

0 comments on commit a3ff771

Please sign in to comment.