ObjFW  Check-in [948a5c25ce]

Overview
Comment:Add -[writeFormat:withArguments:] to OFStream.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 948a5c25ce6950a55bfd12cfeb548cc7ce050362f0e4eb7ca00b1b538aa68827
User & Date: js on 2010-04-10 16:13:09
Other Links: manifest | tags
Context
2010-04-10
16:46
Improve error handling with sockets. check-in: baad47ed5b user: js tags: trunk
16:13
Add -[writeFormat:withArguments:] to OFStream. check-in: 948a5c25ce user: js tags: trunk
13:46
Improve OF_ROL. check-in: 11489e7218 user: js tags: trunk
Changes

Modified src/OFStream.h from [33ab04595d] to [d0c23abdf8].

1
2
3
4
5
6
7
8
9
10


11
12
13
14
15
16
17
/*
 * Copyright (c) 2008 - 2010
 *   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 included in
 * the packaging of this file.
 */



#import "OFObject.h"

@class OFString;
@class OFDataArray;

/**










>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (c) 2008 - 2010
 *   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 included in
 * the packaging of this file.
 */

#include <stdarg.h>

#import "OFObject.h"

@class OFString;
@class OFDataArray;

/**
262
263
264
265
266
267
268










269
270
271
272
273
 * Writes a formatted string into the stream.
 *
 * \param fmt A string used as format
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)fmt, ...;











/**
 * Closes the stream.
 */
- close;
@end







>
>
>
>
>
>
>
>
>
>





264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
 * Writes a formatted string into the stream.
 *
 * \param fmt A string used as format
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)fmt, ...;

/**
 * Writes a formatted string into the stream.
 *
 * \param fmt A string used as format
 * \param args The arguments used in the format string
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)fmt
	withArguments: (va_list)args;

/**
 * Closes the stream.
 */
- close;
@end

Modified src/OFStream.m from [86bdcc3dab] to [426e5fd1b8].

609
610
611
612
613
614
615

616











617

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

	return ret + 1;
}

- (size_t)writeFormat: (OFString*)fmt, ...
{
	va_list args;

	char *t;











	size_t len;


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

	va_start(args, fmt);
	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];
	}
	va_end(args);

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







>
|
>
>
>
>
>
>
>
>
>
>
>

>





<







<







609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635

636
637
638
639
640
641
642

643
644
645
646
647
648
649

	return ret + 1;
}

- (size_t)writeFormat: (OFString*)fmt, ...
{
	va_list args;
	size_t ret;

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

	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);
	}