コードを少し変更して、カスタム ハンドルを受け取ることができるかもしれません。
void parse(my_handle *h, Element *result)
{
// read from handle and process
// call h->read instead of fread
}
ハンドルを次のように定義します。
struct my_handle
{
// wrapper for fread or something
int (*read)(struct my_handle *h, char *buffer, int readsize);
// maybe some more methods you need
};
FILE* ラッパーを実装する
struct my_file_handle
{
struct my_handle base;
FILE *fp;
};
int read_from_file(struct my_handle *h, char *buffer, int readsize)
{
return fread(buffer, 1, readsize, ((my_file_handle*)h)->fp);
}
// function to init the FILE* wrapper
void init_my_file_handle(struct my_file_handle *h, FILE *fp)
{
h->base.read = read_from_file;
h->fp = fp;
}
次に、文字列リーダーを実装します
struct my_string_handle
{
struct my_handle base;
// string buffer, size, and current position
const char *buffer;
int size;
int position;
};
// string reader
int read_from_string(struct my_handle *h, char *buffer, int readsize)
{
// implement it yourself. It's easy.
}
// create string reader handle
void init_my_string_handle(struct my_string_handle *h, const char *str, int strsize)
{
// i think you know how to init it now.
}
///////////////////////////////////////////////
これで、解析関数にハンドルを送信するだけで済みます。この関数は、データがどこから来ているかを気にせず、ネットワークからデータを読み取ることさえできます!