File: cstring_t.cc

package info (click to toggle)
simutrans 100.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,776 kB
  • ctags: 9,485
  • sloc: cpp: 72,459; ansic: 5,646; makefile: 450
file content (252 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <string.h>
#include <stdio.h>
#include "cstring_t.h"

// ------------- global operators --------------------


/**
 * Concatenates a char array and a string
 * @author Hj. Malthaner
 */

/*
cstring_t operator+ (const char *buf, const cstring_t &other)
{
    const int len = strlen(buf) + other.len() + 1;

    char *tmp = new char[len];

    strcpy(tmp, buf);
    strcat(tmp, other);

    cstring_t result (tmp);
    delete tmp;

    return result;

}
*/

// ------------- cstring class --------------------


cstring_t::cstring_t()
{
	//printf("cstring_t::cstring_t()\n");
	buf = 0;
}


cstring_t::cstring_t(const char *other)
{
	const int len = strlen(other)+1;
	buf = new char[len];
	strcpy(buf, other);
}


cstring_t::cstring_t(const cstring_t &other)
{
	const int len = other.len()+1;
	buf = NULL;
	if(len>0) {
		buf = new char[len];
		strcpy(buf, other.buf);
	}
}


cstring_t::~cstring_t()
{
	//printf("cstring_t::~cstring_t()\n buf=%s %p\n", buf, buf);
	if(buf) {
		delete [] buf;
		buf = 0;
	}
}


/**
 * Appends a char array to this string
 * @author Hj. Malthaner
 */
cstring_t cstring_t::operator+ (const char *other) const
{
    //printf("cstring_t cstring_t::operator+ (const char *other) const\n");
	if(buf) {
		const int tmplen = strlen(buf)+strlen(other) + 1;
		char *tmp = new char[tmplen];

		strcpy(tmp, buf);
		strcat(tmp, other);
		cstring_t result(tmp);
		delete [] tmp;
		return result;
	}
	return cstring_t(other);
}



void cstring_t::set_at(int idx, char x) const
{
    if(idx > 0 && idx < len()) {
	buf[idx] = x;
    }
}


cstring_t & cstring_t::operator= (const cstring_t &other)
{
	if(buf!=NULL) {
		delete [] buf;
	}
	buf = NULL;
	if (other.len() >= 0) {
		buf = new char[other.len()+1];
		strcpy(buf, other.buf);
	}
	return *this;
}


cstring_t & cstring_t::operator= (const char *str)
{
	if(buf!=NULL) {
		delete [] buf;
	}
	buf = NULL;
	if(str) {
		buf = new char[strlen(str)+1];
		strcpy(buf, str);
	}
	return *this;
}


/**
 * Comparison operator
 * @author Hj. Malthaner
 */
bool cstring_t::operator== (const cstring_t &other) const
{
  return strcmp(buf, other.buf) == 0;
}


bool cstring_t::operator!= (const cstring_t &other) const
{
  // printf("%s, %s\n   %d\n", buf, other.buf, strcmp(buf, other.buf) != 0);

  return strcmp(buf, other.buf) != 0;
}

bool cstring_t::operator== (const char *other) const
{
  return strcmp(buf, other) == 0;
}


bool cstring_t::operator!= (const char *other) const
{
  // printf("%s, %s\n   %d\n", buf, other.buf, strcmp(buf, other.buf) != 0);

  return strcmp(buf, other) != 0;
}


/**
 * @return Number of characters in this string
 * @author Hj. Malthaner
 */
int cstring_t::len() const
{
	//printf("cstring_t::len() const\n");
	return buf ? ((int)strlen(buf)) : -1;
}


/**
 * Substring operator
 * @param first first char to include
 * @param last position after last char to include
 * @author Hj. Malthaner
 */
cstring_t cstring_t::substr(int first, int last)
{
  int   len  = last - first;
  char *dest = new char[len+1];
  char *src  = buf+first;

  for(int i=0; i<len; i++) {
    dest[i] = src[i];
  }

  dest[len] = '\0';


  cstring_t result(dest);
  delete [] dest;

  return result;
}

long cstring_t::find(char x) const
{
	char *p = buf;

	if(p && *p) {
		do {
			if(*p == x) {
				return (p  - buf);
			}
		} while(*++p);
	}
	return -1;
}

long cstring_t::find(const char *text) const
{
	long l = strlen(text);
	long	n = len() - l + 1;

	for(int i = 0; i < n; i++) {
		if(!strncmp(text, buf, l)) {
			return i;
		}
	}
	return -1;
}

long cstring_t::find_back(char x) const
{
	if(buf) {
		char *p = buf + len();

		while(p-- != buf) {
			if(*p == x) {
				return (p - buf);
			}
		}
	}
	return -1;
}


// replaces all characters
int cstring_t::replace_character(char old,char ch)
{
	int many=0;
	char *p = buf;

	if(p && *p) {
		do {
			if(*p == old) {
				*p = ch;
				many ++;
			}
		} while(*++p);
	}
	return many;
}