Skip to main content.
home | support | download

Back to List Archive

Re: read_stream optimized...

From: <jmruiz(at)not-real.boe.es>
Date: Mon Nov 20 2000 - 10:00:35 GMT
Hi Rainer,

You are right, your read_stream should be faster when streams
are readed from a popen/filter call.
If the file was opened with a fopen call, filelen was a known value,
so we can do an emalloc of the total length. This is the way 
read_buffer worked.
Anyway, I agree with your new function because it does not have to 
know if the stream comes from a fopen or a popen call. 


> char *read_stream(FILE *fp,int filelen)
> {
> int c=0,offset=0,bufferlen=0;
> unsigned char *buffer;
>  if(filelen)
>  {
>   buffer=emalloc(filelen+1);

Here a buffer with the total length of the data to be read is allocated.

>   vread(buffer,1,filelen,fp);

Reads the complete buffer.

>   buffer[filelen]='\0';
>  } else {    /* if we are reading from a popen call, filelen is 0 */

This was really a quick patch to get filter option up once again.

>   buffer=emalloc((bufferlen=MAXSTRLEN)+1);
>   while((c=fgetc(fp))!=EOF)
>   {
>    if(offset==bufferlen)
>    {
>     bufferlen+=MAXSTRLEN;
>     buffer=erealloc(buffer,bufferlen+1);
>    }
>    buffer[offset++]=(unsigned char)c;
>   }
>   buffer[offset]='\0';
>  }
>  return (char *)buffer;
> }
> -----------------
> 
> As I mentioned the routine is not optimized (fget, lots of possible
> reallocs), when reading e.g. data from a file stream. So the routine
> could look like (the vread-part with filelen might be better, but on
> most filesizes below the initial READ_BUFFER_SIZE, does not make any
> difference...):
> 
> -----------------
> 
> #define READ_BUFFER_SIZE (128 * 1024)
> 
> char *read_stream(FILE *fp)
> 
> {  unsigned char *buffer;
>    long          n, rd_len;
> 
> 
>  buffer=emalloc(READ_BUFFER_SIZE);
>  rd_len = 0;
> 
>  while (! feof(fp)) {
>   n = fread(buffer+rd_len,sizeof(unsigned
> char),READ_BUFFER_SIZE,fp);
>   rd_len += n;
>   if (n == READ_BUFFER_SIZE) {
>     buffer = erealloc (buffer, rd_len+READ_BUFFER_SIZE);
>   }
>  }
>  buffer[rd_len]='\0';
> 
>  return (char *)buffer;
> }
> 

cu
Jose 
Received on Mon Nov 20 10:02:20 2000