Overview
Comment: | Make OFStream caching invisible to the programmer. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
fc0b2500b9bd2aba1fb4a623f6af9e5e |
User & Date: | js on 2009-12-03 18:50:41 |
Other Links: | manifest | tags |
Context
2009-12-05
| ||
10:45 | Move LDFLAGS for rpath in objfw-config to a separate --rpath flag. check-in: d4a60cdef5 user: js tags: trunk | |
2009-12-03
| ||
18:50 | Make OFStream caching invisible to the programmer. check-in: fc0b2500b9 user: js tags: trunk | |
11:51 | Add -[componentsJoinedByString:] to OFArray. check-in: fb651428aa user: js tags: trunk | |
Changes
Modified TODO from [dbceb3bf95] to [83c7dabc44].
1 2 3 | Test if autorelease pool releases everything correctly when thread is ended Make strings \0-safe | < | 1 2 3 4 5 6 7 8 9 10 | Test if autorelease pool releases everything correctly when thread is ended Make strings \0-safe Cleanup OFXMLElement Add finalization to OFXMLParser Add namespaces to OFXMLParser Add support for different encodings to OFXMLParser |
︙ | ︙ |
Modified src/OFExceptions.h from [d069b97d96] to [1cdedb1af1].
︙ | ︙ | |||
275 276 277 278 279 280 281 | /** * An OFException indicating a read or write to the file failed. */ @interface OFReadOrWriteFailedException: OFException { size_t req_size; | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 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 | /** * An OFException indicating a read or write to the file failed. */ @interface OFReadOrWriteFailedException: OFException { size_t req_size; int err; } /** * \param class_ The class of the object which caused the exception * \param size The requested size of the data that couldn't be read / written * \return A new open file failed exception */ + newWithClass: (Class)class_ size: (size_t)size; /** * Initializes an already allocated read or write failed exception. * * \param class_ The class of the object which caused the exception * \param size The requested size of the data that couldn't be read / written * \return A new open file failed exception */ - initWithClass: (Class)class_ size: (size_t)size; /** * \return The errno from when the exception was created */ - (int)errNo; /** * \return The requested size of the data that couldn't be read / written */ - (size_t)requestedSize; @end /** * An OFException indicating a read to the file failed. */ @interface OFReadFailedException: OFReadOrWriteFailedException {} @end |
︙ | ︙ |
Modified src/OFExceptions.m from [58c24c0199] to [528afc5c1f].
︙ | ︙ | |||
402 403 404 405 406 407 408 | return mode; } @end @implementation OFReadOrWriteFailedException + newWithClass: (Class)class__ size: (size_t)size | < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < | < < < < | | < | < < < < | | | 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 | return mode; } @end @implementation OFReadOrWriteFailedException + newWithClass: (Class)class__ size: (size_t)size { return [[self alloc] initWithClass: class__ size: size]; } - initWithClass: (Class)class__ { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - initWithClass: (Class)class__ size: (size_t)size { self = [super initWithClass: class__]; req_size = size; /* FIXME: We need something that works for subclasses as well */ if (class__ == [OFTCPSocket class]) err = GET_SOCK_ERR; else err = GET_ERR; return self; } - (int)errNo { return err; } - (size_t)requestedSize { return req_size; } @end @implementation OFReadFailedException - (OFString*)string { if (string != nil) return string;; string = [[OFString alloc] initWithFormat: @"Failed to read %zu bytes in class %s! " ERRFMT, req_size, [class_ className], ERRPARAM]; return string; } @end @implementation OFWriteFailedException - (OFString*)string { if (string != nil) return string; string = [[OFString alloc] initWithFormat: @"Failed to write %zu bytes in class %s! " ERRFMT, req_size, [class_ className], ERRPARAM]; return string; } @end @implementation OFChangeFileModeFailedException + newWithClass: (Class)class__ |
︙ | ︙ |
Modified src/OFFile.h from [9d72f8d05d] to [87163d3271].
︙ | ︙ | |||
122 123 124 125 126 127 128 | /** * Initializes an already allocated OFFile. * * \param fp A file pointer, returned from for example fopen(). * It is not closed when the OFFile object is deallocated! */ - initWithFilePointer: (FILE*)fp; | < < < < < < < < < < < < < < < < < < < < < < < < < < | 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | /** * Initializes an already allocated OFFile. * * \param fp A file pointer, returned from for example fopen(). * It is not closed when the OFFile object is deallocated! */ - initWithFilePointer: (FILE*)fp; @end @interface OFFileSingleton: OFFile @end /// An OFFile object for stdin. extern OFFile *of_stdin; /// An OFFile object for stdout. extern OFFile *of_stdout; /// An OFFile object for stderr. extern OFFile *of_stderr; |
Modified src/OFFile.m from [9c9c1d09c4] to [ea32f092c7].
︙ | ︙ | |||
186 187 188 189 190 191 192 | { if (close && fp != NULL) fclose(fp); [super dealloc]; } | | < < < < < < < < < < < < < < < < | | < < < < < < < < < | < | | | < < | < | | > > | | | < < | 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 | { if (close && fp != NULL) fclose(fp); [super dealloc]; } - (BOOL)atEndOfStreamWithoutCache { if (fp == NULL) return YES; return (feof(fp) == 0 ? NO : YES); } - (size_t)readNBytesWithoutCache: (size_t)size intoBuffer: (char*)buf { size_t ret; if (fp == NULL || feof(fp) || ((ret = fread(buf, 1, size, fp)) == 0 && size != 0 && !feof(fp))) @throw [OFReadFailedException newWithClass: isa size: size]; return ret; } - (size_t)writeNBytes: (size_t)size fromBuffer: (const char*)buf { size_t ret; if (fp == NULL || feof(fp) || ((ret = fwrite(buf, 1, size, fp)) < size && size != 0)) @throw [OFWriteFailedException newWithClass: isa size: size]; return ret; } - close { fclose(fp); fp = NULL; |
︙ | ︙ |
Modified src/OFSocket.m from [d65c17ca33] to [866349c818].
︙ | ︙ | |||
46 47 48 49 50 51 52 | sock = INVALID_SOCKET; saddr = NULL; return self; } | | | | | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | sock = INVALID_SOCKET; saddr = NULL; return self; } - (BOOL)atEndOfStreamWithoutCache { return eos; } - (size_t)readNBytesWithoutCache: (size_t)size intoBuffer: (char*)buf { ssize_t ret; if (sock == INVALID_SOCKET || eos) @throw [OFNotConnectedException newWithClass: isa]; if ((ret = recv(sock, buf, size, 0)) < 0) |
︙ | ︙ |
Modified src/OFStream.h from [f0bc557550] to [15f483123e].
︙ | ︙ | |||
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | @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; /** | > > > > > > > > > > > > > > > > > > > > > | | | | > > | > > > | > > < < < | 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 | @interface OFStream: OFObject { char *cache; size_t cache_len; } /** * Returns a boolean whether the end of the stream has been reached. * * IMPORTANT: Do *NOT* override this in subclasses! Override * atEndOfStreamWithoutCache instead, as otherwise, you *WILL* break caching and * thus get broken results! * * \return A boolean whether the end of the stream has been reached */ - (BOOL)atEndOfStream; /** * Returns a boolean whether the end of the stream has been reached without * looking at the cache. * * IMPORTANT: Do *NOT* use this! Use atEndOfCache instead, as this is *ONLY* * for being overriden in subclasses! * * \return A boolean whether the end of the stream has been reached */ - (BOOL)atEndOfStreamWithoutCache; /** * Reads from the stream into a buffer. * * IMPORTANT: Do *NOT* override this in subclasses! Override * readNBytesWithoutCache:intoBuffer: instead, as otherwise, you *WILL* break * caching and thus get broken results! * * \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; /** * Reads from the stream into a buffer without looking at the cache. * * IMPORTANT: Do *NOT* use this! Use readNBytes:intoBuffer: instead, as this is * *ONLY* for being overriden in subclasses! * * \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)readNBytesWithoutCache: (size_t)size intoBuffer: (char*)buf; /** * Read until a newline, \\0 or end of stream occurs. * * \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. * * \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. |
︙ | ︙ | |||
78 79 80 81 82 83 84 | * Writes a string into the stream, without the trailing zero. * * \param str The string from which the data is written to the stream * \return The number of bytes written */ - (size_t)writeString: (OFString*)str; | < < < < < < < < < < < < < < | 103 104 105 106 107 108 109 110 111 112 113 114 | * Writes a string into the stream, without the trailing zero. * * \param str The string from which the data is written to the stream * \return The number of bytes written */ - (size_t)writeString: (OFString*)str; /** * Closes the stream. */ - close; @end |
Modified src/OFStream.m from [2c5ce7298e] to [dff3658c98].
︙ | ︙ | |||
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #endif return self; } - (BOOL)atEndOfStream { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - (size_t)readNBytes: (size_t)size intoBuffer: (char*)buf { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - (OFString*)readLine { return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8]; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | #endif return self; } - (BOOL)atEndOfStream { if (cache != NULL) return NO; return [self atEndOfStreamWithoutCache]; } - (BOOL)atEndOfStreamWithoutCache { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - (size_t)readNBytes: (size_t)size intoBuffer: (char*)buf { if (cache == NULL) return [self readNBytesWithoutCache: size intoBuffer: buf]; if (size >= cache_len) { size_t ret = cache_len; memcpy(buf, cache, cache_len); [self freeMemory: cache]; cache = NULL; cache_len = 0; return ret; } else { char *tmp = [self allocMemoryWithSize: cache_len - size]; memcpy(tmp, cache + size, cache_len - size); memcpy(buf, cache, size); [self freeMemory: cache]; cache = tmp; cache_len -= size; return size; } } - (size_t)readNBytesWithoutCache: (size_t)size intoBuffer: (char*)buf { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - (OFString*)readLine { return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8]; |
︙ | ︙ | |||
96 97 98 99 100 101 102 | } } /* Read until we get a newline or \0 */ tmp = [self allocMemoryWithSize: pagesize]; for (;;) { | | | | | 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 | } } /* 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++) { |
︙ | ︙ | |||
203 204 205 206 207 208 209 | - (size_t)writeString: (OFString*)str { return [self writeNBytes: [str cStringLength] fromBuffer: [str cString]]; } | < < < < < < < < < < < < < < < < < < | 241 242 243 244 245 246 247 248 249 250 251 252 253 | - (size_t)writeString: (OFString*)str { return [self writeNBytes: [str cStringLength] fromBuffer: [str cString]]; } - close { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } @end |