Article View: comp.unix.programmer
Article #842Re: File pointer to a memory location?
From: Thomas Tornblom
Date: Wed, 12 Sep 1990 09:05
Date: Wed, 12 Sep 1990 09:05
57 lines
3054 bytes
3054 bytes
In article <119609@linus.mitre.org> rtidd@ccels3.mitre.org (Randy Tidd) writes:
With all these new comp.unix.* groups coming out, I hope it's
appropriate to cross-post to .programmer and .internals...
Anyhow, in the application i'm working on I have a series of routines
that were written by someone else that do image processing (the fbm
library, if you're familiar with it), including source. These
routines take a file pointer as an argument, and they read the
image-to-be-processed through this fp. Normally what you do is open a
file and pass in a file pointer to it, or pass in stdin and pipe your
image through the program through the shell.
The problem is in my application, I am using a database and thus don't
have files and thus I don't have file pointers either. What I *can*
do is query the database for an image, and it gives me a block of
memory that holds the image *exactly* as it is stored in file form.
So what I have is a block of memory, having exactly the same size and
contents of a file. What I have to do is pass my image processing
routines a file pointer that points to this block of memory. If the
routines used file *descriptors*, it wouldn't be a problem because I
could just use pipes and be done with it.
You can make a fp from a fd by fp = fdopen(fd, type) it would however
require all the data to pass through the kernel once more.
What I do now is query the database, get a block of memory, dump this
memory to a temporary file, open the file with a file pointer, and
pass the file pointer to the image processing routines. Not only is
this dumb, but images can be a big as 3 megs and this is incredibly
inefficient.
Depending on the system you are using and whether you are concerned
with portability it could be done by faking up an _iobuf struct (the things
fp:s point at) that would hold a pointer to the block of memory and
have all the rest of the members in the struct set up to some sane value.
Something along the line of:
(Warning this is untested and non portable)
---------------
#include <stdio.h>
struct _iobuf fake;
FILE *getimage()
{
char *image;
/* read the image from the db and have image point at it */
fake._ptr = fake._base = image;
fake._cnt = fake._bufsiz = image_size;
fake._flag = _IOREAD | _IOMYBUF;
fake._file = -1;
return &fake;
}
---------------
Technical Details
Message-ID:
<THOMAS.90Sep12110520@uplog.uplog.se>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!sics.se!uplog.se!uplog.uplog.se!thomasReferences:
<119609@linus.mitre.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!sics.se!uplog.se!uplog.uplog.se!thomas From: thomas@uplog.se (Thomas Tornblom) Newsgroups: comp.unix.programmer Subject: Re: File pointer to a memory location? Message-ID: <THOMAS.90Sep12110520@uplog.uplog.se> Date: 12 Sep 90 09:05:20 GMT References: <119609@linus.mitre.org> Sender: news@uplog.se (0000-news(0000)) Followup-To: comp.unix.programmer Organization: TeleLOGIC Uppsala AB Lines: 57 In-Reply-To: rtidd@ccels3.mitre.org's message of 10 Sep 90 16:46:02 GMT