File: cstring_t.h

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 (134 lines) | stat: -rw-r--r-- 2,399 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
#ifndef CSTRING_T_H
#define CSTRING_T_H

#include <stdarg.h>


#ifndef STRICMP
#ifdef _MSC_VER
#define STRICMP stricmp
#define STRNICMP strnicmp
#else
#define STRICMP strcasecmp
#define STRNICMP strncasecmp
#endif
#endif

/**
 * A simple string class.
 * @author Hj. Malthaner
 * @date 12-Jan-2002
 */
class cstring_t
{
private:
	char *buf;

public:
	/**
	 * Builds a uninitialised string (len() == -1)
	 * @author Hj. Malthaner
	 */
	cstring_t();

	/**
	 * Builds a string as a copy of a char array
	 * @author Hj. Malthaner
	 */
	cstring_t(const char *other);

	/**
	 * Builds a string as a copy of a string
	 * @author Hj. Malthaner
	 */
	cstring_t(const cstring_t &other);

	~cstring_t();

	/**
	 * Concatenates this string and a char array
	 * @author Hj. Malthaner
	 */
	cstring_t operator+ (const char *) const;


	/**
	 * Assignement operator
	 * @author Hj. Malthaner
	 */
	cstring_t & operator= (const cstring_t &);
	cstring_t & operator=(const char *);


	/**
	 * Comparison operator
	 * @author Hj. Malthaner
	 */
	bool operator== (const cstring_t &) const;


	bool operator!= (const cstring_t &) const;


	bool operator== (const char *) const;


	bool operator!= (const char *) const;


	/**
	 * Automagic conversion to a const char* for backwards compatibility
	 * @author Hj. Malthaner
	 */
	operator const char*() const { return buf; }

	/**
	 * @return Number of characters in this string
	 * -1 for uninitalized
	 * @author Hj. Malthaner
	 */
	int len() const;

	// true for an empty or unallocated string
	bool empty() { return buf==0  ||  buf[0]==0; }

	/**
	 * Substring operator
	 * @param first first char to include
	 * @param last position after last char to include
	 * @author Hj. Malthaner
	 */
	cstring_t substr(int first, int last);

	cstring_t right(int newlen) {
		int oldlen = len();
		return (newlen > oldlen) ? *this : substr(oldlen - newlen, oldlen);
	}

	cstring_t left(int newlen) {
		int oldlen = len();
		return (newlen > oldlen) ? *this : substr(0, newlen);
	}

	cstring_t mid(int start, int newlen = -1)
	{
		int oldlen = len();
		if(newlen == -1 || start + newlen > oldlen)
			return substr(start, start + oldlen);
		else
			return substr(start, start + newlen);
	}

	int replace_character( char old_ch, char new_ch);

	void set_at(int idx, char) const;

	long find(const char *) const;

	long find(char ) const;

	long find_back(char ) const;
};


#endif