Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented aim

File size: 2.0 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   \brief 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 <string.h>
27#include <assert.h>
28
29SubString::SubString( const char* string, char splitter)
30{
31  this->splittersCount = 0;
32
33  assert( string != NULL);
34
35  for( int i = 0; i < strlen(string); i++) if( string[i] == splitter)
36      splittersCount++;
37
38  this->splittersCount += 1;
39
40  this->strings = new char*[this->splittersCount];
41  assert (strings != NULL);
42
43  int i = 0;
44  int l = 0;
45
46  const char* offset = string;
47  char* end = strchr( string, splitter);
48  while( end != NULL)
49    {
50      assert( i < this->splittersCount);
51      l = end - offset;
52      this->strings[i] = new char[l + 1];
53      assert( strings[i] != NULL);
54      strncpy( strings[i], offset, l);
55      strings[i][l] = 0;
56      i++;
57      end++;
58      offset = end;
59      end = strchr( offset, splitter);
60    }
61
62  strings[i] = new char[l + 1];
63  l = strlen( offset);
64  strncpy( strings[i], offset, l);
65  strings[i][l] = 0;
66}
67
68/**
69   \brief removes the object from memory
70*/
71SubString::~SubString()
72{
73  for( int i = 0; i < this->splittersCount; i++)
74    {
75      delete this->strings[i];
76    }
77
78  delete this->strings;
79}
80
81/**
82   \brief get a particular substring
83   \param i the ID of the substring to return
84   \returns the designated substring or NULL if an invalid ID was given
85*/
86const char* SubString::getString( int i)
87{
88  if( i < this->splittersCount && i >= 0)
89    return this->strings[i];
90  else
91    return NULL;
92}
Note: See TracBrowser for help on using the repository browser.