Differences From Artifact [86b9b6684c]:
- File src/OFStream.m — part of check-in [062a052b50] at 2010-04-17 15:46:34 on branch trunk — Don't return self where not necessary, return void or something useful. (user: js, size: 12449) [annotate] [blame] [check-ins using]
To Artifact [451931881c]:
- File
src/OFStream.m
— part of check-in
[37db8f0fb3]
at
2010-04-17 22:27:17
on branch trunk
— New ivar naming convention.
ivars are now named like this: thisIsAnIVar. If an accessor exists for
the ivar, the ivar is named like the accessor. This is required for
@property to work. (user: js, size: 12405) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
33 34 35 36 37 38 39 | self = [super init]; if (isa == [OFStream class]) @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; cache = NULL; | | | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | self = [super init]; if (isa == [OFStream class]) @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; cache = NULL; wBuffer = NULL; return self; } - (BOOL)_atEndOfStream { @throw [OFNotImplementedException newWithClass: isa |
︙ | ︙ | |||
73 74 75 76 77 78 79 | - (size_t)readNBytes: (size_t)size intoBuffer: (char*)buf { if (cache == NULL) return [self _readNBytes: size intoBuffer: buf]; | | | | | | | | | 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 | - (size_t)readNBytes: (size_t)size intoBuffer: (char*)buf { if (cache == NULL) return [self _readNBytes: size intoBuffer: buf]; if (size >= cacheLen) { size_t ret = cacheLen; memcpy(buf, cache, cacheLen); [self freeMemory: cache]; cache = NULL; cacheLen = 0; return ret; } else { char *tmp = [self allocMemoryWithSize: cacheLen - size]; memcpy(tmp, cache + size, cacheLen - size); memcpy(buf, cache, size); [self freeMemory: cache]; cache = tmp; cacheLen -= size; return size; } } - (void)readExactlyNBytes: (size_t)size intoBuffer: (char*)buf |
︙ | ︙ | |||
236 237 238 239 240 241 242 | { size_t i, len, ret_len; char *ret_c, *tmp, *tmp2; OFString *ret; /* Look if there's a line or \0 in our cache */ if (cache != NULL) { | | | | | | | | | | | 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 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 | { size_t i, len, ret_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 < cacheLen; i++) { if (OF_UNLIKELY(cache[i] == '\n' || cache[i] == '\0')) { ret_len = i; if (i > 0 && cache[i - 1] == '\r') ret_len--; ret = [OFString stringWithCString: cache encoding: encoding length: ret_len]; tmp = [self allocMemoryWithSize: cacheLen - i - 1]; if (tmp != NULL) memcpy(tmp, cache + i + 1, cacheLen - i - 1); [self freeMemory: cache]; cache = tmp; cacheLen -= i + 1; return ret; } } } /* Read until we get a newline or \0 */ tmp = [self allocMemoryWithSize: of_pagesize]; @try { for (;;) { if ([self _atEndOfStream]) { if (cache == NULL) return nil; ret_len = cacheLen; if (ret_len > 0 && cache[ret_len - 1] == '\r') ret_len--; ret = [OFString stringWithCString: cache encoding: encoding length: ret_len]; [self freeMemory: cache]; cache = NULL; cacheLen = 0; return ret; } len = [self _readNBytes: of_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 = cacheLen + i; ret_c = [self allocMemoryWithSize: ret_len]; if (cache != NULL) memcpy(ret_c, cache, cacheLen); memcpy(ret_c + cacheLen, tmp, i); if (ret_len > 0 && ret_c[ret_len - 1] == '\r') ret_len--; @try { ret = [OFString |
︙ | ︙ | |||
324 325 326 327 328 329 330 | allocMemoryWithSize: len - i - 1]; if (tmp2 != NULL) memcpy(tmp2, tmp + i + 1, len - i - 1); [self freeMemory: cache]; cache = tmp2; | | | | | | | 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | allocMemoryWithSize: len - i - 1]; if (tmp2 != NULL) memcpy(tmp2, tmp + i + 1, len - i - 1); [self freeMemory: cache]; cache = tmp2; cacheLen = len - i - 1; return ret; } } /* There was no newline or \0 */ cache = [self resizeMemory: cache toSize: cacheLen + len]; /* * It's possible that cacheLen + len is 0 and thus * cache was set to NULL by resizeMemory:toSize:. */ if (cache != NULL) memcpy(cache + cacheLen, tmp, len); cacheLen += len; } } @finally { [self freeMemory: tmp]; } /* Get rid of a warning, never reached anyway */ assert(0); |
︙ | ︙ | |||
376 377 378 379 380 381 382 | if (delim_len == 0) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; /* Look if there's something in our cache */ if (cache != NULL) { | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | if (delim_len == 0) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; /* Look if there's something in our cache */ if (cache != NULL) { for (i = 0; i < cacheLen; i++) { if (cache[i] != delim[j++]) j = 0; if (j == delim_len || cache[i] == '\0') { if (cache[i] == '\0') delim_len = 1; ret = [OFString stringWithCString: cache encoding: encoding length: i + 1 - delim_len]; tmp = [self allocMemoryWithSize: cacheLen - i - 1]; if (tmp != NULL) memcpy(tmp, cache + i + 1, cacheLen - i - 1); [self freeMemory: cache]; cache = tmp; cacheLen -= i + 1; return ret; } } } /* Read until we get the delimiter or \0 */ tmp = [self allocMemoryWithSize: of_pagesize]; @try { for (;;) { if ([self _atEndOfStream]) { if (cache == NULL) return nil; ret = [OFString stringWithCString: cache encoding: encoding length: cacheLen]; [self freeMemory: cache]; cache = NULL; cacheLen = 0; return ret; } len = [self _readNBytes: of_pagesize intoBuffer: tmp]; /* Look if there's the delimiter or \0 */ for (i = 0; i < len; i++) { if (tmp[i] != delim[j++]) j = 0; if (j == delim_len || tmp[i] == '\0') { if (tmp[i] == '\0') delim_len = 1; ret_len = cacheLen + i + 1 - delim_len; ret_c = [self allocMemoryWithSize: ret_len]; if (cache != NULL && cacheLen <= ret_len) memcpy(ret_c, cache, cacheLen); else if (cache != NULL) memcpy(ret_c, cache, ret_len); if (i >= delim_len) memcpy(ret_c + cacheLen, tmp, i + 1 - delim_len); @try { ret = [OFString stringWithCString: ret_c encoding: encoding length: ret_len]; } @finally { [self freeMemory: ret_c]; } tmp2 = [self allocMemoryWithSize: len - i - 1]; if (tmp2 != NULL) memcpy(tmp2, tmp + i + 1, len - i - 1); [self freeMemory: cache]; cache = tmp2; cacheLen = len - i - 1; return ret; } } /* Neither the delimiter nor \0 was found */ cache = [self resizeMemory: cache toSize: cacheLen + len]; /* * It's possible that cacheLen + len is 0 and thus * cache was set to NULL by resizeMemory:toSize:. */ if (cache != NULL) memcpy(cache + cacheLen, tmp, len); cacheLen += len; } } @finally { [self freeMemory: tmp]; } /* Get rid of a warning, never reached anyway */ assert(0); } - (void)bufferWrites { useWBuffer = YES; } - (void)flushWriteBuffer { if (wBuffer == NULL) return; [self _writeNBytes: wBufferLen fromBuffer: wBuffer]; [self freeMemory: wBuffer]; wBuffer = NULL; wBufferLen = 0; useWBuffer = NO; } - (size_t)writeNBytes: (size_t)size fromBuffer: (const char*)buf { if (!useWBuffer) return [self _writeNBytes: size fromBuffer: buf]; else { wBuffer = [self resizeMemory: wBuffer toSize: wBufferLen + size]; memcpy(wBuffer + wBufferLen, buf, size); wBufferLen += size; return size; } } - (void)writeInt8: (uint8_t)int8 { |
︙ | ︙ |