Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 10, 2008, 12:05:03 AM (16 years ago)
Author:
landauf
Message:

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

Location:
code/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/util

  • code/trunk/src/util/SubString.cc

    r1791 r2171  
    4040#include "SubString.h"
    4141
    42 /**
    43  * @brief default constructor
    44  */
    45 SubString::SubString()
    46 {}
    47 
    48 
    49 /**
    50  * @brief create a SubString from
    51  * @param string the String to Split
    52  * @param delimiter the Character at which to split string (delimiter)
    53  */
    54 SubString::SubString(const std::string& string, char delimiter)
     42namespace orxonox
    5543{
    56   this->split(string, delimiter);
    57 }
    58 
    59 
    60 /**
    61  * @brief Splits a String into multiple splitters.
    62  * @param string the String to split
    63  * @param delimiters multiple set of characters at what to split. (delimiters)
    64  * @param delimiterNeighbours neighbours of the delimiters, that will be erased only when near a delimiter.
    65  * @param emptyEntries If empty entries should be allewed or removed.
    66  * @param escapeChar The Escape Character that overrides splitters commends and so on...
    67  * @param safemode_char within these characters splitting won't happen
    68  * @param comment_char the Comment character.
    69  */
    70 SubString::SubString(const std::string& string,
    71                      const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
    72                      char escapeChar, bool removeEscapeChar, char safemode_char, bool removeSafemodeChar,
    73                      char openparenthesis_char, char closeparenthesis_char, bool removeParenthesisChars, char comment_char)
    74 {
    75   SubString::splitLine(this->strings, this->bInSafemode, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, removeEscapeChar, safemode_char, removeSafemodeChar, openparenthesis_char, closeparenthesis_char, removeParenthesisChars, comment_char);
    76 }
    77 
    78 /**
    79  * @brief creates a SubSet of a SubString.
    80  * @param subString the SubString to take a set from.
    81  * @param subSetBegin the beginning to the end
    82  */
    83 SubString::SubString(const SubString& subString, unsigned int subSetBegin)
    84 {
    85   for (unsigned int i = subSetBegin; i < subString.size(); i++)
    86   {
    87     this->strings.push_back(subString[i]);
    88     this->bInSafemode.push_back(subString.isInSafemode(i));
    89   }
    90 }
    91 
    92 
    93 /**
    94  * @brief creates a SubSet of a SubString.
    95  * @param subString the SubString to take a Set from
    96  * @param subSetBegin the beginning to the end
    97  * @param subSetEnd the end of the SubSet (max subString.size() will be checked internaly)
    98  */
    99 SubString::SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd)
    100 {
    101   for (unsigned int i = subSetBegin; i < subString.size() && i < subSetEnd; i++)
    102   {
    103     this->strings.push_back(subString[i]);
    104     this->bInSafemode.push_back(subString.isInSafemode(i));
    105   }
    106 }
    107 
    108 /**
    109  * @brief creates a Substring from a count and values set.
    110  * @param argc: the Arguments Count.
    111  * @param argv: Argument Values.
    112  */
    113 SubString::SubString(unsigned int argc, const char** argv)
    114 {
    115   for(unsigned int i = 0; i < argc; ++i)
    116   {
    117     this->strings.push_back(std::string(argv[i]));
    118     this->bInSafemode.push_back(false);
    119   }
    120 }
    121 
    122 /**
    123  * @brief removes the object from memory
    124  */
    125 SubString::~SubString()
    126 { }
    127 
    128 /** @brief An empty String */
    129 // const std::string SubString::emptyString = "";
    130 /** @brief Helper that gets you a String consisting of all White Spaces */
    131 const std::string SubString::WhiteSpaces = " \n\t";
    132 /** @brief Helper that gets you a String consisting of all WhiteSpaces and the Comma */
    133 const std::string SubString::WhiteSpacesWithComma = " \n\t,";
    134 /** An Empty SubString */
    135 const SubString SubString::NullSubString = SubString();
    136 
    137 /**
    138  * @brief stores the Value of subString in this SubString
    139  * @param subString will be copied into this String.
    140  * @returns this SubString.
    141  */
    142 SubString& SubString::operator=(const SubString& subString)
    143 {
    144   this->strings = subString.strings;
    145   this->bInSafemode = subString.bInSafemode;
    146   return *this;
    147 }
    148 
    149 
    150 /**
    151  * @brief comparator.
    152  * @param subString the SubString to compare against this one.
    153  * @returns true if the Stored Strings match
    154  */
    155 bool SubString::operator==(const SubString& subString) const
    156 {
    157   return ((this->strings == subString.strings) && (this->bInSafemode == subString.bInSafemode));
    158 }
    159 
    160 /**
    161  * @brief comparator.
    162  * @param subString the SubString to compare against this one.
    163  * @returns true if the Stored Strings match
    164  */
    165 bool SubString::compare(const SubString& subString) const
    166 {
    167   return (*this == subString);
    168 }
    169 
    170 /**
    171  * @brief comparator.
    172  * @param subString the SubString to compare against this one.
    173  * @param length how many entries to compare. (from 0 to length)
    174  * @returns true if the Stored Strings match
    175  */
    176 bool SubString::compare(const SubString& subString, unsigned int length) const
    177 {
    178   if (length > this->size() || length > subString.size())
    179     return false;
    180 
    181   for (unsigned int i = 0; i < length; i++)
    182     if ((this->strings[i] != subString.strings[i]) || (this->bInSafemode[i] != subString.bInSafemode[i]))
    183       return false;
    184   return true;
    185 }
    186 
    187 
    188 /**
    189  * @brief append operator
    190  * @param subString the String to append.
    191  * @returns a SubString where this and subString are appended.
    192  */
    193 SubString SubString::operator+(const SubString& subString) const
    194 {
    195   return SubString(*this) += subString;
    196 }
    197 
    198 
    199 /**
    200  * @brief append operator.
    201  * @param subString append subString to this SubString.
    202  * @returns this substring appended with subString
    203  */
    204 SubString& SubString::operator+=(const SubString& subString)
    205 {
    206   for (unsigned int i = 0; i < subString.size(); i++)
    207   {
    208     this->strings.push_back(subString[i]);
    209     this->bInSafemode.push_back(subString.isInSafemode(i));
    210   }
    211   return *this;
    212 }
    213 
    214 
    215 /**
    216  * @brief Split the String at
    217  * @param string where to split
    218  * @param splitter delimiter.
    219  */
    220 unsigned int SubString::split(const std::string& string, char splitter)
    221 {
    222   this->strings.clear();
    223   this->bInSafemode.clear();
    224   char split[2];
    225   split[0] = splitter;
    226   split[1] = '\0';
    227   SubString::splitLine(this->strings, this->bInSafemode, string, split);
    228   return strings.size();
    229 }
    230 
    231 
    232 /**
    233  * @brief Splits a String into multiple splitters.
    234  * @param string the String to split
    235  * @param delimiters multiple set of characters at what to split. (delimiters)
    236  * @param delimiterNeighbours: Neighbours to the Delimiters that will be erased too.
    237  * @param emptyEntries: If empty entries are added to the List of SubStrings
    238  * @param escapeChar The Escape Character that overrides splitters commends and so on...
    239  * @param safemode_char within these characters splitting won't happen
    240  * @param comment_char the Comment character.
    241  */
    242 unsigned int SubString::split(const std::string& string,
    243                               const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
    244                               char escapeChar, bool removeExcapeChar, char safemode_char, bool removeSafemodeChar,
    245                               char openparenthesis_char, char closeparenthesis_char, bool removeParenthesisChars, char comment_char)
    246 {
    247   this->strings.clear();
    248   this->bInSafemode.clear();
    249   SubString::splitLine(this->strings, this->bInSafemode, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, removeExcapeChar, safemode_char, removeSafemodeChar, openparenthesis_char, closeparenthesis_char, removeParenthesisChars, comment_char);
    250   return this->strings.size();
    251 }
    252 
    253 
    254 /**
    255  * @brief joins together all Strings of this Substring.
    256  * @param delimiter the String between the subStrings.
    257  * @returns the joined String.
    258  */
    259 std::string SubString::join(const std::string& delimiter) const
    260 {
    261   if (!this->strings.empty())
    262   {
    263     std::string retVal = this->strings[0];
    264     for (unsigned int i = 1; i < this->strings.size(); i++)
    265       retVal += delimiter + this->strings[i];
    266     return retVal;
    267   }
    268   else
    269   {
    270     static std::string empty;
    271     return empty;
    272   }
    273 }
    274 
    275 
    276 /**
    277  * @brief creates a SubSet of a SubString.
    278  * @param subSetBegin the beginning to the end
    279  * @returns the SubSet
    280  *
    281  * This function is added for your convenience, and does the same as
    282  * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
    283  */
    284 SubString SubString::subSet(unsigned int subSetBegin) const
    285 {
    286   return SubString(*this, subSetBegin);
    287 }
    288 
    289 
    290 /**
    291  * @brief creates a SubSet of a SubString.
    292  * @param subSetBegin the beginning to
    293  * @param subSetEnd the end of the SubSet to select (if bigger than subString.size() it will be downset.)
    294  * @returns the SubSet
    295  *
    296  * This function is added for your convenience, and does the same as
    297  * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
    298  */
    299 SubString SubString::subSet(unsigned int subSetBegin, unsigned int subSetEnd) const
    300 {
    301   return SubString(*this, subSetBegin, subSetEnd);
    302 }
    303 
    304 
    305 /**
    306  * @brief splits line into tokens and stores them in ret.
    307  * @param ret the Array, where the Splitted strings will be stored in
    308  * to the beginning of the current token is stored
    309  * @param line the inputLine to split
    310  * @param delimiters a String of Delimiters (here the input will be splitted)
    311  * @param delimiterNeighbours Naighbours to the Delimitter, that will be removed if they are to the left or the right of a Delimiter.
    312  * @param emptyEntries: if empty Strings are added to the List of Strings.
    313  * @param escape_char: Escape carater (escapes splitters)
    314  * @param safemode_char: the beginning of the safemode is marked with this
    315  * @param removeSafemodeChar removes the safemode_char from the beginning and the ending of a token
    316  * @param openparenthesis_char the beginning of a safemode is marked with this
    317  * @param closeparenthesis_char the ending of a safemode is marked with this
    318  * @param removeParenthesisChars removes the parenthesis from the beginning and the ending of a token
    319  * @param comment_char: the beginning of a comment is marked with this: (until the end of a Line)
    320  * @param start_state: the Initial state on how to parse the String.
    321  * @return SPLIT_LINE_STATE the parser was in when returning
    322  *
    323  * This is the Actual Splitting Algorithm from Clemens Wacha
    324  * Supports delimiters, escape characters,
    325  * ignores special  characters between safemode_char and between comment_char and linend '\n'.
    326  */
    327 SubString::SPLIT_LINE_STATE
    328 SubString::splitLine(std::vector<std::string>& ret,
    329                      std::vector<bool>& bInSafemode,
    330                      const std::string& line,
    331                      const std::string& delimiters,
    332                      const std::string& delimiterNeighbours,
    333                      bool emptyEntries,
    334                      char escape_char,
    335                      bool removeExcapeChar,
    336                      char safemode_char,
    337                      bool removeSafemodeChar,
    338                      char openparenthesis_char,
    339                      char closeparenthesis_char,
    340                      bool removeParenthesisChars,
    341                      char comment_char,
    342                      SPLIT_LINE_STATE start_state)
    343 {
    344   SPLIT_LINE_STATE state = start_state;
    345   unsigned int i = 0;
    346   unsigned int fallBackNeighbours = 0;
    347 
    348   std::string token;
    349   bool inSafemode = false;
    350 
    351   if(start_state != SL_NORMAL && ret.size() > 0)
    352   {
    353     token = ret[ret.size()-1];
    354     ret.pop_back();
    355   }
    356   if(start_state != SL_NORMAL && bInSafemode.size() > 0)
    357   {
    358     inSafemode = bInSafemode[bInSafemode.size()-1];
    359     bInSafemode.pop_back();
    360   }
    361 
    362   while(i < line.size())
    363   {
    364     switch(state)
    365     {
    366       case SL_NORMAL:
    367         if(line[i] == escape_char)
    368         {
    369           state = SL_ESCAPE;
    370           if (!removeExcapeChar)
    371             token += line[i];
    372         }
    373         else if(line[i] == safemode_char)
    374         {
    375           state = SL_SAFEMODE;
    376           inSafemode = true;
    377           if (!removeSafemodeChar)
    378             token += line[i];
    379         }
    380         else if(line[i] == openparenthesis_char)
    381         {
    382           state = SL_PARENTHESES;
    383           inSafemode = true;
    384           if (!removeParenthesisChars)
    385             token += line[i];
    386         }
    387         else if(line[i] == comment_char)
    388         {
    389           if (fallBackNeighbours > 0)
     44    /**
     45     * @brief default constructor
     46     */
     47    SubString::SubString()
     48    {}
     49
     50
     51    /**
     52     * @brief create a SubString from
     53     * @param string the String to Split
     54     * @param delimiter the Character at which to split string (delimiter)
     55     */
     56    SubString::SubString(const std::string& string, char delimiter)
     57    {
     58        this->split(string, delimiter);
     59    }
     60
     61
     62    /**
     63     * @brief Splits a String into multiple splitters.
     64     * @param string the String to split
     65     * @param delimiters multiple set of characters at what to split. (delimiters)
     66     * @param delimiterNeighbours neighbours of the delimiters, that will be erased only when near a delimiter.
     67     * @param emptyEntries If empty entries should be allewed or removed.
     68     * @param escapeChar The Escape Character that overrides splitters commends and so on...
     69     * @param safemode_char within these characters splitting won't happen
     70     * @param comment_char the Comment character.
     71     */
     72    SubString::SubString(const std::string& string,
     73                         const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
     74                         char escapeChar, bool removeEscapeChar, char safemode_char, bool removeSafemodeChar,
     75                         char openparenthesis_char, char closeparenthesis_char, bool removeParenthesisChars, char comment_char)
     76    {
     77        SubString::splitLine(this->strings, this->bInSafemode, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, removeEscapeChar, safemode_char, removeSafemodeChar, openparenthesis_char, closeparenthesis_char, removeParenthesisChars, comment_char);
     78    }
     79
     80    /**
     81     * @brief creates a SubSet of a SubString.
     82     * @param subString the SubString to take a set from.
     83     * @param subSetBegin the beginning to the end
     84     */
     85    SubString::SubString(const SubString& subString, unsigned int subSetBegin)
     86    {
     87        for (unsigned int i = subSetBegin; i < subString.size(); i++)
     88        {
     89            this->strings.push_back(subString[i]);
     90            this->bInSafemode.push_back(subString.isInSafemode(i));
     91        }
     92    }
     93
     94
     95    /**
     96     * @brief creates a SubSet of a SubString.
     97     * @param subString the SubString to take a Set from
     98     * @param subSetBegin the beginning to the end
     99     * @param subSetEnd the end of the SubSet (max subString.size() will be checked internaly)
     100     */
     101    SubString::SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd)
     102    {
     103        for (unsigned int i = subSetBegin; i < subString.size() && i < subSetEnd; i++)
     104        {
     105            this->strings.push_back(subString[i]);
     106            this->bInSafemode.push_back(subString.isInSafemode(i));
     107        }
     108    }
     109
     110    /**
     111     * @brief creates a Substring from a count and values set.
     112     * @param argc: the Arguments Count.
     113     * @param argv: Argument Values.
     114     */
     115    SubString::SubString(unsigned int argc, const char** argv)
     116    {
     117        for(unsigned int i = 0; i < argc; ++i)
     118        {
     119            this->strings.push_back(std::string(argv[i]));
     120            this->bInSafemode.push_back(false);
     121        }
     122    }
     123
     124    /**
     125     * @brief removes the object from memory
     126     */
     127    SubString::~SubString()
     128    { }
     129
     130    /** @brief An empty String */
     131    // const std::string SubString::emptyString = "";
     132    /** @brief Helper that gets you a String consisting of all White Spaces */
     133    const std::string SubString::WhiteSpaces = " \n\t";
     134    /** @brief Helper that gets you a String consisting of all WhiteSpaces and the Comma */
     135    const std::string SubString::WhiteSpacesWithComma = " \n\t,";
     136    /** An Empty SubString */
     137    const SubString SubString::NullSubString = SubString();
     138
     139    /**
     140     * @brief stores the Value of subString in this SubString
     141     * @param subString will be copied into this String.
     142     * @returns this SubString.
     143     */
     144    SubString& SubString::operator=(const SubString& subString)
     145    {
     146        this->strings = subString.strings;
     147        this->bInSafemode = subString.bInSafemode;
     148        return *this;
     149    }
     150
     151
     152    /**
     153     * @brief comparator.
     154     * @param subString the SubString to compare against this one.
     155     * @returns true if the Stored Strings match
     156     */
     157    bool SubString::operator==(const SubString& subString) const
     158    {
     159        return ((this->strings == subString.strings) && (this->bInSafemode == subString.bInSafemode));
     160    }
     161
     162    /**
     163     * @brief comparator.
     164     * @param subString the SubString to compare against this one.
     165     * @returns true if the Stored Strings match
     166     */
     167    bool SubString::compare(const SubString& subString) const
     168    {
     169        return (*this == subString);
     170    }
     171
     172    /**
     173     * @brief comparator.
     174     * @param subString the SubString to compare against this one.
     175     * @param length how many entries to compare. (from 0 to length)
     176     * @returns true if the Stored Strings match
     177     */
     178    bool SubString::compare(const SubString& subString, unsigned int length) const
     179    {
     180        if (length > this->size() || length > subString.size())
     181            return false;
     182
     183        for (unsigned int i = 0; i < length; i++)
     184            if ((this->strings[i] != subString.strings[i]) || (this->bInSafemode[i] != subString.bInSafemode[i]))
     185                return false;
     186        return true;
     187    }
     188
     189
     190    /**
     191     * @brief append operator
     192     * @param subString the String to append.
     193     * @returns a SubString where this and subString are appended.
     194     */
     195    SubString SubString::operator+(const SubString& subString) const
     196    {
     197        return SubString(*this) += subString;
     198    }
     199
     200
     201    /**
     202     * @brief append operator.
     203     * @param subString append subString to this SubString.
     204     * @returns this substring appended with subString
     205     */
     206    SubString& SubString::operator+=(const SubString& subString)
     207    {
     208        for (unsigned int i = 0; i < subString.size(); i++)
     209        {
     210            this->strings.push_back(subString[i]);
     211            this->bInSafemode.push_back(subString.isInSafemode(i));
     212        }
     213        return *this;
     214    }
     215
     216
     217    /**
     218     * @brief Split the String at
     219     * @param string where to split
     220     * @param splitter delimiter.
     221     */
     222    unsigned int SubString::split(const std::string& string, char splitter)
     223    {
     224        this->strings.clear();
     225        this->bInSafemode.clear();
     226        char split[2];
     227        split[0] = splitter;
     228        split[1] = '\0';
     229        SubString::splitLine(this->strings, this->bInSafemode, string, split);
     230        return strings.size();
     231    }
     232
     233
     234    /**
     235     * @brief Splits a String into multiple splitters.
     236     * @param string the String to split
     237     * @param delimiters multiple set of characters at what to split. (delimiters)
     238     * @param delimiterNeighbours: Neighbours to the Delimiters that will be erased too.
     239     * @param emptyEntries: If empty entries are added to the List of SubStrings
     240     * @param escapeChar The Escape Character that overrides splitters commends and so on...
     241     * @param safemode_char within these characters splitting won't happen
     242     * @param comment_char the Comment character.
     243     */
     244    unsigned int SubString::split(const std::string& string,
     245                                  const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
     246                                  char escapeChar, bool removeExcapeChar, char safemode_char, bool removeSafemodeChar,
     247                                  char openparenthesis_char, char closeparenthesis_char, bool removeParenthesisChars, char comment_char)
     248    {
     249        this->strings.clear();
     250        this->bInSafemode.clear();
     251        SubString::splitLine(this->strings, this->bInSafemode, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, removeExcapeChar, safemode_char, removeSafemodeChar, openparenthesis_char, closeparenthesis_char, removeParenthesisChars, comment_char);
     252        return this->strings.size();
     253    }
     254
     255
     256    /**
     257     * @brief joins together all Strings of this Substring.
     258     * @param delimiter the String between the subStrings.
     259     * @returns the joined String.
     260     */
     261    std::string SubString::join(const std::string& delimiter) const
     262    {
     263        if (!this->strings.empty())
     264        {
     265            std::string retVal = this->strings[0];
     266            for (unsigned int i = 1; i < this->strings.size(); i++)
     267                retVal += delimiter + this->strings[i];
     268            return retVal;
     269        }
     270        else
     271        {
     272            static std::string empty;
     273            return empty;
     274        }
     275    }
     276
     277
     278    /**
     279     * @brief creates a SubSet of a SubString.
     280     * @param subSetBegin the beginning to the end
     281     * @returns the SubSet
     282     *
     283     * This function is added for your convenience, and does the same as
     284     * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
     285     */
     286    SubString SubString::subSet(unsigned int subSetBegin) const
     287    {
     288        return SubString(*this, subSetBegin);
     289    }
     290
     291
     292    /**
     293     * @brief creates a SubSet of a SubString.
     294     * @param subSetBegin the beginning to
     295     * @param subSetEnd the end of the SubSet to select (if bigger than subString.size() it will be downset.)
     296     * @returns the SubSet
     297     *
     298     * This function is added for your convenience, and does the same as
     299     * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
     300     */
     301    SubString SubString::subSet(unsigned int subSetBegin, unsigned int subSetEnd) const
     302    {
     303        return SubString(*this, subSetBegin, subSetEnd);
     304    }
     305
     306
     307    /**
     308     * @brief splits line into tokens and stores them in ret.
     309     * @param ret the Array, where the Splitted strings will be stored in
     310     * to the beginning of the current token is stored
     311     * @param line the inputLine to split
     312     * @param delimiters a String of Delimiters (here the input will be splitted)
     313     * @param delimiterNeighbours Naighbours to the Delimitter, that will be removed if they are to the left or the right of a Delimiter.
     314     * @param emptyEntries: if empty Strings are added to the List of Strings.
     315     * @param escape_char: Escape carater (escapes splitters)
     316     * @param safemode_char: the beginning of the safemode is marked with this
     317     * @param removeSafemodeChar removes the safemode_char from the beginning and the ending of a token
     318     * @param openparenthesis_char the beginning of a safemode is marked with this
     319     * @param closeparenthesis_char the ending of a safemode is marked with this
     320     * @param removeParenthesisChars removes the parenthesis from the beginning and the ending of a token
     321     * @param comment_char: the beginning of a comment is marked with this: (until the end of a Line)
     322     * @param start_state: the Initial state on how to parse the String.
     323     * @return SPLIT_LINE_STATE the parser was in when returning
     324     *
     325     * This is the Actual Splitting Algorithm from Clemens Wacha
     326     * Supports delimiters, escape characters,
     327     * ignores special  characters between safemode_char and between comment_char and linend '\n'.
     328     */
     329    SubString::SPLIT_LINE_STATE
     330    SubString::splitLine(std::vector<std::string>& ret,
     331                         std::vector<bool>& bInSafemode,
     332                         const std::string& line,
     333                         const std::string& delimiters,
     334                         const std::string& delimiterNeighbours,
     335                         bool emptyEntries,
     336                         char escape_char,
     337                         bool removeExcapeChar,
     338                         char safemode_char,
     339                         bool removeSafemodeChar,
     340                         char openparenthesis_char,
     341                         char closeparenthesis_char,
     342                         bool removeParenthesisChars,
     343                         char comment_char,
     344                         SPLIT_LINE_STATE start_state)
     345    {
     346        SPLIT_LINE_STATE state = start_state;
     347        unsigned int i = 0;
     348        unsigned int fallBackNeighbours = 0;
     349
     350        std::string token;
     351        bool inSafemode = false;
     352
     353        if(start_state != SL_NORMAL && ret.size() > 0)
     354        {
     355            token = ret[ret.size()-1];
     356            ret.pop_back();
     357        }
     358        if(start_state != SL_NORMAL && bInSafemode.size() > 0)
     359        {
     360            inSafemode = bInSafemode[bInSafemode.size()-1];
     361            bInSafemode.pop_back();
     362        }
     363
     364        while(i < line.size())
     365        {
     366            switch(state)
     367            {
     368            case SL_NORMAL:
     369                if(line[i] == escape_char)
     370                {
     371                    state = SL_ESCAPE;
     372                    if (!removeExcapeChar)
     373                        token += line[i];
     374                }
     375                else if(line[i] == safemode_char)
     376                {
     377                    state = SL_SAFEMODE;
     378                    inSafemode = true;
     379                    if (!removeSafemodeChar)
     380                        token += line[i];
     381                }
     382                else if(line[i] == openparenthesis_char)
     383                {
     384                    state = SL_PARENTHESES;
     385                    inSafemode = true;
     386                    if (!removeParenthesisChars)
     387                        token += line[i];
     388                }
     389                else if(line[i] == comment_char)
     390                {
     391                    if (fallBackNeighbours > 0)
     392                        token = token.substr(0, token.size() - fallBackNeighbours);
     393                    /// FINISH
     394                    if(emptyEntries || token.size() > 0)
     395                    {
     396                        ret.push_back(token);
     397                        token.clear();
     398                        bInSafemode.push_back(inSafemode);
     399                        inSafemode = false;
     400                    }
     401                    token += line[i];       // EAT
     402                    state = SL_COMMENT;
     403                }
     404                else if(delimiters.find(line[i]) != std::string::npos)
     405                {
     406                    // line[i] is a delimiter
     407                    if (fallBackNeighbours > 0)
     408                        token = token.substr(0, token.size() - fallBackNeighbours);
     409                    /// FINISH
     410                    if(emptyEntries || token.size() > 0)
     411                    {
     412                        ret.push_back(token);
     413                        token.clear();
     414                        bInSafemode.push_back(inSafemode);
     415                        inSafemode = false;
     416                    }
     417                    state = SL_NORMAL;
     418                }
     419                else
     420                {
     421                    if (delimiterNeighbours.find(line[i]) != std::string::npos)
     422                    {
     423                        if (token.size() > 0)
     424                            ++fallBackNeighbours;
     425                        else
     426                        {
     427                            i++;
     428                            continue;
     429                        }
     430                    }
     431                    else
     432                        fallBackNeighbours = 0;
     433                    token += line[i];       // EAT
     434                }
     435                break;
     436            case SL_ESCAPE:
     437                if (!removeSafemodeChar)
     438                    token += line[i];
     439                else
     440                {
     441                    if(line[i] == 'n') token += '\n';
     442                    else if(line[i] == 't') token += '\t';
     443                    else if(line[i] == 'v') token += '\v';
     444                    else if(line[i] == 'b') token += '\b';
     445                    else if(line[i] == 'r') token += '\r';
     446                    else if(line[i] == 'f') token += '\f';
     447                    else if(line[i] == 'a') token += '\a';
     448                    else if(line[i] == '?') token += '\?';
     449                    else token += line[i];  // EAT
     450                }
     451                state = SL_NORMAL;
     452                break;
     453            case SL_SAFEMODE:
     454                if(line[i] == safemode_char)
     455                {
     456                    state = SL_NORMAL;
     457                    if (!removeSafemodeChar)
     458                        token += line[i];
     459                }
     460                else if(line[i] == escape_char)
     461                {
     462                    state = SL_SAFEESCAPE;
     463                }
     464                else
     465                {
     466                    token += line[i];       // EAT
     467                }
     468                break;
     469
     470            case SL_SAFEESCAPE:
     471                if(line[i] == 'n') token += '\n';
     472                else if(line[i] == 't') token += '\t';
     473                else if(line[i] == 'v') token += '\v';
     474                else if(line[i] == 'b') token += '\b';
     475                else if(line[i] == 'r') token += '\r';
     476                else if(line[i] == 'f') token += '\f';
     477                else if(line[i] == 'a') token += '\a';
     478                else if(line[i] == '?') token += '\?';
     479                else token += line[i];  // EAT
     480                state = SL_SAFEMODE;
     481                break;
     482
     483            case SL_PARENTHESES:
     484                if(line[i] == closeparenthesis_char)
     485                {
     486                    state = SL_NORMAL;
     487                    if (!removeParenthesisChars)
     488                        token += line[i];
     489                }
     490                else if(line[i] == escape_char)
     491                {
     492                    state = SL_PARENTHESESESCAPE;
     493                }
     494                else
     495                {
     496                    token += line[i];       // EAT
     497                }
     498                break;
     499
     500            case SL_PARENTHESESESCAPE:
     501                if(line[i] == 'n') token += '\n';
     502                else if(line[i] == 't') token += '\t';
     503                else if(line[i] == 'v') token += '\v';
     504                else if(line[i] == 'b') token += '\b';
     505                else if(line[i] == 'r') token += '\r';
     506                else if(line[i] == 'f') token += '\f';
     507                else if(line[i] == 'a') token += '\a';
     508                else if(line[i] == '?') token += '\?';
     509                else token += line[i];  // EAT
     510                state = SL_PARENTHESES;
     511                break;
     512
     513            case SL_COMMENT:
     514                if(line[i] == '\n')
     515                {
     516                    /// FINISH
     517                    if(token.size() > 0)
     518                    {
     519                        ret.push_back(token);
     520                        token.clear();
     521                        bInSafemode.push_back(inSafemode);
     522                        inSafemode = false;
     523                    }
     524                    state = SL_NORMAL;
     525                }
     526                else
     527                {
     528                    token += line[i];       // EAT
     529                }
     530                break;
     531
     532            default:
     533                // nothing
     534                break;
     535            }
     536            i++;
     537        }
     538
     539        /// FINISH
     540        if (fallBackNeighbours > 0)
    390541            token = token.substr(0, token.size() - fallBackNeighbours);
    391           /// FINISH
    392           if(emptyEntries || token.size() > 0)
    393           {
     542        if(emptyEntries || token.size() > 0)
     543        {
    394544            ret.push_back(token);
    395545            token.clear();
    396546            bInSafemode.push_back(inSafemode);
    397547            inSafemode = false;
    398           }
    399           token += line[i];       // EAT
    400           state = SL_COMMENT;
    401         }
    402         else if(delimiters.find(line[i]) != std::string::npos)
    403         {
    404           // line[i] is a delimiter
    405           if (fallBackNeighbours > 0)
    406             token = token.substr(0, token.size() - fallBackNeighbours);
    407           /// FINISH
    408           if(emptyEntries || token.size() > 0)
    409           {
    410             ret.push_back(token);
    411             token.clear();
    412             bInSafemode.push_back(inSafemode);
    413             inSafemode = false;
    414           }
    415           state = SL_NORMAL;
    416         }
    417         else
    418         {
    419           if (delimiterNeighbours.find(line[i]) != std::string::npos)
    420           {
    421             if (token.size() > 0)
    422               ++fallBackNeighbours;
    423             else
    424             {
    425               i++;
    426               continue;
    427             }
    428           }
    429           else
    430             fallBackNeighbours = 0;
    431           token += line[i];       // EAT
    432         }
    433         break;
    434       case SL_ESCAPE:
    435         if (!removeSafemodeChar)
    436           token += line[i];
    437         else
    438         {
    439           if(line[i] == 'n') token += '\n';
    440           else if(line[i] == 't') token += '\t';
    441           else if(line[i] == 'v') token += '\v';
    442           else if(line[i] == 'b') token += '\b';
    443           else if(line[i] == 'r') token += '\r';
    444           else if(line[i] == 'f') token += '\f';
    445           else if(line[i] == 'a') token += '\a';
    446           else if(line[i] == '?') token += '\?';
    447           else token += line[i];  // EAT
    448         }
    449         state = SL_NORMAL;
    450         break;
    451       case SL_SAFEMODE:
    452         if(line[i] == safemode_char)
    453         {
    454           state = SL_NORMAL;
    455           if (!removeSafemodeChar)
    456             token += line[i];
    457         }
    458         else if(line[i] == escape_char)
    459         {
    460           state = SL_SAFEESCAPE;
    461         }
    462         else
    463         {
    464           token += line[i];       // EAT
    465         }
    466         break;
    467 
    468       case SL_SAFEESCAPE:
    469         if(line[i] == 'n') token += '\n';
    470         else if(line[i] == 't') token += '\t';
    471         else if(line[i] == 'v') token += '\v';
    472         else if(line[i] == 'b') token += '\b';
    473         else if(line[i] == 'r') token += '\r';
    474         else if(line[i] == 'f') token += '\f';
    475         else if(line[i] == 'a') token += '\a';
    476         else if(line[i] == '?') token += '\?';
    477         else token += line[i];  // EAT
    478         state = SL_SAFEMODE;
    479         break;
    480 
    481       case SL_PARENTHESES:
    482         if(line[i] == closeparenthesis_char)
    483         {
    484           state = SL_NORMAL;
    485           if (!removeParenthesisChars)
    486             token += line[i];
    487         }
    488         else if(line[i] == escape_char)
    489         {
    490           state = SL_PARENTHESESESCAPE;
    491         }
    492         else
    493         {
    494           token += line[i];       // EAT
    495         }
    496         break;
    497 
    498       case SL_PARENTHESESESCAPE:
    499         if(line[i] == 'n') token += '\n';
    500         else if(line[i] == 't') token += '\t';
    501         else if(line[i] == 'v') token += '\v';
    502         else if(line[i] == 'b') token += '\b';
    503         else if(line[i] == 'r') token += '\r';
    504         else if(line[i] == 'f') token += '\f';
    505         else if(line[i] == 'a') token += '\a';
    506         else if(line[i] == '?') token += '\?';
    507         else token += line[i];  // EAT
    508         state = SL_PARENTHESES;
    509         break;
    510 
    511       case SL_COMMENT:
    512         if(line[i] == '\n')
    513         {
    514           /// FINISH
    515           if(token.size() > 0)
    516           {
    517             ret.push_back(token);
    518             token.clear();
    519             bInSafemode.push_back(inSafemode);
    520             inSafemode = false;
    521           }
    522           state = SL_NORMAL;
    523         }
    524         else
    525         {
    526           token += line[i];       // EAT
    527         }
    528         break;
    529 
    530       default:
    531         // nothing
    532         break;
    533     }
    534     i++;
    535   }
    536 
    537   /// FINISH
    538   if (fallBackNeighbours > 0)
    539     token = token.substr(0, token.size() - fallBackNeighbours);
    540   if(emptyEntries || token.size() > 0)
    541   {
    542     ret.push_back(token);
    543     token.clear();
    544     bInSafemode.push_back(inSafemode);
    545     inSafemode = false;
    546   }
    547   return(state);
     548        }
     549        return(state);
     550    }
     551
     552
     553    /**
     554     * @brief Some nice debug information about this SubString
     555     */
     556    void SubString::debug() const
     557    {
     558        printf("Substring-information::count=%d ::", this->strings.size());
     559        for (unsigned int i = 0; i < this->strings.size(); i++)
     560            printf("s%d='%s'::", i, this->strings[i].c_str());
     561        printf("\n");
     562    }
    548563}
    549 
    550 
    551 /**
    552  * @brief Some nice debug information about this SubString
    553  */
    554 void SubString::debug() const
    555 {
    556   printf("Substring-information::count=%d ::", this->strings.size());
    557   for (unsigned int i = 0; i < this->strings.size(); i++)
    558     printf("s%d='%s'::", i, this->strings[i].c_str());
    559   printf("\n");
    560 }
Note: See TracChangeset for help on using the changeset viewer.