ObjFW  Check-in [486760ed2b]

Overview
Comment:Add methods to initalize from and append from formatted C strings.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 486760ed2bdc054437c21ac44663f996731296fafbea500f7c8a9ed6f5a7c4dd
User & Date: js on 2008-12-24 11:55:05
Other Links: manifest | tags
Context
2008-12-24
16:11
Let libobjc free our objects. check-in: e9aa88be06 user: js tags: trunk
11:55
Add methods to initalize from and append from formatted C strings. check-in: 486760ed2b user: js tags: trunk
2008-12-23
17:10
errno is not thread-safe on Win32, therefore use something else. check-in: cda65a1899 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [7922fd98d6] to [46aed1bedf].

133
134
135
136
137
138
139










140
141
142
143
144
145
146
- (const char*)cString;
@end

/**
 * An OFException indicating that the encoding is invalid for this object.
 */
@interface OFInvalidEncodingException: OFException {}










/**
 * \return An error message for the exception as a C string
 */
- (const char*)cString;
@end

/**







>
>
>
>
>
>
>
>
>
>







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
- (const char*)cString;
@end

/**
 * An OFException indicating that the encoding is invalid for this object.
 */
@interface OFInvalidEncodingException: OFException {}
/**
 * \return An error message for the exception as a C string
 */
- (const char*)cString;
@end

/**
 * An OFException indicating that the format is invalid.
 */
@interface OFInvalidFormatException: OFException {}
/**
 * \return An error message for the exception as a C string
 */
- (const char*)cString;
@end

/**

Modified src/OFExceptions.m from [ddf754ab44] to [2196c79e5f].

159
160
161
162
163
164
165













166
167
168
169
170
171
172
{
	if (string != NULL)
		return string;

	asprintf(&string, "The encoding is invalid for object of classs %s!",
	    [object name]);














	return string;
}
@end

@implementation OFInitializationFailedException
+ newWithClass: (Class)class_
{







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







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
{
	if (string != NULL)
		return string;

	asprintf(&string, "The encoding is invalid for object of classs %s!",
	    [object name]);

	return string;
}
@end

@implementation OFInvalidFormatException
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "The format is invalid for object of classs %s!",
	    [object name]);

	return string;
}
@end

@implementation OFInitializationFailedException
+ newWithClass: (Class)class_
{

Modified src/OFObject.h from [ac2041ef80] to [9f93088fa9].

26
27
28
29
30
31
32









33
34
35
36
37
38
39
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;










/**
 * Allocate memory and store it in the objects memory pool so it can be free'd
 * automatically when the object is free'd.
 *
 * \param size The size of the memory to allocate
 * \return A pointer to the allocated memory
 */







>
>
>
>
>
>
>
>
>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;

/**
 * Adds a pointer to the memory pool.
 * This is useful to add memory allocated by functions such as asprintf to the
 * pool so it gets freed automatically when the object is freed.
 *
 * \param ptr A pointer to add to the memory pool
 */
- addToMemoryPool: (void*)ptr;

/**
 * Allocate memory and store it in the objects memory pool so it can be free'd
 * automatically when the object is free'd.
 *
 * \param size The size of the memory to allocate
 * \return A pointer to the allocated memory
 */

Modified src/OFObject.m from [59a115d6f5] to [239d0ace71].

39
40
41
42
43
44
45























46
47
48
49
50
51
52
	if (__memchunks != NULL)
		free(__memchunks);

	free(self);

	return nil;
}
























- (void*)getMemWithSize: (size_t)size
{
	void *ptr, **memchunks;
	size_t memchunks_size;

	if (size == 0)







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







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
	if (__memchunks != NULL)
		free(__memchunks);

	free(self);

	return nil;
}

- addToMemoryPool: (void*)ptr
{
	void **memchunks;
	size_t memchunks_size;

	memchunks_size = __memchunks_size + 1;

	if (SIZE_MAX - __memchunks_size == 0 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithObject: self];

	if ((memchunks = realloc(__memchunks,
	    memchunks_size * sizeof(void*))) == NULL)
		@throw [OFNoMemException newWithObject: self
					       andSize: memchunks_size];

	__memchunks = memchunks;
	__memchunks[__memchunks_size] = ptr;
	__memchunks_size = memchunks_size;

	return ptr;
}

