一位读者的界面消耗文件和char *用C [英] A reader interface that consumes FILEs and char* in C
问题描述
我修改我继承解析器目前只能从FILE *通过读取保存。它现在拥有的能够从字符提取数据*常量以及C字符串内使嵌入式文本可以分析的要求。
我看了看读者的形式提供了一个简单的界面,这样既可以提供一个文件阅读器和一个char读者从中解析器可以抢个字符。例如:
//输入
为const char *海峡=stringToParse;
FILE * F =的fopen(...);//创建一个阅读器。每个读者存储功能PTR到析构函数
//用于关闭如果需要的文件和内部状态对象。
阅读器* R =的FileReader(F);
// -要么-
阅读器* R = CharReader(STR);//开始解析---------------------------
//解析器内部,重复调用:
INT错误=的ReadBytes(安培; BUF / *目标BUF * /&安培; NREAD / * N读出* / maxBytes / *最大读取* /);
//结束分析-----------------------------CloseReader(安培; R); //调用析构函数,自由的状态,自
我想保持这个真的很简单。是否有任何明显的其他方式来实现这一目标使用我错过了较少的基础设施?
注:我简化了这个大大的还有什么要突出编程接口担忧。实际上它使用wchar_t的内部和编码东东玉米粥,是有点老鼠窝的,我将在同一时间解开。
感谢大家谁回答。最干净的答案是使用fmemopen。我下面提供一个完整的示例:
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;无效转储(FILE * F){
焦炭℃;
而((C =龟etc(F))!= EOF)
的putchar(C);
}INT主(INT ARGC,CHAR *的argv []){
/ *开弦* /
为const char海峡[] =你好!字符串\\ n;
FILE * FSTR = fmemopen(安培; STR,strlen的(STR),R); /* 打开文件 */
FILE * ffile的=的fopen(hello.file,R); / *每个转储到标准输出* /
转储(ffile的);
转储(FSTR); /* 清理 */
FCLOSE(ffile的);
FCLOSE(FSTR);
}
您甚至不中你的基础设施需要一个 CharReader
。相反,当内存缓冲区有布局文件下同应该工作:
为const char *海峡=stringToParse;
FILE * F = fmemopen(STR,strlen的(STR),R);阅读器* R =的FileReader(F);//使用的FileReader从字符串读取...
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.
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:
// 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(&buf /* target buf */, &nRead /* n read out */, maxBytes /* max to read */);
// End parsing -----------------------------
CloseReader(&r); // calls destructor, free's state, self
I'd like to keep this really simple. Are there any obvious other ways to achieve this using less infrastructure that I have missed?
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.
Thanks to everyone who answered. The cleanest answer is to use fmemopen. I've provided a full example below:
#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[]) {
/* open string */
const char str[] = "Hello string!\n";
FILE *fstr = fmemopen(&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);
}
You don't even need a CharReader
in your infrastructure. Instead, the following should work when memory buffers have the same layout as files:
const char *str = "stringToParse";
FILE *f = fmemopen(str, strlen(str), "r");
Reader *r = FileReader(f);
// use FileReader to read from string...
这篇关于一位读者的界面消耗文件和char *用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!