ObjFW  Check-in [32d166557b]

Overview
Comment:Make -[readLineWithEncoding:] more fault tolerant.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 32d166557bb62c046665bcfc783e14212d3f573ca62c82da595eba2da4dffc2a
User & Date: js on 2010-01-04 14:04:04
Other Links: manifest | tags
Context
2010-01-04
14:52
Added tag 0.1.1-release for changeset 7d8fa75b6de6 check-in: a5403e4717 user: js tags: trunk
14:04
Make -[readLineWithEncoding:] more fault tolerant. check-in: 32d166557b user: js tags: trunk
00:18
Fix forgotten static for enumeration_mutation_handler. check-in: c46f609578 user: js tags: trunk
Changes

Modified src/OFStream.m from [dff3658c98] to [a1881748df].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
 * the packaging of this file.
 */

#include "config.h"

#include <string.h>
#include <unistd.h>


#import "OFStream.h"
#import "OFExceptions.h"
#import "OFMacros.h"

#ifdef _WIN32
#include <windows.h>







>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * the packaging of this file.
 */

#include "config.h"

#include <string.h>
#include <unistd.h>
#include <assert.h>

#import "OFStream.h"
#import "OFExceptions.h"
#import "OFMacros.h"

#ifdef _WIN32
#include <windows.h>
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
			}
		}
	}

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: pagesize];


	for (;;) {
		if ([self atEndOfStreamWithoutCache]) {
			[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;
		}

		@try {
			len = [self readNBytesWithoutCache: pagesize
						intoBuffer: tmp];
		} @catch (OFException *e) {
			[self freeMemory: tmp];
			@throw e;
		}

		/* Look if there's a newline or \0 */
		for (i = 0; i < len; i++) {
			if (OF_UNLIKELY(tmp[i] == '\n' || tmp[i] == '\0')) {

				ret_len = cache_len + i;
				@try {
					ret_c = [self
					    allocMemoryWithSize: ret_len];
				} @catch (OFException *e) {
					[self freeMemory: tmp];
					@throw e;
				}
				if (cache != NULL)
					memcpy(ret_c, cache, cache_len);
				memcpy(ret_c + cache_len, tmp, i);

				if (i < len) {
					@try {









						tmp2 = [self
						    allocMemoryWithSize: len -
									 i - 1];
					} @catch (OFException *e) {
						[self freeMemory: ret_c];
						[self freeMemory: tmp];
						@throw e;
					}
					memcpy(tmp2, tmp + i + 1, len - i - 1);


					[self freeMemory: cache];
					cache = tmp2;
					cache_len = len - i - 1;
				} else {
					[self freeMemory: cache];
					cache = NULL;
					cache_len = 0;
				}
				[self freeMemory: tmp];

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

		/* There was no newline or \0 */
		@try {
			cache = [self resizeMemory: cache
					    toSize: cache_len + len];
		} @catch (OFException *e) {
			[self freeMemory: tmp];
			@throw e;
		}

		/*
		 * It's possible that cache_len + len is 0 and thus cache was
		 * set to NULL by resizeMemory:toSize:.
		 */
		if (cache != NULL)
			memcpy(cache + cache_len, tmp, len);

		cache_len += len;
	}






}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];







>
|
|
<
<
|
|

|
|
|

|
|
|

|
|

<


<
<
<
|
<
|
|
|
>
|
<


<
<
<
|
|
|
|

<

>
>
>
>
>
>
>
>
>



<
<
<
<
<
|
>

|
|
|
|
|
|
|
|
<

<
<
<
<
<
<
<
<
|
|
|

|
<


<
<
<
|
<
|
|
|
|
|
|

|
|
>
>
>
>
>
>







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

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: pagesize];

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {


				if (cache == NULL)
					return nil;

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

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

				return ret;
			}


			len = [self readNBytesWithoutCache: pagesize
						intoBuffer: tmp];





			/* Look if there's a newline or \0 */
			for (i = 0; i < len; i++) {
				if (OF_UNLIKELY(tmp[i] == '\n' ||
				    tmp[i] == '\0')) {
					ret_len = cache_len + i;

					ret_c = [self
					    allocMemoryWithSize: ret_len];




					if (cache != NULL)
						memcpy(ret_c, cache, cache_len);
					memcpy(ret_c + cache_len, tmp, i);


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

					if (i < len) {
						tmp2 = [self
						    allocMemoryWithSize: len -
									 i - 1];





						memcpy(tmp2, tmp + i + 1,
						    len - i - 1);

						[self freeMemory: cache];
						cache = tmp2;
						cache_len = len - i - 1;
					} else {
						[self freeMemory: cache];
						cache = NULL;
						cache_len = 0;
					}










					return ret;
				}
			}

			/* There was no newline or \0 */

			cache = [self resizeMemory: cache
					    toSize: cache_len + len];





			/*
			 * It's possible that cache_len + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cache_len, tmp, len);

			cache_len += len;
		}
	} @finally {
		[self freeMemory: tmp];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];