Note that there are some explanatory texts on larger screens.

plurals
  1. POA reader interface that consumes FILEs and char* in C
    primarykey
    data
    text
    <p>I am modifying a parser I've inherited which currently only reads from FILE* via read. It now has the requirement of being able to pull data from char* constants as well so inline text inside C strings can be parsed.</p> <p>I've looked at providing a simple interface to both in the form of "readers" so you can provide a file reader and a char reader from which the parser can grab characters. For example:</p> <pre><code>// Inputs const char *str = "stringToParse"; FILE *f = fopen(...); // Creating a reader. Each reader stores a function ptr to a destructor // which closes the file if required and an internal state object. Reader *r = FileReader(f); // -or- Reader *r = CharReader(str); // Start parsing --------------------------- // Inside the parser, repeated calls to: int error = ReadBytes(&amp;buf /* target buf */, &amp;nRead /* n read out */, maxBytes /* max to read */); // End parsing ----------------------------- CloseReader(&amp;r); // calls destructor, free's state, self </code></pre> <p>I'd like to keep this really simple. Are there any obvious other ways to achieve this using less infrastructure that I have missed? </p> <p>Note: I have simplified this considerably from what is there to highlight the programming interface concerns. It actually uses wchar_t internally and a mush of encoding stuff and is a bit of a rat's nest which I will untangle at the same time.</p> <hr> <p>Thanks to everyone who answered. The cleanest answer is to use fmemopen. I've provided a full example below:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void dump(FILE *f) { char c; while ((c = fgetc(f)) != EOF) putchar(c); } int main(int argc, char *argv[]) { /* open string */ const char str[] = "Hello string!\n"; FILE *fstr = fmemopen(&amp;str, strlen(str), "r"); /* open file */ FILE *ffile = fopen("hello.file", "r"); /* dump each to stdout */ dump(ffile); dump(fstr); /* clean up */ fclose(ffile); fclose(fstr); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload