ObjFW  Diff

Differences From Artifact [37b9bc8f4c]:

To Artifact [7570d35c8f]:


1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"


#import <stdlib.h>
#import <string.h>
#import <ctype.h>

#ifdef HAVE_SYS_MMAN_H
#import <sys/mman.h>
#else













>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import <ctype.h>

#ifdef HAVE_SYS_MMAN_H
#import <sys/mman.h>
#else
101
102
103
104
105
106
107




















108
109
110
111
112
113
114
	return [[self alloc] init];
}

+ newFromCString: (const char*)str
{
	return [[self alloc] initFromCString: str];
}





















- init
{
	if ((self = [super init])) {
		length = 0;
		string = NULL;
		is_utf8 = NO;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	return [[self alloc] init];
}

+ newFromCString: (const char*)str
{
	return [[self alloc] initFromCString: str];
}

+ newFromFormatCString: (const char*)fmt, ...
{
	id ret;
	va_list args;

	va_start(args, fmt);
	ret = [[self alloc] initFromFormatCString: fmt
				    withArguments: args];
	va_end(args);

	return ret;
}

+ newFromFormatCString: (const char*)fmt
	 withArguments: (va_list)args
{
	return [[self alloc] initFromFormatCString: fmt
				     withArguments: args];
}

- init
{
	if ((self = [super init])) {
		length = 0;
		string = NULL;
		is_utf8 = NO;
133
134
135
136
137
138
139





















































140
141
142
143
144
145
146
				    newWithObject: self];
			}

			string = [self getMemWithSize: length + 1];
			memcpy(string, str, length + 1);
		}
	}






















































	return self;
}

- (const char*)cString
{
	return string;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
				    newWithObject: self];
			}

			string = [self getMemWithSize: length + 1];
			memcpy(string, str, length + 1);
		}
	}

	return self;
}

- initFromFormatCString: (const char*)fmt, ...
{
	id ret;
	va_list args;

	va_start(args, fmt);
	ret = [self initFromFormatCString: fmt
			    withArguments: args];
	va_end(args);

	return ret;
}

- initFromFormatCString: (const char*)fmt
	  withArguments: (va_list)args
{
	int t;

	if ((self = [super init])) {
		if (fmt == NULL)
			@throw [OFInvalidFormatException newWithObject: self];

		if ((t = vasprintf(&string, fmt, args)) == -1)
			/*
			 * This is only the most likely error to happen.
			 * Unfortunately, as errno isn't always thread-safe,
			 * there's no good way for us to find out what really
			 * happened.
			 */
			@throw [OFNoMemException newWithObject: self];
		length = t;

		switch (check_utf8(string, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			free(string);
			[super free];
			@throw [OFInvalidEncodingException newWithObject: self];
		}

		@try {
			[self addToMemoryPool: string];
		} @catch (OFException *e) {
			free(string);
			@throw e;
		}
	}

	return self;
}

- (const char*)cString
{
	return string;
194
195
196
197
198
199
200



































201
202
203
204
205
206
207
	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;




































	return self;
}

- reverse
{
	size_t i, j, len = length / 2;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- appendWithFormatCString: (const char*)fmt, ...
{
	id ret;
	va_list args;

	va_start(args, fmt);
	ret = [self appendWithFormatCString: fmt
			       andArguments: args];
	va_end(args);

	return ret;
}

- appendWithFormatCString: (const char*)fmt
	     andArguments: (va_list)args
{
	char *t;

	if (fmt == NULL)
		@throw [OFInvalidFormatException newWithObject: self];

	if ((vasprintf(&t, fmt, args)) == -1)
		/*
		 * This is only the most likely error to happen.
		 * Unfortunately, as errno isn't always thread-safe, there's
		 * no good way for us to find out what really happened.
		 */
		@throw [OFNoMemException newWithObject: self];

	[self appendCString: t];
	free(t);

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;