Convert string to stream?

C

Casper Bang

I am using an older C library (Flex scanner) which requires that a variable
is set as a pointer to a file:

#define FILE _iobuf
FILE* hFile;

It works great but I would like to add a wrapper so I can parse a string in
memmory as well. Presently I do that by saving the string in a temp file and
then making the original call to the library with the filehandle. This is
slow and clumsy so I was wondering if there's some way of converting a
string to an appropiate stream (or _iobuf whatever that is)?

Thanks in advance,
Casper
 
M

Mike Wahler

Casper Bang said:
I am using an older C library (Flex scanner) which requires that a variable
is set as a pointer to a file:

#define FILE _iobuf
FILE* hFile;


'_iobuf' is a name which is reserved to the implementation.
Do not use it in your code. Same goes for 'FILE'.

Use #include <stdio.h> to get the proper defintion
of 'FILE' for your implementation.

#include <stdio.h>

FILE *hFile;
It works great but I would like to add a wrapper so I can parse a string in
memmory as well. Presently I do that by saving the string in a temp file and
then making the original call to the library with the filehandle. This is
slow and clumsy so I was wondering if there's some way of converting a
string to an appropiate stream (or _iobuf whatever that is)?

I suppose you found that 'io_buf' by looking at header file(s).
It's an implementation detail -- hands off to the application
programmer. #include <stdio.h> and use 'FILE *'.

There's no standard way to do what you want.

-Mike
 
J

Jerry Coffin

I am using an older C library (Flex scanner) which requires that a variable
is set as a pointer to a file:

#define FILE _iobuf
FILE* hFile;

It works great but I would like to add a wrapper so I can parse a string in
memmory as well.

A Flex scanner will normally use YY_INPUT to get input. You can define
that yourself if you want it to read data in a different fashion than
the usual.
Presently I do that by saving the string in a temp file and
then making the original call to the library with the filehandle. This is
slow and clumsy so I was wondering if there's some way of converting a
string to an appropiate stream (or _iobuf whatever that is)?

It's OS specific, but in quite a few cases you could also create a pipe
and have it read from the pipe, while something at the other end feeds
the data from the string into the pipe. This would be useful if the
scanner itself needed to be oblivious to the source of the data.

Flex also supports a '-+' argument to create a C++ lexer as a set of
classes. In this case, the lexer class reads from an istream so it
should be quite easy to pass an istringstream instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top