Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/old.we/src/lib/util/substring.cc @ 5837

Last change on this file since 5837 was 5209, checked in by bensch, 19 years ago

orxonox/trunk: valgrind's second fruits….. the char-arrays
some time in the near future i will hopefully be faster in interpreting this WALLgrind… but it is great, i can tell you (or at least anyone that reads this :))

File size: 4.9 KB
Line 
1/*
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
13   co-programmer: Benjamin Grauer
14
15   2005-06-10: some naming conventions
16*/
17
18
19/**
20 *  breaks a string into parts that were initially seperated by comma
21 * @param string the string to break into substrings
22*/
23
24#include "substring.h"
25
26#include "debug.h"
27#include <string.h>
28#include <assert.h>
29
30SubString::SubString( const char* string, char splitter)
31{
32  this->splittersCount = 0;
33  if (string == NULL)
34  {
35    this->strings = NULL;
36    this->offsets = NULL;
37    return;
38  }
39
40  for( int i = 0; i < strlen(string); i++)
41    if( string[i] == splitter)
42      this->splittersCount++;
43
44  this->splittersCount += 1;
45
46  this->strings = new char*[this->splittersCount];
47  this->offsets = new unsigned int[this->splittersCount];
48  assert (this->strings != NULL && this->offsets != NULL);
49
50  int i = 0;
51  int l = 0;
52
53  if( this->splittersCount > 1)
54  {
55    const char* offset = string;
56    const char* end = strchr( string, splitter);
57    while( end != NULL)
58    {
59      assert( i < this->splittersCount);
60      l = end - offset;
61      this->strings[i] = new char[l + 1];
62      assert( strings[i] != NULL);
63      strncpy( strings[i], offset, l);
64      strings[i][l] = '\0';
65      this->offsets[i] = offset - string;
66      i++;
67      end++;
68      offset = end;
69      end = strchr( offset, splitter);
70    }
71
72    l = strlen( offset);
73    strings[i] = new char[l + 1];
74    strncpy( strings[i], offset, l);
75    strings[i][l] = '\0';
76    this->offsets[i] = offset - string;
77  }
78  else
79  {
80    this->strings[0] = new char[strlen(string)+1];
81    strcpy(this->strings[0], string);
82    this->offsets[0] = 0;
83  }
84}
85
86
87/**
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;
99    this->offsets = NULL;
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
120  // allocate memory
121  this->strings = new char*[this->splittersCount];
122  this->offsets = new unsigned int[this->splittersCount];
123  assert (this->strings != NULL && this->offsets != NULL);
124
125  // split the String into substrings
126  int l = 0;
127  unsigned int i = 0;
128  if( this->splittersCount > 1)
129  {
130    const char* offset = string;
131    const char* end = offset + strcspn(offset, " \t\n");
132    for (i = 0; i < this->splittersCount; i++)
133    {
134      assert( i < this->splittersCount);
135      l = end - offset;
136      this->strings[i] = new char[l + 1];
137      assert( strings[i] != NULL);
138      strncpy( strings[i], offset, l);
139      strings[i][l] = '\0';
140      this->offsets[i] = offset - string;
141      end += strspn(end, " \t\n");
142      offset = end;
143      end = offset + strcspn(offset, " \t\n");
144    }
145  }
146  else
147  {
148    unsigned int length = strcspn(string, " \t\n");
149    this->strings[0] = new char[length+1];
150    strncpy(this->strings[0], string, length);
151    this->strings[0][length] = '\0';
152    offsets[0] = 0;
153  }
154}
155
156/**
157 *  removes the object from memory
158*/
159SubString::~SubString()
160{
161  if (this->strings)
162  {
163    for(unsigned int i = 0; i < this->splittersCount; i++)
164      delete[] this->strings[i];
165    delete[] this->strings;
166  }
167  delete[] this->offsets;
168}
169
170/**
171 *  get a particular substring
172 * @param i the ID of the substring to return
173 * @returns the designated substring or NULL if an invalid ID was given
174*/
175const char* SubString::getString(unsigned int i)
176{
177  if( i < this->splittersCount && i >= 0)
178    return this->strings[i];
179  else
180    return NULL;
181}
182
183/**
184 * get a particular substring's offset
185 * @param i the ID of the substring to get the offset from
186 * @returns the offset or NULL if an invalid ID was given
187 */
188unsigned int SubString::getOffset(unsigned int i)
189{
190  if( i < this->splittersCount && i >= 0)
191    return this->offsets[i];
192  else
193    return 0;
194}
195
196/**
197 * Some nice debug information about this SubString
198 */
199void SubString::debug() const
200{
201  PRINT(0)("Substring-information::count=%d ::", this->splittersCount);
202  if (this->strings != NULL)
203    for (unsigned int i = 0; i < this->splittersCount; i++)
204     PRINT(0)("s%d='%s'::", i, this->strings[i]);
205  PRINT(0)("\n");
206}
Note: See TracBrowser for help on using the repository browser.