typedef struct _iobuf FILE;

B

Beefheart

I have some c code which I have acquired and would like to use,
unfortunately all the code (there is a lot of it) is expecting a passed
FILE* pointer.
The data I have is of the correct type but it is in memory. Is there a way
I can create a FILE structure and point it to the data in memory, or
generally convert my in memory data to a FILE structure?

I know that I could always save the data in memory to disk and then load it
again, to acquire the FILE structure but I would rather not.

many thanks
 
K

Kenneth Brody

Beefheart said:
I have some c code which I have acquired and would like to use,
unfortunately all the code (there is a lot of it) is expecting a passed
FILE* pointer.
The data I have is of the correct type but it is in memory. Is there a way
I can create a FILE structure and point it to the data in memory, or
generally convert my in memory data to a FILE structure?

I know that I could always save the data in memory to disk and then load it
again, to acquire the FILE structure but I would rather not.

Unless you have access to the source of the functions you are calling,
and can modify them to take a pointer to the data, the only portable
way I know of to pass a FILE* to the data is to write it to disk, fopen
the file, and pass a real FILE*.

There may be a platform-/compiler-specific method to do what you ask,
but I can't help you there.
 
D

Default User

Beefheart said:
I have some c code which I have acquired and would like to use,
unfortunately all the code (there is a lot of it) is expecting a passed
FILE* pointer.
The data I have is of the correct type but it is in memory. Is there a way
I can create a FILE structure and point it to the data in memory, or
generally convert my in memory data to a FILE structure?

I know that I could always save the data in memory to disk and then load it
again, to acquire the FILE structure but I would rather not.



There's nothing in ISO standard C that will allow you to do that. Any
methods will be platform-specific and off-topic here. You need a
newsgroup dedicated to your particular hardware/OS/compiler.

Possibilities to investigate include writing your own driver or a
thirdparty ramdisk solution.




Brian Rodenborn
 
C

CBFalconer

Default said:
There's nothing in ISO standard C that will allow you to do that.
Any methods will be platform-specific and off-topic here. You
need a newsgroup dedicated to your particular hardware/OS/compiler.

Possibilities to investigate include writing your own driver or a
thirdparty ramdisk solution.

I suspect the OP is highly confused, and needs to describe his
problems more clearly. There is no way he is allowed to create a
FILE structure. It sounds to me as if he really needs to open
some appropriate files.
 
B

Beefheart

I have found a function called fmemopen on the http://www.gnu.org/ site,
which seems to exactly what I need and seems to be in some elusive version
of stdio.h. Only problem is I dont seem to be able to find the source code.

is this off topic, sorry newbie question?
 
R

red floyd

CBFalconer said:
Default said:
Beefheart wrote:
[request for in-memory FILE type redacted]
I suspect the OP is highly confused, and needs to describe his
problems more clearly. There is no way he is allowed to create a
FILE structure. It sounds to me as if he really needs to open
some appropriate files.

I think the OP was wondering if there's a C equivalent to the C++
stringstream construct. I don't believe there is.
 
C

Chris Torek

I have found a function called fmemopen on the http://www.gnu.org/ site,
which seems to exactly what I need and seems to be in some elusive version
of stdio.h. Only problem is I dont seem to be able to find the source code.

fmemopen() is not standard, so there is no assurance that you
have one.

I added a generalized "function-oriented" stdio interface to 4.xBSD,
which is now in NetBSD and FreeBSD. Here, rather than a specific
"open memory region" function, there is an "open these functions"
function. You provide functions that implement reading, writing,
and/or seeking -- at least one of read and write is required --
and closing, along with a "void *" value. Your provided functions
will be called to accomplish data transfers. (For optimization
purposes you are allowed to call setvbuf() to attempt to redirect
future read/write operations to specific areas of memory, but you
must be prepared for this to fail or be overridden by the user.)

