File: rcstring.h

package info (click to toggle)
tardy 1.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 1,604 kB
  • ctags: 1,015
  • sloc: cpp: 9,530; sh: 4,241; makefile: 724; awk: 44
file content (175 lines) | stat: -rw-r--r-- 4,602 bytes parent folder | download | duplicates (3)
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
/*
 *	tardy - a tar post-processor
 *	Copyright (C) 1991-1995, 1998, 1999, 2002, 2004 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: interface definition for common/str.cc
 */

#ifndef COMMON_RCSTRING_H
#define COMMON_RCSTRING_H

#include <ac/stddef.h>
#include <ac/stdarg.h>
#include <main.h>

typedef unsigned long str_hash_ty;

typedef struct string_ty string_ty;
struct string_ty
{
	str_hash_ty	str_hash;
	string_ty	*str_next;
	long		str_references;
	size_t		str_length;
	char		str_text[1];
};

void str_initialize(void);
string_ty *str_from_c(const char *);
string_ty *str_n_from_c(const char *, size_t);
string_ty *str_copy(string_ty *);
void str_free(string_ty *);
string_ty *str_catenate(string_ty *, string_ty *);
string_ty *str_cat_three(string_ty *, string_ty *, string_ty *);
int str_bool(string_ty *);
string_ty *str_upcase(string_ty *);
string_ty *str_downcase(string_ty *);
void str_dump(void);
string_ty *str_field(string_ty *str, int sep, int fldnum);
void slow_to_fast(char **, string_ty **, size_t);
string_ty *str_format(char *, ...) ATTR_PRINTF(1, 2);
string_ty *str_vformat(char *, va_list);

inline bool
str_equal(string_ty *s1, string_ty *s2)
{
	return (s1 == s2);
}


/**
  * The rcstring class is used to represent a reference counted string
  * class.  It has optinal string equality time charactgerstics.
  */
class rcstring
{
public:
	~rcstring() { if (p) str_free(p); }

	rcstring() { p = 0; }

	rcstring(const rcstring &arg) : p(str_copy(arg.p)) {}

	rcstring(const char *arg) : p(str_from_c(arg)) {}

	rcstring(const char *arg, size_t len) : p(str_n_from_c(arg, len)) {}

	rcstring &operator = (const rcstring &arg) { if (p) str_free(p);
		arg.bind(); p = str_copy(arg.p); return *this; }

	const char *to_c_string() const { bind(); return p->str_text; }

	operator const char * () const { return to_c_string(); }

	size_t length() const { bind(); return p->str_length; }

	str_hash_ty hash() const { bind(); return p->str_hash; }

	int operator[] (int n) const { if (n < 0) return 0; bind();
		if (n >= (int)p->str_length) return 0;
		return (unsigned char)p->str_text[n]; }

	rcstring &operator += (const rcstring &arg) { bind(); arg.bind();
		string_ty *tmp = str_catenate(p, arg.p); str_free(p);
		p = tmp; return *this; }

	friend bool operator == (const rcstring &, const rcstring &);
	friend bool operator != (const rcstring &, const rcstring &);
	friend bool operator < (const rcstring &, const rcstring &);
	friend bool operator <= (const rcstring &, const rcstring &);
	friend bool operator > (const rcstring &, const rcstring &);
	friend bool operator >= (const rcstring &, const rcstring &);
	friend rcstring operator + (const rcstring &, const rcstring &);

private:
	mutable string_ty *p;
	void bind() const { if (!p) p = str_from_c(""); }
};


inline bool
operator == (const rcstring &lhs, const rcstring &rhs)
{
	lhs.bind();
	rhs.bind();
	return str_equal(lhs.p, rhs.p);
}


inline bool
operator == (const rcstring &lhs, const char *rhs)
{
	return (lhs == rcstring(rhs));
}


inline bool
operator == (const char *lhs, const rcstring &rhs)
{
	return (rcstring(lhs) == rhs);
}


inline bool
operator != (const rcstring &lhs, const rcstring &rhs)
{
	lhs.bind();
	rhs.bind();
	return !str_equal(lhs.p, rhs.p);
}


inline bool
operator != (const rcstring &lhs, const char *rhs)
{
	return (lhs != rcstring(rhs));
}


inline bool
operator != (const char *lhs, const rcstring &rhs)
{
	return (rcstring(lhs) != rhs);
}

bool operator < (const rcstring &, const rcstring &);
bool operator <= (const rcstring &, const rcstring &);
bool operator > (const rcstring &, const rcstring &);
bool operator >= (const rcstring &, const rcstring &);

inline rcstring
operator + (const rcstring &lhs, const rcstring &rhs)
{
	lhs.bind();
	rhs.bind();
	rcstring result;
	result.p = str_catenate(lhs.p, rhs.p);
	return result;
}

#endif /* COMMON_RCSTRING_H */