Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert string to stream?

Casper Bang
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Mike Wahler
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Convert string to stream?



"Casper Bang" <casper@jbr.dk> wrote in message
news:3f731a2b$0$54828$edfadb0f@dread11.news.tele.d k...[color=blue]
> I am using an older C library (Flex scanner) which requires that a[/color]
variable[color=blue]
> is set as a pointer to a file:
>
> #define FILE _iobuf
> FILE* hFile;[/color]


'_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;
[color=blue]
>
> It works great but I would like to add a wrapper so I can parse a string[/color]
in[color=blue]
> memmory as well. Presently I do that by saving the string in a temp file[/color]
and[color=blue]
> 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)?[/color]

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


Jerry Coffin
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Convert string to stream?


In article <3f731a2b$0$54828$edfadb0f@dread11.news.tele.dk> ,
casper@jbr.dk says...[color=blue]
> 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.[/color]

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.
[color=blue]
> 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)?[/color]

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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Closed Thread