apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jon Travis <jtra...@covalent.net>
Subject Re: apr file questions
Date Thu, 18 Apr 2002 16:43:58 GMT
On Thu, Apr 18, 2002 at 10:40:12AM -0400, Cliff Woolley wrote:
> On 18 Apr 2002, Jeff Trawick wrote:
> 
> Since APR_EOF is one of the errors you might get back from
> apr_file_read(), I agree with Jeff.  This:
> 
> > >          while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
> > >          {
> > >             ...
> > >          }
> 
> is probably preferable.  You'll most likely want to hang onto that return
> code, though, like this:
> 
>              while ((rc = apr_file_read(file, buffer, &len)) != APR_SUCCESS)
>              {
>                 ...
>              }

Of course there is some amount of suckiness associated with this, because
len will get mucked with by apr_file_read.  I've seen this happen in more
than one case where len gets reset so short reads occur.  Ideally you
always get the initial size back until the last couple times, but for files
which grow as you are reading them, etc. this won't work out very well.
So, you'd need:

len = sizeof(buffer)
while((rc = apr_file_read(file, buffer, &len)) != APR_SUCCESS){
   ...
   len = sizeof(buffer)
}

I've been preferring to stick the buffer scoped locally to the loop, and
have the read occur inside -- that way there is only one initialization of
the length, and it's obvious:

while(!apr_file_eof(file)){
   char somebuf[1024];
   apr_size_t len = sizeof(somebuf);

   if((rc = apr_file_read(file, somebuf, &len) .. blah blah blah
}

-- Jon


Mime
View raw message