ObjFW  Check-in [f1dae95070]

Overview
Comment:Improve -[readLine] in OFStream.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f1dae95070ff2498b0fadbec6c6db827a0953665ca42f92c2f0d22d497699ac4
User & Date: js on 2009-06-02 19:25:36
Other Links: manifest | tags
Context
2009-06-02
20:15
No need to override -[release] in OFAutoreleasePool. check-in: e6f6113b5c user: js tags: trunk
19:25
Improve -[readLine] in OFStream. check-in: f1dae95070 user: js tags: trunk
17:21
Take care of the root metaclass's super being the root class. check-in: 4c343b7841 user: js tags: trunk
Changes

Modified src/OFFile.h from [0828962522] to [61195747ef].

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return An initialized OFFile
 */
- initWithPath: (OFString*)path
       andMode: (OFString*)mode;

/**
 * \return A boolean whether the end of the file has been reached
 */
- (BOOL)atEndOfFile;

/**
 * Reads from the file into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size * nitems big!
 * \param nitems nitem The number of items to read







<
<
<
<
<







102
103
104
105
106
107
108





109
110
111
112
113
114
115
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return An initialized OFFile
 */
- initWithPath: (OFString*)path
       andMode: (OFString*)mode;






/**
 * Reads from the file into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size * nitems big!
 * \param nitems nitem The number of items to read

Modified src/OFFile.m from [a323ff36d0] to [c2c38ca999].

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
	if (fp != NULL)
		fclose(fp);

	[super dealloc];
}

- (BOOL)atEndOfFile
{
	if (fp == NULL)
		return YES;

	return (feof(fp) == 0 ? NO : YES);
}








|







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
	if (fp != NULL)
		fclose(fp);

	[super dealloc];
}

- (BOOL)atEndOfStream
{
	if (fp == NULL)
		return YES;

	return (feof(fp) == 0 ? NO : YES);
}

Modified src/OFSocket.m from [309a255317] to [4843a4b3cd].

45
46
47
48
49
50
51







52
53
54
55
56
57
58
	self = [super init];

	sock = INVALID_SOCKET;
	saddr = NULL;

	return self;
}








- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)







>
>
>
>
>
>
>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	self = [super init];

	sock = INVALID_SOCKET;
	saddr = NULL;

	return self;
}

- (BOOL)atEndOfStream
{
	/* FIXME: Implement this! */

	return NO;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)

Modified src/OFStream.h from [29bc7e1e05] to [a8f3308e3a].

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
 */
@interface OFStream: OFObject
{
	char   *cache;
	size_t cache_len;
}






/**
 * Reads from the stream into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf;

/**
 * Read until a newline or \0 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.

 */
- (OFString*)readLine;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream







>
>
>
>
>

















|
>







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
 */
@interface OFStream: OFObject
{
	char   *cache;
	size_t cache_len;
}

/**
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)atEndOfStream;

/**
 * Reads from the stream into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf;

/**
 * Read until a newline or \0 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*)readLine;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream

Modified src/OFStream.m from [3f64dd68d8] to [a25cf5ad1f].

41
42
43
44
45
46
47






48
49
50
51
52
53
54
		GetSystemInfo(&si);
		pagesize = si.dwPageSize - 1;
	}
#endif

	return self;
}







- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}







>
>
>
>
>
>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		GetSystemInfo(&si);
		pagesize = si.dwPageSize - 1;
	}
#endif

	return self;
}

- (BOOL)atEndOfStream
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}
93
94
95
96
97
98
99























100
101
102
103
104
105
106
		}
	}

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

	for (;;) {























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







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







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

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

	for (;;) {
		if ([self atEndOfStream]) {
			[self freeMemory: tmp];

			if (cache == NULL)
				return nil;

			ret_c = [self allocMemoryWithSize: cache_len + 1];
			memcpy(ret_c, cache, cache_len);
			ret_c[cache_len] = '\0';

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

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

			return ret;
		}

		@try {
			len = [self readNBytes: pagesize - 1
				    intoBuffer: tmp];
		} @catch (OFException *e) {
			[self freeMemory: tmp];
			@throw e;
		}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
				} @catch (OFException *e) {
					[self freeMemory: tmp];
					@throw e;
				}
				if (cache != NULL)
					memcpy(ret_c, cache, cache_len);
				memcpy(ret_c + cache_len, tmp, i);
				ret_c[i] = '\0';

				if (i < len) {
					@try {
						tmp2 = [self
						    allocMemoryWithSize: len -
									 i - 1];
					} @catch (OFException *e) {







|







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
				} @catch (OFException *e) {
					[self freeMemory: tmp];
					@throw e;
				}
				if (cache != NULL)
					memcpy(ret_c, cache, cache_len);
				memcpy(ret_c + cache_len, tmp, i);
				ret_c[cache_len + i] = '\0';

				if (i < len) {
					@try {
						tmp2 = [self
						    allocMemoryWithSize: len -
									 i - 1];
					} @catch (OFException *e) {
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
					cache_len = len - i - 1;
				} else {
					if (cache != NULL)
						[self freeMemory: cache];
					cache = NULL;
					cache_len = 0;
				}

				[self freeMemory: tmp];

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







<

>







168
169
170
171
172
173
174

175
176
177
178
179
180
181
182
183
					cache_len = len - i - 1;
				} else {
					if (cache != NULL)
						[self freeMemory: cache];
					cache = NULL;
					cache_len = 0;
				}

				[self freeMemory: tmp];

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

Modified tests/OFHashes/OFHashes.m from [39ae9d5ac9] to [ebf6f59419].

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	size_t len;

	OFMD5Hash  *md5  = [OFMD5Hash md5Hash];
	OFSHA1Hash *sha1 = [OFSHA1Hash sha1Hash];
	OFFile *f = [OFFile fileWithPath: @"testfile"
				 andMode: @"rb"];

	while (![f atEndOfFile]) {
		len = [f readNBytes: 64
			 intoBuffer: buf];
		[md5 updateWithBuffer: buf
			       ofSize: len];
		[sha1 updateWithBuffer: buf
				ofSize: len];
	}







|







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	size_t len;

	OFMD5Hash  *md5  = [OFMD5Hash md5Hash];
	OFSHA1Hash *sha1 = [OFSHA1Hash sha1Hash];
	OFFile *f = [OFFile fileWithPath: @"testfile"
				 andMode: @"rb"];

	while (![f atEndOfStream]) {
		len = [f readNBytes: 64
			 intoBuffer: buf];
		[md5 updateWithBuffer: buf
			       ofSize: len];
		[sha1 updateWithBuffer: buf
				ofSize: len];
	}