ObjFW  Check-in [4c4608fbba]

Overview
Comment:Add of_asprintf and allow %@ in format strings.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4c4608fbbaba40e0e009199c7b183935ce50ad8dcf8615d905c38776a50d1f0b
User & Date: js on 2011-01-29 19:01:32
Other Links: manifest | tags
Context
2011-01-29
19:16
Use %@ where it is useful. check-in: d0eace5cd1 user: js tags: trunk
19:01
Add of_asprintf and allow %@ in format strings. check-in: 4c4608fbba user: js tags: trunk
2011-01-24
20:20
ObjC++ needs those defines for <stdin.h>. check-in: 5ad479153e user: js tags: trunk
Changes

Modified src/Makefile from [b73e183993] to [25fedfa6c0].

38
39
40
41
42
43
44

45
46
47
48
49
50
51
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\
       OFURL.m			\
       OFXMLAttribute.m		\
       OFXMLElement.m		\
       OFXMLElementBuilder.m	\
       OFXMLParser.m		\

       unicode.m

INCLUDES := ${SRCS:.m=.h}	\
	    OFCollection.h	\
	    ObjFW.h		\
	    asprintf.h		\
	    ${ATOMIC_H}		\







>







38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\
       OFURL.m			\
       OFXMLAttribute.m		\
       OFXMLElement.m		\
       OFXMLElementBuilder.m	\
       OFXMLParser.m		\
       of_asprintf.m		\
       unicode.m

INCLUDES := ${SRCS:.m=.h}	\
	    OFCollection.h	\
	    ObjFW.h		\
	    asprintf.h		\
	    ${ATOMIC_H}		\

Modified src/OFMutableString.m from [8301473e4d] to [751ced34bb].

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"

