Index: branches/std/src/lib/util/substring.cc
===================================================================
--- branches/std/src/lib/util/substring.cc	(revision 7202)
+++ branches/std/src/lib/util/substring.cc	(revision 7211)
@@ -24,62 +24,14 @@
 #include "substring.h"
 
-#include "debug.h"
 #include <string.h>
-#include <assert.h>
-
-SubString::SubString( const char* string, char splitter)
-{
-  this->splittersCount = 0;
-  if (string == NULL)
-  {
-    this->strings = NULL;
-    this->offsets = NULL;
-    return;
-  }
-
-  for( int i = 0; i < strlen(string); i++)
-    if( string[i] == splitter)
-      this->splittersCount++;
-
-  this->splittersCount += 1;
-
-  this->strings = new char*[this->splittersCount];
-  this->offsets = new unsigned int[this->splittersCount];
-  assert (this->strings != NULL && this->offsets != NULL);
-
-  int i = 0;
-  int l = 0;
-
-  if( this->splittersCount > 1)
-  {
-    const char* offset = string;
-    const char* end = strchr( string, splitter);
-    while( end != NULL)
-    {
-      assert( i < this->splittersCount);
-      l = end - offset;
-      this->strings[i] = new char[l + 1];
-      assert( strings[i] != NULL);
-      strncpy( strings[i], offset, l);
-      strings[i][l] = '\0';
-      this->offsets[i] = offset - string;
-      i++;
-      end++;
-      offset = end;
-      end = strchr( offset, splitter);
-    }
-
-    l = strlen( offset);
-    strings[i] = new char[l + 1];
-    strncpy( strings[i], offset, l);
-    strings[i][l] = '\0';
-    this->offsets[i] = offset - string;
-  }
-  else
-  {
-    this->strings[0] = new char[strlen(string)+1];
-    strcpy(this->strings[0], string);
-    this->offsets[0] = 0;
-  }
+#include <cassert>
+
+SubString::SubString(const std::string& string, char splitter)
+{
+  char split[2];
+  split[0] = splitter;
+  split[1] = '\0';
+  SubString::splitLine(this->strings, this->offsets,
+                       string, split);
 }
 
@@ -91,137 +43,150 @@
  *
  */
-SubString::SubString(const char* string, bool whiteSpaces)
-{
-  this->splittersCount = 0;
-  if (string == NULL || whiteSpaces == false)
+SubString::SubString(const std::string& string, bool whiteSpaces)
+{
+  SubString::splitLine(this->strings, this->offsets,
+                      string);
+}
+
+SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
+{
+  SubString::splitLine(this->strings, this->offsets,
+                       string, splitters, escapeChar, safemode_char);
+}
+
+
+/**
+ * splits line into tokens and stores them in ret. Supports delimiters, escape characters,
+ * ignores special  characters between safemode_char and between comment_char and linend '\n'.
+ *
+ * @returns SPLIT_LINE_STATE the parser was in when returning
+ */
+SPLIT_LINE_STATE SubString::splitLine(std::vector<std::string>& ret, std::vector<unsigned int>& offsets,
+                                      const std::string& line, const std::string& delimiters,
+                                      char escape_char, char safemode_char, char comment_char,
+                                      SPLIT_LINE_STATE start_state)
+{
+  SPLIT_LINE_STATE state = start_state;
+  unsigned int i = 0;
+  std::string token;
+
+  if(start_state != SL_NORMAL && ret.size() > 0)
   {
-    this->strings = NULL;
-    this->offsets = NULL;
-    return;
+    token = ret[ret.size()-1];
+    ret.pop_back();
   }
 
-  // chop the input to the beginning of something usefull
-  if (strlen(string) > 0)
-    string = string + strspn(string, " \t\n");
-
-  // count the Splitters
-  bool lastWasWhiteSpace = false;
-  for(unsigned int i = 0; i < strlen(string); i++)
-    if( string[i] == ' ' || string[i] == '\t' || string[i] == '\n' )
-      lastWasWhiteSpace = true;
-    else
+  while(i < line.size())
+  {
+    switch(state)
     {
-      if (lastWasWhiteSpace)
-        this->splittersCount ++;
-      lastWasWhiteSpace = false;
+    case SL_NORMAL:
+      if(line[i] == escape_char)
+      {
+        state = SL_ESCAPE;
+      }
+      else if(line[i] == safemode_char)
+      {
+        state = SL_SAFEMODE;
+      }
+      else if(line[i] == comment_char)
+      {
+        /// FINISH
+        if(token.size() > 0)
+        {
+          ret.push_back(token);
+          offsets.push_back(i);
+          token.clear();
+        }
+        token += line[i];       // EAT
+        state = SL_COMMENT;
+      }
+      else if(delimiters.find(line[i]) != std::string::npos)
+      {
+        // line[i] is a delimiter
+        /// FINISH
+        if(token.size() > 0)
+        {
+          ret.push_back(token);
+          offsets.push_back(i);
+          token.clear();
+        }
+      }
+      else
+      {
+        token += line[i];       // EAT
+      }
+      break;
+    case SL_ESCAPE:
+      if(line[i] == 'n') token += '\n';
+      else if(line[i] == 't') token += '\t';
+      else if(line[i] == 'v') token += '\v';
+      else if(line[i] == 'b') token += '\b';
+      else if(line[i] == 'r') token += '\r';
+      else if(line[i] == 'f') token += '\f';
+      else if(line[i] == 'a') token += '\a';
+      else if(line[i] == '?') token += '\?';
+      else token += line[i];  // EAT
+      state = SL_NORMAL;
+      break;
+    case SL_SAFEMODE:
+      if(line[i] == safemode_char)
+      {
+        state = SL_NORMAL;
+      }
+      else if(line[i] == escape_char)
+      {
+        state = SL_SAFEESCAPE;
+      }
+      else
+      {
+        token += line[i];       // EAT
+      }
+      break;
+    case SL_SAFEESCAPE:
+      if(line[i] == 'n') token += '\n';
+      else if(line[i] == 't') token += '\t';
+      else if(line[i] == 'v') token += '\v';
+      else if(line[i] == 'b') token += '\b';
+      else if(line[i] == 'r') token += '\r';
+      else if(line[i] == 'f') token += '\f';
+      else if(line[i] == 'a') token += '\a';
+      else if(line[i] == '?') token += '\?';
+      else token += line[i];  // EAT
+      state = SL_SAFEMODE;
+      break;
+    case SL_COMMENT:
+      if(line[i] == '\n')
+      {
+        /// FINISH
+        if(token.size() > 0)
+        {
+          ret.push_back(token);
+          offsets.push_back(i);
+          token.clear();
+        }
+        state = SL_NORMAL;
+      }
+      else
+      {
+        token += line[i];       // EAT
+      }
+      break;
+    default:
+      // nothing
+      break;
     }
-  this->splittersCount += 1;
-
-  // allocate memory
-  this->strings = new char*[this->splittersCount];
-  this->offsets = new unsigned int[this->splittersCount];
-  assert (this->strings != NULL && this->offsets != NULL);
-
-
-  // split the String into substrings
-  int l = 0;
-  unsigned int i = 0;
-  if( this->splittersCount > 1)
+    i++;
+  }
+
+  /// FINISH
+  if(token.size() > 0)
   {
-    const char* offset = string;
-    const char* end = offset + strcspn(offset, " \t\n");
-    for (i = 0; i < this->splittersCount; i++)
-    {
-      assert( i < this->splittersCount);
-      l = end - offset;
-      this->strings[i] = new char[l + 1];
-      assert( strings[i] != NULL);
-      strncpy( strings[i], offset, l);
-      strings[i][l] = '\0';
-      this->offsets[i] = offset - string;
-      end += strspn(end, " \t\n");
-      offset = end;
-      end = offset + strcspn(offset, " \t\n");
-    }
+    ret.push_back(token);
+    offsets.push_back(i);
+    token.clear();
   }
-  else
-  {
-    unsigned int length = strcspn(string, " \t\n");
-    this->strings[0] = new char[length+1];
-    strncpy(this->strings[0], string, length);
-    this->strings[0][length] = '\0';
-    offsets[0] = 0;
-  }
-}
-
-SubString::SubString(const char* string, const char* splitters, char escapeChar)
-{
-  this->splittersCount = 0;
-  if (string == NULL || splitters == NULL)
-  {
-    this->strings = NULL;
-    this->offsets = NULL;
-    return;
-  }
-
-  // chop the input to the beginning of something usefull
-  if (strlen(string) > 0)
-    string = string + strspn(string, splitters);
-
-  // count the Splitters
-  bool lastWasSplitter = false;
-  for(unsigned int i = 0; i < strlen(string); i++)
-  {
-
-    if( strchr(splitters, string[i] ))
-      lastWasSplitter = true;
-    else
-    {
-      if (lastWasSplitter)
-      {
-        this->splittersCount ++;
-        lastWasSplitter = false;
-      }
-    }
-  }
-  this->splittersCount += 1;
-
-  // allocate memory
-  this->strings = new char*[this->splittersCount];
-  this->offsets = new unsigned int[this->splittersCount];
-  assert (this->strings != NULL && this->offsets != NULL);
-
-
-  // split the String into substrings
-  int l = 0;
-  unsigned int i = 0;
-  if( this->splittersCount > 1)
-  {
-    const char* offset = string;
-    const char* end = offset + strcspn(offset, splitters);
-    for (i = 0; i < this->splittersCount; i++)
-    {
-      assert( i < this->splittersCount);
-      l = end - offset;
-      this->strings[i] = new char[l + 1];
-      assert( strings[i] != NULL);
-      strncpy( strings[i], offset, l);
-      strings[i][l] = '\0';
-      this->offsets[i] = offset - string;
-      end += strspn(end, splitters);
-      offset = end;
-      end = offset + strcspn(offset, splitters);
-    }
-  }
-  else
-  {
-    unsigned int length = strcspn(string, splitters);
-    this->strings[0] = new char[length+1];
-    strncpy(this->strings[0], string, length);
-    this->strings[0][length] = '\0';
-    offsets[0] = 0;
-  }
-}
-
+  return(state);
+}
 
 /**
@@ -229,26 +194,5 @@
 */
 SubString::~SubString()
-{
-  if (this->strings)
-  {
-    for(unsigned int i = 0; i < this->splittersCount; i++)
-      delete[] this->strings[i];
-    delete[] this->strings;
-  }
-  delete[] this->offsets;
-}
-
-/**
- *  get a particular substring
- * @param i the ID of the substring to return
- * @returns the designated substring or NULL if an invalid ID was given
-*/
-const char* SubString::getString(unsigned int i)
-{
-  if( i < this->splittersCount && i >= 0)
-    return this->strings[i];
-  else
-    return NULL;
-}
+{ }
 
 /**
@@ -259,5 +203,5 @@
 unsigned int SubString::getOffset(unsigned int i)
 {
-  if( i < this->splittersCount && i >= 0)
+  if( i < this->offsets.size() && i >= 0)
     return this->offsets[i];
   else
@@ -270,8 +214,7 @@
 void SubString::debug() const
 {
-  PRINT(0)("Substring-information::count=%d ::", this->splittersCount);
-  if (this->strings != NULL)
-    for (unsigned int i = 0; i < this->splittersCount; i++)
-     PRINT(0)("s%d='%s'::", i, this->strings[i]);
-  PRINT(0)("\n");
-}
+  printf("Substring-information::count=%d ::", this->strings.size());
+  for (unsigned int i = 0; i < this->strings.size(); i++)
+    printf("s%d='%s'::", i, this->strings[i].c_str());
+  printf("\n");
+}
