Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/substring.cc @ 6381

Last change on this file since 6381 was 5656, checked in by bensch, 19 years ago

orxonox/trunk: new Version of the String-splitter (without Escape-sequence for the time being)

File size: 6.6 KB
RevLine 
[4597]1/*
[3941]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
[4597]13   co-programmer: Benjamin Grauer
14
15   2005-06-10: some naming conventions
[4220]16*/
17
18
[3941]19/**
[4836]20 *  breaks a string into parts that were initially seperated by comma
21 * @param string the string to break into substrings
[4220]22*/
[3941]23
[4220]24#include "substring.h"
25
[4833]26#include "debug.h"
[4220]27#include <string.h>
28#include <assert.h>
29
[4734]30SubString::SubString( const char* string, char splitter)
[4220]31{
[4830]32  this->splittersCount = 0;
[5150]33  if (string == NULL)
34  {
35    this->strings = NULL;
[5200]36    this->offsets = NULL;
[5150]37    return;
38  }
[4597]39
[5150]40  for( int i = 0; i < strlen(string); i++)
41    if( string[i] == splitter)
42      this->splittersCount++;
[4597]43
[4830]44  this->splittersCount += 1;
[4597]45
[4830]46  this->strings = new char*[this->splittersCount];
[5200]47  this->offsets = new unsigned int[this->splittersCount];
48  assert (this->strings != NULL && this->offsets != NULL);
[4597]49
[4220]50  int i = 0;
51  int l = 0;
[4597]52
[5150]53  if( this->splittersCount > 1)
54  {
55    const char* offset = string;
[5183]56    const char* end = strchr( string, splitter);
[5150]57    while( end != NULL)
[4220]58    {
[4830]59      assert( i < this->splittersCount);
[4220]60      l = end - offset;
[4830]61      this->strings[i] = new char[l + 1];
[4220]62      assert( strings[i] != NULL);
63      strncpy( strings[i], offset, l);
[5183]64      strings[i][l] = '\0';
[5200]65      this->offsets[i] = offset - string;
[4220]66      i++;
67      end++;
68      offset = end;
[4734]69      end = strchr( offset, splitter);
[4220]70    }
[4597]71
[5209]72    l = strlen( offset);
[5150]73    strings[i] = new char[l + 1];
74    strncpy( strings[i], offset, l);
75    strings[i][l] = '\0';
[5200]76    this->offsets[i] = offset - string;
[5150]77  }
78  else
79  {
[5183]80    this->strings[0] = new char[strlen(string)+1];
[5150]81    strcpy(this->strings[0], string);
[5200]82    this->offsets[0] = 0;
[5150]83  }
[4220]84}
85
[5183]86
[3941]87/**
[5183]88 * Splits a String into a Substring removing all whiteSpaces
89 * @param string the String to Split
90 * @param whiteSpaces MUST BE __TRUE__
91 *
92 */
93SubString::SubString(const char* string, bool whiteSpaces)
94{
95  this->splittersCount = 0;
96  if (string == NULL || whiteSpaces == false)
97  {
98    this->strings = NULL;
[5200]99    this->offsets = NULL;
[5183]100    return;
101  }
102
103  // chop the input to the beginning of something usefull
104  if (strlen(string) > 0)
105    string = string + strspn(string, " \t\n");
106
107  // count the Splitters
108  bool lastWasWhiteSpace = false;
109  for(unsigned int i = 0; i < strlen(string); i++)
110    if( string[i] == ' ' || string[i] == '\t' || string[i] == '\n' )
111      lastWasWhiteSpace = true;
112    else
113    {
114      if (lastWasWhiteSpace)
115        this->splittersCount ++;
116      lastWasWhiteSpace = false;
117    }
118  this->splittersCount += 1;
119
[5184]120  // allocate memory
[5183]121  this->strings = new char*[this->splittersCount];
[5200]122  this->offsets = new unsigned int[this->splittersCount];
123  assert (this->strings != NULL && this->offsets != NULL);
[5183]124
[5656]125
[5184]126  // split the String into substrings
[5183]127  int l = 0;
128  unsigned int i = 0;
129  if( this->splittersCount > 1)
130  {
131    const char* offset = string;
132    const char* end = offset + strcspn(offset, " \t\n");
133    for (i = 0; i < this->splittersCount; i++)
134    {
135      assert( i < this->splittersCount);
136      l = end - offset;
137      this->strings[i] = new char[l + 1];
138      assert( strings[i] != NULL);
139      strncpy( strings[i], offset, l);
140      strings[i][l] = '\0';
[5200]141      this->offsets[i] = offset - string;
[5183]142      end += strspn(end, " \t\n");
143      offset = end;
144      end = offset + strcspn(offset, " \t\n");
145    }
146  }
147  else
148  {
149    unsigned int length = strcspn(string, " \t\n");
150    this->strings[0] = new char[length+1];
151    strncpy(this->strings[0], string, length);
152    this->strings[0][length] = '\0';
[5200]153    offsets[0] = 0;
[5183]154  }
155}
156
[5656]157SubString::SubString(const char* string, const char* splitters, char escapeChar)
158{
159  this->splittersCount = 0;
160  if (string == NULL || splitters == NULL)
161  {
162    this->strings = NULL;
163    this->offsets = NULL;
164    return;
165  }
166
167  // chop the input to the beginning of something usefull
168  if (strlen(string) > 0)
169    string = string + strspn(string, splitters);
170
171  // count the Splitters
172  bool lastWasSplitter = false;
173  for(unsigned int i = 0; i < strlen(string); i++)
174  {
175
176    if( strchr(splitters, string[i] ))
177      lastWasSplitter = true;
178    else
179    {
180      if (lastWasSplitter)
181      {
182        this->splittersCount ++;
183        lastWasSplitter = false;
184      }
185    }
186  }
187  this->splittersCount += 1;
188
189  // allocate memory
190  this->strings = new char*[this->splittersCount];
191  this->offsets = new unsigned int[this->splittersCount];
192  assert (this->strings != NULL && this->offsets != NULL);
193
194
195  // split the String into substrings
196  int l = 0;
197  unsigned int i = 0;
198  if( this->splittersCount > 1)
199  {
200    const char* offset = string;
201    const char* end = offset + strcspn(offset, splitters);
202    for (i = 0; i < this->splittersCount; i++)
203    {
204      assert( i < this->splittersCount);
205      l = end - offset;
206      this->strings[i] = new char[l + 1];
207      assert( strings[i] != NULL);
208      strncpy( strings[i], offset, l);
209      strings[i][l] = '\0';
210      this->offsets[i] = offset - string;
211      end += strspn(end, splitters);
212      offset = end;
213      end = offset + strcspn(offset, splitters);
214    }
215  }
216  else
217  {
218    unsigned int length = strcspn(string, splitters);
219    this->strings[0] = new char[length+1];
220    strncpy(this->strings[0], string, length);
221    this->strings[0][length] = '\0';
222    offsets[0] = 0;
223  }
224}
225
226
[5183]227/**
[4836]228 *  removes the object from memory
[3941]229*/
[4220]230SubString::~SubString()
231{
[5150]232  if (this->strings)
233  {
[5183]234    for(unsigned int i = 0; i < this->splittersCount; i++)
[5150]235      delete[] this->strings[i];
236    delete[] this->strings;
237  }
[5200]238  delete[] this->offsets;
[4220]239}
[3941]240
241/**
[4836]242 *  get a particular substring
243 * @param i the ID of the substring to return
244 * @returns the designated substring or NULL if an invalid ID was given
[4220]245*/
[5150]246const char* SubString::getString(unsigned int i)
[4220]247{
[4830]248  if( i < this->splittersCount && i >= 0)
249    return this->strings[i];
250  else
251    return NULL;
[4220]252}
[4833]253
254/**
[5200]255 * get a particular substring's offset
256 * @param i the ID of the substring to get the offset from
257 * @returns the offset or NULL if an invalid ID was given
258 */
259unsigned int SubString::getOffset(unsigned int i)
260{
261  if( i < this->splittersCount && i >= 0)
262    return this->offsets[i];
263  else
264    return 0;
265}
266
267/**
[4833]268 * Some nice debug information about this SubString
269 */
270void SubString::debug() const
271{
272  PRINT(0)("Substring-information::count=%d ::", this->splittersCount);
[5183]273  if (this->strings != NULL)
274    for (unsigned int i = 0; i < this->splittersCount; i++)
275     PRINT(0)("s%d='%s'::", i, this->strings[i]);
[4833]276  PRINT(0)("\n");
277}
Note: See TracBrowser for help on using the repository browser.