#######################################################################################################################* *--------------------------------------------------Read/Write strings-----------------------------------------------------* *#########################################################################################################################*/
| 463 | *--------------------------------------------------Read/Write strings-----------------------------------------------------* |
| 464 | *#########################################################################################################################*/ |
| 465 | cc_result Stream_ReadLine(struct Stream* s, cc_string* text) { |
| 466 | cc_bool readAny = false; |
| 467 | cc_codepoint cp; |
| 468 | cc_result res; |
| 469 | |
| 470 | cc_uint8 tmp[8]; |
| 471 | cc_uint32 len; |
| 472 | |
| 473 | text->length = 0; |
| 474 | for (;;) { |
| 475 | len = 0; |
| 476 | |
| 477 | /* Read a UTF8 character from the stream */ |
| 478 | /* (in most cases it's just one byte) */ |
| 479 | do { |
| 480 | if ((res = s->ReadU8(s, &tmp[len]))) break; |
| 481 | len++; |
| 482 | } while (!Convert_Utf8ToCodepoint(&cp, tmp, len)); |
| 483 | |
| 484 | if (res == ERR_END_OF_STREAM) break; |
| 485 | if (res) return res; |
| 486 | |
| 487 | readAny = true; |
| 488 | /* Handle \r\n or \n line endings */ |
| 489 | if (cp == '\r') continue; |
| 490 | if (cp == '\n') return 0; |
| 491 | |
| 492 | /* ignore byte order mark */ |
| 493 | if (cp == 0xFEFF) continue; |
| 494 | String_Append(text, Convert_CodepointToCP437(cp)); |
| 495 | } |
| 496 | return readAny ? 0 : ERR_END_OF_STREAM; |
| 497 | } |
| 498 | |
| 499 | cc_result Stream_WriteLine(struct Stream* s, cc_string* text) { |
| 500 | cc_uint8 buffer[2048 + 10]; /* some space for newline */ |
no test coverage detected