- (void*)getMemWithSize: (size_t)size
{
	void *ptr, **memchunks;
	size_t memchunks_size;

	if (size == 0)

Modified src/OFString.h from [eb7e2198d6] to [e5252f091b].

1
2
3
4
5
6
7
8
9
10


11
12
13
14
15
16
17
/*
 * 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 "OFObject.h"

/**
 * A class for storing and modifying strings.
 */
@interface OFString: OFObject










>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * 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 <stdarg.h>

#import "OFObject.h"

/**
 * A class for storing and modifying strings.
 */
@interface OFString: OFObject
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
 * Creates a new OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return A new OFString
 */
+ newFromCString: (const char*)str;





















/**
 * Initializes an already allocated OFString.
 *
 * \return An initialized OFString
 */
- init;

/**
 * Initializes an already allocated OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return An initialized OFString
 */
- initFromCString: (const char*)str;





















/**
 * \return The OFString as a wide C string
 */
- (const char*)cString;

/**
 * \return The length of the OFString







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















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







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
 * Creates a new OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return A new OFString
 */
+ newFromCString: (const char*)str;

/**
 * Creates a new OFString from a format C string.
 * See printf for the format syntax.
 *
 * \param fmt A C string used as format to initialize the OFString
 * \return A new OFString
 */
+ newFromFormatCString: (const char*)fmt, ...;

/**
 * Creates a new OFString from a format C string.
 * See printf for the format syntax.
 *
 * \param fmt A C string used as format to initialize the OFString
 * \param args The arguments used in the format string
 * \return A new OFString
 */
+ newFromFormatCString: (const char*)fmt
	 withArguments: (va_list)args;

/**
 * Initializes an already allocated OFString.
 *
 * \return An initialized OFString
 */
- init;

/**
 * Initializes an already allocated OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return An initialized OFString
 */
- initFromCString: (const char*)str;

/**
 * Initializes an already allocated OFString from a format C string.
 * See printf for the format syntax.
 *
 * \param fmt A C string used as format to initialize the OFString
 * \return An initialized OFString
 */
- initFromFormatCString: (const char*)fmt, ...;

/**
 * Initializes an already allocated OFString from a format C string.
 * See printf for the format syntax.
 *
 * \param fmt A C string used as format to initialize the OFString
 * \param args The arguments used in the format string
 * \return An initialized OFString
 */
- initFromFormatCString: (const char*)fmt
	  withArguments: (va_list)args;

/**
 * \return The OFString as a wide C string
 */
- (const char*)cString;

/**
 * \return The length of the OFString
94
95
96
97
98
99
100


















101
102
103
104
105
106
107
/**
 * Append a C string to the OFString.
 *
 * \param str A C string to append
 */
- appendCString: (const char*)str;



















/**
 * Reverse the OFString.
 */
- reverse;

/**
 * Upper the OFString.







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







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
/**
 * Append a C string to the OFString.
 *
 * \param str A C string to append
 */
- appendCString: (const char*)str;

/**
 * Append a formatted C string to the OFString.
 * See printf for the format syntax.
 *
 * \param fmt A format C string which generates the string to append
 */
- appendWithFormatCString: (const char*)fmt, ...;

/**
 * Append a formatted C string to the OFString.
 * See printf for the format syntax.
 *
 * \param fmt A format C string which generates the string to append
 * \param args The arguments used in the format string
 */
- appendWithFormatCString: (const char*)fmt
	     andArguments: (va_list)args;

/**
 * Reverse the OFString.
 */
- reverse;

/**
 * Upper the OFString.

Modified src/OFString.m from [37b9bc8f4c] to [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;

Modified src/asprintf.c from [4b720653cd] to [05268d6b50].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31










32
33
34
35
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int
asprintf(char **strp, const char *fmt, ...)
{
	int ret, size;
	va_list args;

	va_start(args, fmt);

	if ((size = vsnprintf(NULL, 0, fmt, args)) < 0)
		return size;
	if ((*strp = malloc((size_t)size + 1)) == NULL)
		return -1;

	ret = vsnprintf(*strp, (size_t)size + 1, fmt, args);










	va_end(args);

	return ret;
}







|

|
<
<
<






|
>
>
>
>
>
>
>
>
>
>




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
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int
vasprintf(char **strp, const char *fmt, va_list args)
{
	int size;




	if ((size = vsnprintf(NULL, 0, fmt, args)) < 0)
		return size;
	if ((*strp = malloc((size_t)size + 1)) == NULL)
		return -1;

	return vsnprintf(*strp, (size_t)size + 1, fmt, args);
}

int
asprintf(char **strp, const char *fmt, ...)
{
	int ret;
	va_list args;

	va_start(args, fmt);
	ret = vasprintf(strp, fmt, args);
	va_end(args);

	return ret;
}

Modified src/asprintf.h from [5cb7f85c1e] to [a6368fb2a3].

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

/*
 * 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.
 */

extern int asprintf(char**, const char*, ...);













>
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * 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.
 */

extern int asprintf(char**, const char*, ...);
extern int vasprintf(char**, const char*, va_list);

Modified tests/OFString/OFString.m from [fd0d2946e0] to [22509a4b2b].

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 10
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 12
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\
82
83
84
85
86
87
88








89
90
91
92
93
	CHECK_EXCEPT(s1 = [OFString newFromCString: "\xE0\x80"],
	    OFInvalidEncodingException)
	CHECK_EXCEPT(s1 = [OFString newFromCString: "\xF0\x80\x80\xC0"],
	    OFInvalidEncodingException)

	s1 = [OFString newFromCString: "äöü€𝄞"];
	CHECK(!strcmp([[s1 reverse] cString], "𝄞€üöä"))









	puts("");

	return 0;
}







>
>
>
>
>
>
>
>





82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	CHECK_EXCEPT(s1 = [OFString newFromCString: "\xE0\x80"],
	    OFInvalidEncodingException)
	CHECK_EXCEPT(s1 = [OFString newFromCString: "\xF0\x80\x80\xC0"],
	    OFInvalidEncodingException)

	s1 = [OFString newFromCString: "äöü€𝄞"];
	CHECK(!strcmp([[s1 reverse] cString], "𝄞€üöä"))
	[s1 free];

	/* Format tests */
	s1 = [OFString newFromFormatCString: "%s: %d", "test", 123];
	CHECK(!strcmp([s1 cString], "test: 123"))

	[s1 appendWithFormatCString: "%02X", 15];
	CHECK(!strcmp([s1 cString], "test: 1230F"))

	puts("");

	return 0;
}