ObjFW  Check-in [9d93300b4e]

Overview
Comment:Add -[readLineWithEncoding:] to read in the specified encoding.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9d93300b4ee9b5e802db2adb2d59bc904a87b1b0afd58a984cf12c66fbf47859
User & Date: js on 2009-07-21 23:25:52
Other Links: manifest | tags
Context
2009-07-23
15:39
Include stdint.h in OFMacros.h. check-in: 15a5433649 user: js tags: trunk
2009-07-21
23:25
Add -[readLineWithEncoding:] to read in the specified encoding. check-in: 9d93300b4e user: js tags: trunk
16:08
Fix wrong order of retain / release. check-in: a7b6d69e14 user: js tags: trunk
Changes

Modified src/OFStream.h from [937b4422c4] to [d7a78fa6d9].

44
45
46
47
48
49
50












51
52
53
54
55
56
57
 * before and optionally get the cache before clearing it!
 *
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLine;













/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */







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







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
 * before and optionally get the cache before clearing it!
 *
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLine;

/**
 * Read with the specified encoding until a newline, \\0 or end of stream
 * occurs.
 *
 * If you want to use readNBytes afterwards again, you have to clear the cache
 * before and optionally get the cache before clearing it!
 *
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLineWithEncoding: (enum of_string_encoding)encoding;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */

Modified src/OFStream.m from [a24c081951] to [4d39d4c3d2].

61
62
63
64
65
66
67





68
69
70
71
72
73
74
75
76
77

78
79
80
81
82
83
84
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (OFString*)readLine
{





	size_t i, len;
	char *ret_c, *tmp, *tmp2;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
		for (i = 0; i < cache_len; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				ret = [OFString stringWithCString: cache

							   length: i];

				tmp = [self allocMemoryWithSize: cache_len -
								 i - 1];
				memcpy(tmp, cache + i + 1, cache_len - i - 1);

				[self freeMemory: cache];







>
>
>
>
>










>







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
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (enum of_string_encoding)encoding
{
	size_t i, len;
	char *ret_c, *tmp, *tmp2;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
		for (i = 0; i < cache_len; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: i];

				tmp = [self allocMemoryWithSize: cache_len -
								 i - 1];
				memcpy(tmp, cache + i + 1, cache_len - i - 1);

				[self freeMemory: cache];
97
98
99
100
101
102
103

104
105
106
107
108
109
110
		if ([self atEndOfStream]) {
			[self freeMemory: tmp];

			if (cache == NULL)
				return nil;

			ret = [OFString stringWithCString: cache

						   length: cache_len];

			[self freeMemory: cache];
			cache = NULL;
			cache_len = 0;

			return ret;







>







103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
		if ([self atEndOfStream]) {
			[self freeMemory: tmp];

			if (cache == NULL)
				return nil;

			ret = [OFString stringWithCString: cache
						 encoding: encoding
						   length: cache_len];

			[self freeMemory: cache];
			cache = NULL;
			cache_len = 0;

			return ret;
156
157
158
159
160
161
162
163

164
165
166
167
168
169
170
					cache = NULL;
					cache_len = 0;
				}
				[self freeMemory: tmp];

				@try {
					ret = [OFString
					    stringWithCString: ret_c];

				} @finally {
					[self freeMemory: ret_c];
				}
				return ret;
			}
		}








|
>







163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
					cache = NULL;
					cache_len = 0;
				}
				[self freeMemory: tmp];

				@try {
					ret = [OFString
					    stringWithCString: ret_c
						     encoding: encoding];
				} @finally {
					[self freeMemory: ret_c];
				}
				return ret;
			}
		}