Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7474 in orxonox.OLD for trunk


Ignore:
Timestamp:
May 2, 2006, 6:24:43 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: SubString::split algorithm revisited: now it Splits Strings where Delimitters are, but it ereases the Neighbours of those delimiters, if you want it
also there is now a way to have empty Entries.

Location:
trunk/src/lib
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/shell/shell_input.cc

    r7458 r7474  
    1818#include "shell_input.h"
    1919
    20 
    21 
    2220#include "shell_command.h"
    2321#include "shell_command_class.h"
    24 #include "shell_completion.h"
    2522#include "event_handler.h"
    2623
    2724#include "debug.h"
    2825#include "compiler.h"
    29 #include "stdlibincl.h"
    3026#include "key_names.h"
    3127
  • trunk/src/lib/util/executor/executor.h

    r7419 r7474  
    5252    // EXECUTE
    5353    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
    54     virtual void operator()(BaseObject* object, const std::string& parameters = "") const = 0;
     54    virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const = 0;
    5555    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes @brief here for your convenience*/
    5656    void execute (BaseObject* object, const std::string& parameters = "") const { (*this)(object, parameters); };
     
    256256    } fp;
    257257
    258     virtual void operator()(BaseObject* object, const std::string& parameters = "") const
     258    virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    259259    {
    260       SubString sub;
    261       sub.split(parameters, " \n\t,", '\\');
    262260//! FUNCTOR_LIST is the List of Executive Functions
    263261#define FUNCTOR_LIST(x) ExecutorExecute ## x
     
    319317
    320318
    321     virtual void operator()(BaseObject* object, const std::string& parameters = "") const
     319    virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    322320    {
    323       SubString sub;
    324       sub.split(parameters, " \n\t,", '\\');
    325321//! FUNCTOR_LIST is the List of Executive Functions
    326322#define FUNCTOR_LIST(x) ExecutorExecute ## x
  • trunk/src/lib/util/executor/executor_specials.h

    r7419 r7474  
    6060     * @param loadString ignored in this case
    6161     */
    62     virtual void operator()(BaseObject* object, const std::string& = "") const
     62    virtual void operator()(BaseObject* object, const SubString& = SubString()) const
    6363    {
    6464      if (object != NULL && this->element != NULL)
  • trunk/src/lib/util/loading/load_param.cc

    r7331 r7474  
    6767    {
    6868      PRINTF(4)("Loading value '%s' with Parameters '%s' onto: %s::%s\n", this->paramName.c_str(), loadString.c_str(), this->object->getClassName(), this->object->getName());
    69       (*this->executor)(this->object, loadString);
     69      (*this->executor)(this->object, SubString(loadString, ",", SubString::WhiteSpaces, false, '\\'));
    7070    }
    7171    delete this->executor;
  • trunk/src/lib/util/substring.cc

    r7398 r7474  
    4242 * @brief create a SubString from
    4343 * @param string the String to Spilit
    44  * @param splitter the Character at which to split string (delimiter)
    45  */
    46 SubString::SubString(const std::string& string, char splitter)
    47 {
    48   char split[2];
    49   split[0] = splitter;
    50   split[1] = '\0';
    51   SubString::splitLine(this->strings, string, split);
     44 * @param delimiter the Character at which to split string (delimiter)
     45 */
     46SubString::SubString(const std::string& string, char delimiter)
     47{
     48  this->split(string, delimiter);
    5249}
    5350
     
    5653 * @brief Splits a String into multiple splitters.
    5754 * @param string the String to split
    58  * @param splitters multiple set of characters at what to split. (delimiters)
     55 * @param delimiters multiple set of characters at what to split. (delimiters)
     56 * @param delimiterNeighbours neighbours of the delimiters, that will be erased only when near a delimiter.
    5957 * @param escapeChar The Escape Character that overrides splitters commends and so on...
    6058 * @param safemode_char within these characters splitting won't happen
    6159 * @param comment_char the Comment character.
    6260 */
    63 SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar, char safemode_char, char comment_char)
    64 {
    65   SubString::splitLine(this->strings, string, splitters, escapeChar, safemode_char, comment_char);
     61SubString::SubString(const std::string& string,
     62                     const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
     63                     char escapeChar, char safemode_char, char comment_char)
     64{
     65  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
    6666}
    6767
     
    196196 * @brief Splits a String into multiple splitters.
    197197 * @param string the String to split
    198  * @param splitters multiple set of characters at what to split. (delimiters)
     198 * @param delimiters multiple set of characters at what to split. (delimiters)
     199 * @param delimiterNeighbours: Neighbours to the Delimiters that will be erased too.
     200 * @param emptyEntries: If empty entries are added to the List of SubStrings
    199201 * @param escapeChar The Escape Character that overrides splitters commends and so on...
    200202 * @param safemode_char within these characters splitting won't happen
    201203 * @param comment_char the Comment character.
    202204 */
    203 unsigned int SubString::split(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
     205unsigned int SubString::split(const std::string& string,
     206                              const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
     207                              char escapeChar,char safemode_char, char comment_char)
    204208{
    205209  this->strings.clear();
    206   SubString::splitLine(this->strings, string, splitters, escapeChar, safemode_char, comment_char);
     210  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
    207211  return this->strings.size();
    208212}
     
    264268 * @param line the inputLine to split
    265269 * @param delimiters a String of Delimiters (here the input will be splitted)
     270 * @param delimiterNeighbour Naighbours to the Delimitter, that will be removed if they are to the left or the right of a Delimiter.
     271 * @param emptyEntries: if empty Strings are added to the List of Strings.
    266272 * @param escape_char: Escape carater (escapes splitters)
    267273 * @param safemode_char: the beginning of the safemode is marked with this
     
    274280 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
    275281 *
    276  */
    277 SPLIT_LINE_STATE SubString::splitLine(std::vector<std::string>& ret,
    278                                       const std::string& line,
    279                                       const std::string& delimiters,
    280                                       char escape_char,
    281                                       char safemode_char,
    282                                       char comment_char,
    283                                       SPLIT_LINE_STATE start_state)
     282 *
     283 */
     284SubString::SPLIT_LINE_STATE
     285SubString::splitLine(std::vector<std::string>& ret,
     286                     const std::string& line,
     287                     const std::string& delimiters,
     288                     const std::string& delimiterNeighbours,
     289                     bool emptyEntries,
     290                     char escape_char,
     291                     char safemode_char,
     292                     char comment_char,
     293                     SPLIT_LINE_STATE start_state)
    284294{
    285295  SPLIT_LINE_STATE state = start_state;
    286296  unsigned int i = 0;
     297  unsigned int fallBackNeighbours = 0;
     298
    287299  std::string token;
    288300
     
    297309    switch(state)
    298310    {
    299         case SL_NORMAL:
     311      case SL_NORMAL:
    300312        if(line[i] == escape_char)
    301313        {
     
    308320        else if(line[i] == comment_char)
    309321        {
     322          if (fallBackNeighbours > 0)
     323            token = token.substr(0, token.size() - fallBackNeighbours);
    310324          /// FINISH
    311           if(token.size() > 0)
     325          if(emptyEntries || token.size() > 0)
    312326          {
    313327            ret.push_back(token);
     
    320334        {
    321335          // line[i] is a delimiter
     336          if (fallBackNeighbours > 0)
     337            token = token.substr(0, token.size() - fallBackNeighbours);
    322338          /// FINISH
    323           if(token.size() > 0)
     339          if(emptyEntries || token.size() > 0)
    324340          {
    325341            ret.push_back(token);
    326342            token.clear();
    327343          }
     344          state = SL_NORMAL;
    328345        }
    329346        else
    330347        {
     348          if (delimiterNeighbours.find(line[i]) != std::string::npos)
     349          {
     350            if (token.size() > 0)
     351              ++fallBackNeighbours;
     352            else
     353            {
     354              i++;
     355              continue;
     356            }
     357          }
     358          else
     359            fallBackNeighbours = 0;
    331360          token += line[i];       // EAT
    332361        }
    333362        break;
    334         case SL_ESCAPE:
     363      case SL_ESCAPE:
    335364        if(line[i] == 'n') token += '\n';
    336365        else if(line[i] == 't') token += '\t';
     
    344373        state = SL_NORMAL;
    345374        break;
    346         case SL_SAFEMODE:
     375      case SL_SAFEMODE:
    347376        if(line[i] == safemode_char)
    348377        {
     
    358387        }
    359388        break;
    360         case SL_SAFEESCAPE:
     389
     390      case SL_SAFEESCAPE:
    361391        if(line[i] == 'n') token += '\n';
    362392        else if(line[i] == 't') token += '\t';
     
    370400        state = SL_SAFEMODE;
    371401        break;
    372         case SL_COMMENT:
     402
     403      case SL_COMMENT:
    373404        if(line[i] == '\n')
    374405        {
     
    386417        }
    387418        break;
    388         default:
     419
     420      default:
    389421        // nothing
    390422        break;
     
    394426
    395427  /// FINISH
    396   if(token.size() > 0)
     428  if (fallBackNeighbours > 0)
     429    token = token.substr(0, token.size() - fallBackNeighbours);
     430  if(emptyEntries || token.size() > 0)
    397431  {
    398432    ret.push_back(token);
  • trunk/src/lib/util/substring.h

    r7398 r7474  
    1010#include <string>
    1111
    12 typedef enum {
    13   SL_NORMAL,
    14   SL_ESCAPE,
    15   SL_SAFEMODE,
    16   SL_SAFEESCAPE,
    17   SL_COMMENT,
    18 } SPLIT_LINE_STATE;
    1912
    2013//! A class that can load one string and split it in multipe ones
     
    2619{
    2720public:
     21  typedef enum {
     22    SL_NORMAL,
     23    SL_ESCAPE,
     24    SL_SAFEMODE,
     25    SL_SAFEESCAPE,
     26    SL_COMMENT,
     27  } SPLIT_LINE_STATE;
     28
     29
     30public:
    2831  SubString();
    29   SubString(const std::string& string, char splitter = ',');
    30   SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
     32  SubString(const std::string& string, char delimiter = ',');
     33  SubString(const std::string& string,
     34            const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries=false,
     35            char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
    3136  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
    3237  SubString(const SubString& subString) { *this = subString; };
     
    4752  /////////////////////////////////////////
    4853  // Split and Join the any String. ///////
    49   unsigned int split(const std::string& string = "", char splitter = ',');
    50   unsigned int split(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
     54  unsigned int split(const std::string& string = "", char delimiter = ',');
     55  unsigned int split(const std::string& string,
     56                     const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false,
     57                     char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
    5158  std::string join(const std::string& delimiter = " ") const;
    5259  ////////////////////////////////////////
     
    6269  inline unsigned int size() const { return this->strings.size(); };
    6370  /** @param i the i'th String @returns the i'th string from the subset of Strings */
    64   const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
     71const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
    6572  /** @param i the i'th String @returns the i'th string from the subset of Strings */
    6673  const std::string& getString(unsigned int i) const { return (*this)[i]; };
     
    7077                                    const std::string& line,
    7178                                    const std::string& delimiters = SubString::WhiteSpaces,
     79                                    const std::string& delimiterNeighbours = "",
     80                                    bool emptyEntries = false,
    7281                                    char escape_char = '\\',
    7382                                    char safemode_char = '"',
Note: See TracChangeset for help on using the changeset viewer.