@implementation OFMutableString
- (void)_applyTable: (const of_unichar_t* const[])table
	   withSize: (size_t)table_size
{
	of_unichar_t c;







|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"

#import "of_asprintf.h"
#import "unicode.h"

@implementation OFMutableString
- (void)_applyTable: (const of_unichar_t* const[])table
	   withSize: (size_t)table_size
{
	of_unichar_t c;
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
253
254
255
256
	va_end(args);
}

- (void)appendFormat: (OFString*)fmt
       withArguments: (va_list)args
{
	char *t;


	if (fmt == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((vasprintf(&t, [fmt cString], args)) == -1) {
		/*
		 * This is only the most likely error to happen. Unfortunately,
		 * there is no good way to check what really happened.
		 */
		@throw [OFOutOfMemoryException newWithClass: isa];

	}

	@try {
		[self appendCString: t];

	} @finally {
		free(t);
	}
}

- (void)prependString: (OFString*)str
{







>





|
<
<
<
<
|
>
|
<

|
>







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
253
254
	va_end(args);
}

- (void)appendFormat: (OFString*)fmt
       withArguments: (va_list)args
{
	char *t;
	int len;

	if (fmt == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((len = of_vasprintf(&t, [fmt cString], args)) == -1)




		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];


	@try {
		[self appendCString: t
			 withLength: len];
	} @finally {
		free(t);
	}
}

- (void)prependString: (OFString*)str
{

Modified src/OFStream.m from [c9c58ea96b] to [91d5f2f808].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#import "OFStream.h"
#import "OFString.h"
#import "OFDataArray.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"

@implementation OFStream
#ifndef _WIN32
+ (void)initialize
{
	if (self == [OFStream class])
		signal(SIGPIPE, SIG_IGN);







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#import "OFStream.h"
#import "OFString.h"
#import "OFDataArray.h"
#import "OFExceptions.h"
#import "macros.h"

#import "of_asprintf.h"

@implementation OFStream
#ifndef _WIN32
+ (void)initialize
{
	if (self == [OFStream class])
		signal(SIGPIPE, SIG_IGN);
672
673
674
675
676
677
678
679
680

681
682
683
684
685
686
687
688
689
690
691
692

693
694
695
696
697
698
699

	return ret;
}

- (size_t)writeFormat: (OFString*)fmt
	withArguments: (va_list)args
{
	size_t len;
	char *t;


	if (fmt == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((len = vasprintf(&t, [fmt cString], args)) == -1) {
		/*
		 * This is only the most likely error to happen. Unfortunately,
		 * there is no good way to check what really happened.
		 */
		@throw [OFOutOfMemoryException newWithClass: isa];
	}


	@try {
		return [self writeNBytes: len
			      fromBuffer: t];
	} @finally {
		free(t);
	}







<

>





|
<
<
<
<
|
<
>







672
673
674
675
676
677
678

679
680
681
682
683
684
685
686




687

688
689
690
691
692
693
694
695

	return ret;
}

- (size_t)writeFormat: (OFString*)fmt
	withArguments: (va_list)args
{

	char *t;
	int len;

	if (fmt == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((len = of_vasprintf(&t, [fmt cString], args)) == -1)




		@throw [OFInvalidArgumentException newWithClass: isa

						       selector: _cmd];

	@try {
		return [self writeNBytes: len
			      fromBuffer: t];
	} @finally {
		free(t);
	}

Modified src/OFString.m from [a9943514ef] to [d45226b27d].

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#import "OFString.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"

extern const uint16_t of_iso_8859_15[256];
extern const uint16_t of_windows_1252[256];

/* References for static linking */
void _references_to_categories_of_OFString()







|







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#import "OFString.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "of_asprintf.h"
#import "unicode.h"

extern const uint16_t of_iso_8859_15[256];
extern const uint16_t of_windows_1252[256];

/* References for static linking */
void _references_to_categories_of_OFString()
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

482
483
484
485
486
487
488
489
490
491

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
{
	self = [super init];

	@try {
		int t;

		if (fmt == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((t = vasprintf(&string, [fmt cString], args)) == -1)
			/*
			 * This is only the most likely error to happen.
			 * Unfortunately, there is no good way to check what
			 * really happened.
			 */
			@throw [OFOutOfMemoryException newWithClass: isa];


		@try {
			length = t;

			switch (of_string_check_utf8(string, length)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException







|





|
<
<
<
<
<
|
>


|







462
463
464
465
466
467
468
469
470
471
472
473
474
475





476
477
478
479
480
481
482
483
484
485
486
487

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
{
	self = [super init];

	@try {
		int len;

		if (fmt == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((len = of_vasprintf(&string, [fmt cString], args)) == -1)





			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		@try {
			length = len;

			switch (of_string_check_utf8(string, length)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException

Modified src/OFXMLElement.m from [5cd0b460a8] to [866cd32b8c].

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	@try {
		name = [name_ copy];
		ns = [ns_ copy];

		if (stringval != nil) {
			OFAutoreleasePool *pool;

			pool = [[OFAutoreleasePool alloc] init];;
			[self addChild:
			    [OFXMLElement elementWithCharacters: stringval]];
			[pool release];
		}

		namespaces = [[OFMutableDictionary alloc]
		    initWithKeysAndObjects:







|







111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	@try {
		name = [name_ copy];
		ns = [ns_ copy];

		if (stringval != nil) {
			OFAutoreleasePool *pool;

			pool = [[OFAutoreleasePool alloc] init];
			[self addChild:
			    [OFXMLElement elementWithCharacters: stringval]];
			[pool release];
		}

		namespaces = [[OFMutableDictionary alloc]
		    initWithKeysAndObjects:

Modified src/ObjFW.h from [f49c5a28b5] to [ec1ce05ff2].

66
67
68
69
70
71
72


#ifdef OF_THREADS
# import "OFThread.h"
# import "threading.h"
#endif

#import "asprintf.h"








>
66
67
68
69
70
71
72
73

#ifdef OF_THREADS
# import "OFThread.h"
# import "threading.h"
#endif

#import "asprintf.h"
#import "of_asprintf.h"

Added src/of_asprintf.h version [8c49ad9e92].













































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include <stdarg.h>

#import "macros.h"

extern int of_asprintf(char**, const char*, ...);
extern int of_vasprintf(char**, const char*, va_list);

Added src/of_asprintf.m version [939c56bbd9].



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <wchar.h>

#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "asprintf.h"

#define MAX_SUBFMT_LEN 64

struct context {
	const char *fmt;
	size_t fmt_len;
	char subfmt[MAX_SUBFMT_LEN + 1];
	size_t subfmt_len;
	va_list args;
	char *buf;
	size_t buf_len;
	size_t i, last;
	enum {
		STATE_STRING,
		STATE_FORMAT_FLAGS,
		STATE_FORMAT_FIELD_WIDTH,
		STATE_FORMAT_LENGTH_MODIFIER,
		STATE_FORMAT_CONVERSION_SPECIFIER
	} state;
	enum {
		LENGTH_MODIFIER_NONE,
		LENGTH_MODIFIER_HH,
		LENGTH_MODIFIER_H,
		LENGTH_MODIFIER_L,
		LENGTH_MODIFIER_LL,
		LENGTH_MODIFIER_J,
		LENGTH_MODIFIER_Z,
		LENGTH_MODIFIER_T,
		LENGTH_MODIFIER_CAPITAL_L
	} len_mod;
};

static bool
append_str(struct context *ctx, const char *astr, size_t astr_len)
{
	char *nbuf;

	if (astr_len == 0)
		return true;

	if (ctx->buf == NULL) {
		if ((ctx->buf = malloc(astr_len + 1)) == NULL)
			return false;

		memcpy(ctx->buf, astr, astr_len);
		ctx->buf_len = astr_len;

		return true;
	}

	if ((nbuf = realloc(ctx->buf, ctx->buf_len + astr_len + 1)) == NULL)
		return false;

	memcpy(nbuf + ctx->buf_len, astr, astr_len);

	ctx->buf = nbuf;
	ctx->buf_len += astr_len;

	return true;
}

static bool
append_subfmt(struct context *ctx, const char *asubfmt, size_t asubfmt_len)
{
	if (ctx->subfmt_len + asubfmt_len > MAX_SUBFMT_LEN)
		return false;

	memcpy(ctx->subfmt + ctx->subfmt_len, asubfmt, asubfmt_len);
	ctx->subfmt_len += asubfmt_len;
	ctx->subfmt[ctx->subfmt_len] = 0;

	return true;
}

static bool
state_string(struct context *ctx)
{
	if (ctx->fmt[ctx->i] == '%') {
		if (ctx->i > 0)
			if (!append_str(ctx, ctx->fmt + ctx->last,
			    ctx->i - ctx->last))
				return false;

		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;

		ctx->last = ctx->i + 1;
		ctx->state = STATE_FORMAT_FLAGS;
	}

	return true;
}

static bool
state_format_flags(struct context *ctx)
{
	switch (ctx->fmt[ctx->i]) {
	case '-':
	case '+':
	case ' ':
	case '#':
	case '0':
		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;

		break;
	default:
		ctx->state = STATE_FORMAT_FIELD_WIDTH;
		ctx->i--;

		break;
	}

	return true;
}

static bool
state_format_field_width(struct context *ctx)
{
	if ((ctx->fmt[ctx->i] >= '0' && ctx->fmt[ctx->i] <= '9') ||
	    ctx->fmt[ctx->i] == '*' || ctx->fmt[ctx->i] == '.') {
		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;
	} else {
		ctx->state = STATE_FORMAT_LENGTH_MODIFIER;
		ctx->i--;
	}

	return true;
}

static bool
state_format_length_modifier(struct context *ctx)
{
	/* Only one allowed */
	switch (ctx->fmt[ctx->i]) {
	case 'h': /* and also hh */
		if (ctx->fmt_len > ctx->i + 1 && ctx->fmt[ctx->i + 1] == 'h') {
			if (!append_subfmt(ctx, ctx->fmt + ctx->i, 2))
				return false;

			ctx->i++;
			ctx->len_mod = LENGTH_MODIFIER_HH;
		} else {
			if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
				return false;

			ctx->len_mod = LENGTH_MODIFIER_H;
		}

		break;
	case 'l': /* and also ll */
		if (ctx->fmt_len > ctx->i + 1 && ctx->fmt[ctx->i + 1] == 'l') {
			if (!append_subfmt(ctx, ctx->fmt + ctx->i, 2))
				return false;

			ctx->i++;
			ctx->len_mod = LENGTH_MODIFIER_LL;
		} else {
			if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
				return false;

			ctx->len_mod = LENGTH_MODIFIER_L;
		}

		break;
	case 'j':
		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;

		ctx->len_mod = LENGTH_MODIFIER_J;

		break;
	case 'z':
		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;

		ctx->len_mod = LENGTH_MODIFIER_Z;

		break;
	case 't':
		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;

		ctx->len_mod = LENGTH_MODIFIER_T;

		break;
	case 'L':
		if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
			return false;

		ctx->len_mod = LENGTH_MODIFIER_CAPITAL_L;

		break;
	default:
		ctx->i--;

		break;
	}

	ctx->state = STATE_FORMAT_CONVERSION_SPECIFIER;
	return true;
}

static bool
state_format_conversion_specifier(struct context *ctx)
{
	char *tmp = NULL;
	int tmp_len = 0;

	if (!append_subfmt(ctx, ctx->fmt + ctx->i, 1))
		return false;

	switch (ctx->fmt[ctx->i]) {
	case '@':;
		OFAutoreleasePool *pool;

		ctx->subfmt[ctx->subfmt_len - 1] = 's';

		@try {
			pool = [[OFAutoreleasePool alloc] init];
		} @catch (id e) {
			[e release];
			return false;
		}

		@try {
			const char *desc =
			    [[va_arg(ctx->args, id) description] cString];

			tmp_len = asprintf(&tmp, ctx->subfmt, desc);
		} @catch (id e) {
			[e release];
			return false;
		} @finally {
			[pool release];
		}

		break;
	case 'd':
	case 'i':
		switch (ctx->len_mod) {
		case LENGTH_MODIFIER_NONE:
		case LENGTH_MODIFIER_HH:
		case LENGTH_MODIFIER_H:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, int));
			break;
		case LENGTH_MODIFIER_L:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, long));
			break;
		case LENGTH_MODIFIER_LL:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, long long));
			break;
		case LENGTH_MODIFIER_J:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, intmax_t));
			break;
		case LENGTH_MODIFIER_Z:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, ssize_t));
			break;
		case LENGTH_MODIFIER_T:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, ptrdiff_t));
			break;
		default:
			return false;
		}

		break;
	case 'o': case 'u': case 'x': case 'X':
		switch (ctx->len_mod) {
		case LENGTH_MODIFIER_NONE:
		case LENGTH_MODIFIER_HH:
		case LENGTH_MODIFIER_H:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, unsigned int));
			break;
		case LENGTH_MODIFIER_L:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, unsigned long));
			break;
		case LENGTH_MODIFIER_LL:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, unsigned long long));
			break;
		case LENGTH_MODIFIER_J:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, uintmax_t));
			break;
		case LENGTH_MODIFIER_Z:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, size_t));
			break;
		case LENGTH_MODIFIER_T:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, ptrdiff_t));
			break;
		default:
			return false;
		}

		break;
	case 'f':
	case 'F':
	case 'e':
	case 'E':
	case 'g':
	case 'G':
	case 'a':
	case 'A':
		switch (ctx->len_mod) {
		case LENGTH_MODIFIER_NONE:
		case LENGTH_MODIFIER_L:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, double));
			break;
		case LENGTH_MODIFIER_CAPITAL_L:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, long double));
			break;
		default:
			return false;
		}

		break;
	case 'c':
		switch (ctx->len_mod) {
		case LENGTH_MODIFIER_NONE:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, int));
			break;
		case LENGTH_MODIFIER_L:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, wint_t));
			break;
		default:
			return false;
		}

		break;
	case 's':
		switch (ctx->len_mod) {
		case LENGTH_MODIFIER_NONE:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, const char*));
			break;
		case LENGTH_MODIFIER_L:
			tmp_len = asprintf(&tmp, ctx->subfmt,
			    va_arg(ctx->args, const wchar_t*));
			break;
		default:
			return false;
		}

		break;
	case 'p':
		if (ctx->len_mod != LENGTH_MODIFIER_NONE)
			return false;

		tmp_len = asprintf(&tmp, ctx->subfmt, va_arg(ctx->args, void*));

		break;
	case 'n':
		switch (ctx->len_mod) {
		case LENGTH_MODIFIER_NONE:
			*va_arg(ctx->args, int*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_HH:
			*va_arg(ctx->args, signed char*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_H:
			*va_arg(ctx->args, short*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_L:
			*va_arg(ctx->args, long*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_LL:
			*va_arg(ctx->args, long long*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_J:
			*va_arg(ctx->args, intmax_t*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_Z:
			*va_arg(ctx->args, size_t*) = ctx->buf_len;
			break;
		case LENGTH_MODIFIER_T:
			*va_arg(ctx->args, ptrdiff_t*) = ctx->buf_len;
			break;
		default:
			return false;
		}

		break;
	case '%':
		if (ctx->len_mod != LENGTH_MODIFIER_NONE)
			return false;

		if (!append_str(ctx, "%", 1))
			return false;

		break;
	default:
		return false;
	}

	if (tmp_len == -1)
		return false;

	if (tmp != NULL) {
		if (!append_str(ctx, tmp, tmp_len)) {
			free(tmp);
			return false;
		}

		free(tmp);
	}

	memset(ctx->subfmt, 0, MAX_SUBFMT_LEN);
	ctx->subfmt_len = 0;
	ctx->len_mod = LENGTH_MODIFIER_NONE;

	ctx->last = ctx->i + 1;
	ctx->state = STATE_STRING;

	return true;
}

static bool (*states[])(struct context*) = {
	state_string,
	state_format_flags,
	state_format_field_width,
	state_format_length_modifier,
	state_format_conversion_specifier
};

int
of_vasprintf(char **ret, const char *fmt, va_list args)
{
	struct context ctx;

	ctx.fmt = fmt;
	ctx.fmt_len = strlen(fmt);
	memset(ctx.subfmt, 0, MAX_SUBFMT_LEN + 1);
	ctx.subfmt_len = 0;
	va_copy(ctx.args, args);
	ctx.buf = NULL;
	ctx.buf_len = 0;
	ctx.last = 0;
	ctx.state = STATE_STRING;
	ctx.len_mod = LENGTH_MODIFIER_NONE;

	for (ctx.i = 0; ctx.i < ctx.fmt_len; ctx.i++) {
		if (!states[ctx.state](&ctx)) {
			if (ctx.buf != NULL)
				free(ctx.buf);

			return -1;
		}
	}

	if (ctx.state != STATE_STRING) {
		if (ctx.buf != NULL)
			free(ctx.buf);

		return -1;
	}

	if (!append_str(&ctx, ctx.fmt + ctx.last, ctx.fmt_len - ctx.last)) {
		free(ctx.buf);
		return -1;
	}

	ctx.buf[ctx.buf_len] = 0;

	*ret = ctx.buf;
	return ctx.buf_len;
}

int
of_asprintf(char **ret, const char *fmt, ...)
{
	va_list args;
	int r;

	va_start(args, fmt);
	r = of_vasprintf(ret, fmt, args);
	va_end(args);

	return r;
}