I am modifying a parser that I have inherited, which currently only reads from FILE * via read. Now he has a need to be able to extract data from char * constants so that inline text inside C lines can be parsed.
I have considered providing a simple interface both in the form of "readers" and for reading files and a char reader from which the parser can capture characters. For instance:
const char *str = "stringToParse";
FILE *f = fopen(...);
Reader *r = FileReader(f);
Reader *r = CharReader(str);
int error = ReadBytes(&buf , &nRead , maxBytes );
CloseReader(&r);
I would like it to be very simple. Are there any obvious other ways to achieve this using less infrastructure that I missed?
. , , . wchar_t , .
, . - fmemopen. :
#include <stdio.h>
#include <string.h>
void dump(FILE *f) {
char c;
while ((c = fgetc(f)) != EOF)
putchar(c);
}
int main(int argc, char *argv[]) {
const char str[] = "Hello string!\n";
FILE *fstr = fmemopen(&str, strlen(str), "r");
FILE *ffile = fopen("hello.file", "r");
dump(ffile);
dump(fstr);
fclose(ffile);
fclose(fstr);
}