Just like stdio fgetc(), except works on a CFILE Returns a char or EOF
| 564 | // Just like stdio fgetc(), except works on a CFILE |
| 565 | // Returns a char or EOF |
| 566 | int cfgetc(CFILE *cfp) { |
| 567 | int c; |
| 568 | static uint8_t ch[3] = "\0\0"; |
| 569 | if (cfp->position >= cfp->size) |
| 570 | return EOF; |
| 571 | |
| 572 | fread(ch, sizeof(char), 1, cfp->file); |
| 573 | c = ch[0]; |
| 574 | // c = getc( cfp->file ); |
| 575 | if (cfeof(cfp)) |
| 576 | c = EOF; |
| 577 | if (c != EOF) { |
| 578 | cfp->position++; |
| 579 | // do special newline handling for text files: |
| 580 | // if CR or LF by itself, return as newline |
| 581 | // if CR/LF pair, return as newline |
| 582 | if (cfp->flags & CFF_TEXT) { |
| 583 | if (c == 10) // return LF as newline |
| 584 | c = '\n'; |
| 585 | else if (c == 13) { // check for CR/LF pair |
| 586 | fread(ch, sizeof(char), 1, cfp->file); |
| 587 | int cc = ch[0]; // getc(cfp->file); |
| 588 | // if (cc != EOF) { |
| 589 | if (!cfeof(cfp)) { |
| 590 | if (cc == 10) // line feed? |
| 591 | cfp->position++; //..yes, so swallow it |
| 592 | else { |
| 593 | // ungetc(cc,cfp->file); //..no, so put it back |
| 594 | fseek(cfp->file, -1, SEEK_CUR); |
| 595 | } |
| 596 | } |
| 597 | c = '\n'; // return CR or CR/LF pair as newline |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | return c; |
| 602 | } |
| 603 | // Just like stdio fseek(), except works on a CFILE |
| 604 | int cfseek(CFILE *cfp, long offset, int where) { |
| 605 | int c; |
no test coverage detected