Reads a string from a CFILE. If the file is type binary, this function reads until a NULL or EOF is found. If the file is text, the function reads until a newline or EOF is found. The string is always written to the destination buffer null-terminated, without the newline. Parameters: buf - where the string is written n - the maximum string length, including the terminating 0 cfp - the CFILE po
| 719 | // Returns the number of bytes in the string, before the terminator |
| 720 | // Does not generate an exception on EOF |
| 721 | int cf_ReadString(char *buf, size_t n, CFILE *cfp) { |
| 722 | int c; |
| 723 | int count; |
| 724 | char *bp; |
| 725 | if (n == 0) |
| 726 | return -1; |
| 727 | bp = buf; |
| 728 | for (count = 0;; count++) { |
| 729 | c = cfgetc(cfp); |
| 730 | if (c == EOF) { |
| 731 | if (!cfeof(cfp)) // not actually at EOF, so must be error |
| 732 | ThrowCFileError(CFE_READING, cfp, strerror(errno)); |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | if ((!(cfp->flags & CFF_TEXT) && (c == 0)) || ((cfp->flags & CFF_TEXT) && (c == '\n'))) |
| 737 | break; // end-of-string |
| 738 | if (count < n - 1) // store char if room in buffer |
| 739 | *bp++ = c; |
| 740 | } |
| 741 | *bp = 0; // write terminator |
| 742 | return count; |
| 743 | } |
| 744 | // Writes the specified number of bytes from a file into the buffer |
| 745 | // DO NOT USE THIS TO WRITE STRUCTURES. This function is for byte |
| 746 | // data, such as a string or a bitmap of 8-bit pixels. |