Given funopen(), you can implement the equivalent of fmemopen().
You can also implement a "FILE *" stream that sits atop a compression
algorithm, or a "FILE *" stream that implements encryption, or
any other trick you like. You can stack these on top of each
other because each one is just a "FILE *":

struct compression_data {
FILE *substream;
... more data here as needed ...
};

static int compress_write(void *cookie, const char *data, int len) {
struct compression_data *p = cookie;

... do whatever it takes to compress the data and write it
to p->substream ...

return number_of_bytes_transferred; /* or -1 for error */
}

FILE *compressed_output_stream(FILE *substream) {
FILE *rv;
struct compression_data *p;

if ((p = malloc(sizeof *p)) == NULL)
return NULL;
p->substream = substream;
rv = funopen(p, NULL, compress_write, NULL, compress_close);
if (rv == NULL)
free(p);
return rv;
}

struct encryption_data {
FILE *substream;
...
};

static int encrypt_write(void *cookie, const char *data, int len) {
struct encryption_data *p = cookie;

... do whatever it takes to encrypt the data and write it
to p->substream ...
return number_of_bytes_transferred;
}

FILE *encrypted_output_stream(FILE *substream, ...) {
FILE *rv;
struct encryption_data *p;

if ((p = malloc(sizeof *p)) == NULL)
return NULL;
p->substream = substream;
... handle additional details like keys here ...
rv = funopen(p, NULL, encrypt_write, NULL, encrypt_close);
if (rv == NULL)
free(p);
return rv;
}

Now you can get an encrypted compressed file like this:

FILE *bottom, *middle, *top;

out = fopen("foo.encrypted.compressed", "w");
middle = out ? compressed_output_stream(out) : NULL;
top = middle ? encrypted_output_stream(middle, ...) : NULL;

Now ordinary fprintf()s to "top" will encrypt, then compress, then
write. (Each "close" function, not shown here, has to push any
unwritten data through, of course.) Attempts to fseek() on "middle"
and "top" will be rejected since there is no underlying seek
function. (Supporting seeks within compressed or encrypted files
is generally difficult.)

Alas, funopen() is likewise nonstandard.
 
R

Ross Kendall Axe

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Beefheart wrote:
| I have found a function called fmemopen on the http://www.gnu.org/ site,
| which seems to exactly what I need and seems to be in some elusive version
| of stdio.h. Only problem is I dont seem to be able to find the source
code.
|
| is this off topic, sorry newbie question?
|
|

Yes, it seems it is. From the relevent section of GNU's site:

Portability Note: The facilities described in this section are specific
to GNU. Other systems or C implementations might or might not provide
equivalent functionality.

<OT>
My stdio.h does indeed contain fmemopen, but then *I* run a glibc (i.e.
GNU) operating system. If you don't, then you're probably SOL.
</OT>

Ross
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAkvHB9bR4xmappRARAmogAKCu8RnZFbRLUcu2vBG/7sfT+KXmAACg2erH
gSgLv5dqyak7N7+N/+OSlNg=
=kcWU
-----END PGP SIGNATURE-----
 
T

those who know me have no need of my name

in comp.lang.c i read:
Beefheart wrote:
| I have found a function called fmemopen
| is this off topic, sorry newbie question?
Yes, it seems it is. From the relevent section of GNU's site:

Portability Note: The facilities described in this section are specific
to GNU. Other systems or C implementations might or might not provide
equivalent functionality.

not only might there not be an fmemopen, if there is one there is no way to
know if it implements the same semantics, even if it takes the same number
and type of parameters and returns the same type. (in this particular
case it's hard to imagine any other semantic that fit all those conditions.)
 
T

those who know me have no need of my name

in comp.lang.c i read:
I think the OP was wondering if there's a C equivalent to the C++
stringstream construct. I don't believe there is.

nothing in program memory specifically. but tmpfile() can be wonderfully
useful, and it may be that it will operate entirely in memory (whether it
actually will is off-topic for this group).
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top