fopen.c: re-indented, fixed previous mistake

I've made the code intended using curl-style now to look more like other
examples.

My previous "fix" was a bit too invasive but is now fixed again.
This commit is contained in:
Daniel Stenberg 2010-11-04 11:42:27 +01:00
parent 542318b113
commit 809a748124

View File

@ -53,7 +53,11 @@
#include <curl/curl.h>
enum fcurl_type_e { CFTYPE_NONE=0, CFTYPE_FILE=1, CFTYPE_CURL=2 };
enum fcurl_type_e {
CFTYPE_NONE=0,
CFTYPE_FILE=1,
CFTYPE_CURL=2
};
struct fcurl_data
{
@ -83,8 +87,7 @@ void url_rewind(URL_FILE *file);
CURLM *multi_handle;
/* curl calls this routine to get more data */
static size_t
write_callback(char *buffer,
static size_t write_callback(char *buffer,
size_t size,
size_t nitems,
void *userp)
@ -97,36 +100,28 @@ write_callback(char *buffer,
rembuff=url->buffer_len - url->buffer_pos; /* remaining space in buffer */
if(size > rembuff)
{
if(size > rembuff) {
/* not enough space in buffer */
newbuff=realloc(url->buffer,url->buffer_len + (size - rembuff));
if(newbuff==NULL)
{
if(newbuff==NULL) {
fprintf(stderr,"callback buffer grow failed\n");
size=rembuff;
}
else
{
else {
/* realloc suceeded increase buffer size*/
url->buffer_len+=size - rembuff;
url->buffer=newbuff;
/*printf("Callback buffer grown to %d bytes\n",url->buffer_len);*/
}
}
memcpy(&url->buffer[url->buffer_pos], buffer, size);
url->buffer_pos += size;
/*fprintf(stderr, "callback %d size bytes\n", size);*/
return size;
}
/* use to attempt to fill the read buffer up to requested number of bytes */
static int
fill_buffer(URL_FILE *file,int want,int waittime)
static int fill_buffer(URL_FILE *file,int want,int waittime)
{
fd_set fdread;
fd_set fdwrite;
@ -141,8 +136,7 @@ fill_buffer(URL_FILE *file,int want,int waittime)
return 0;
/* attempt to fill buffer */
do
{
do {
int maxfd = -1;
long curl_timeo = -1;
@ -180,6 +174,7 @@ fill_buffer(URL_FILE *file,int want,int waittime)
break;
case 0:
default:
/* timeout or readable/writable sockets */
curl_multi_perform(multi_handle, &file->still_running);
break;
@ -189,12 +184,10 @@ fill_buffer(URL_FILE *file,int want,int waittime)
}
/* use to remove want bytes from the front of a files buffer */
static int
use_buffer(URL_FILE *file,int want)
static int use_buffer(URL_FILE *file,int want)
{
/* sort out buffer */
if((file->buffer_pos - want) <=0)
{
if((file->buffer_pos - want) <=0) {
/* ditch buffer - write will recreate */
if(file->buffer)
free(file->buffer);
@ -203,8 +196,7 @@ use_buffer(URL_FILE *file,int want)
file->buffer_pos=0;
file->buffer_len=0;
}
else
{
else {
/* move rest down make it available for later */
memmove(file->buffer,
&file->buffer[want],
@ -215,10 +207,7 @@ use_buffer(URL_FILE *file,int want)
return 0;
}
URL_FILE *
url_fopen(const char *url,const char *operation)
URL_FILE *url_fopen(const char *url,const char *operation)
{
/* this code could check for URLs or types in the 'url' and
basicly use the real fopen() for standard files */
@ -233,11 +222,9 @@ url_fopen(const char *url,const char *operation)
memset(file, 0, sizeof(URL_FILE));
if((file->handle.file=fopen(url,operation)))
{
file->type = CFTYPE_FILE; /* marked as URL */
}
else
{
else {
file->type = CFTYPE_CURL; /* marked as URL */
file->handle.curl = curl_easy_init();
@ -254,8 +241,7 @@ url_fopen(const char *url,const char *operation)
/* lets start the fetch */
curl_multi_perform(multi_handle, &file->still_running);
if((file->buffer_pos == 0) && (!file->still_running))
{
if((file->buffer_pos == 0) && (!file->still_running)) {
/* if still_running is 0 now, we should return NULL */
/* make sure the easy handle is not in the multi handle anymore */
@ -272,13 +258,11 @@ url_fopen(const char *url,const char *operation)
return file;
}
int
url_fclose(URL_FILE *file)
int url_fclose(URL_FILE *file)
{
int ret=0;/* default is good return */
switch(file->type)
{
switch(file->type) {
case CFTYPE_FILE:
ret=fclose(file->handle.file); /* passthrough */
break;
@ -295,7 +279,6 @@ url_fclose(URL_FILE *file)
ret=EOF;
errno=EBADF;
break;
}
if(file->buffer)
@ -306,13 +289,11 @@ url_fclose(URL_FILE *file)
return ret;
}
int
url_feof(URL_FILE *file)
int url_feof(URL_FILE *file)
{
int ret=0;
switch(file->type)
{
switch(file->type) {
case CFTYPE_FILE:
ret=feof(file->handle.file);
break;
@ -321,6 +302,7 @@ url_feof(URL_FILE *file)
if((file->buffer_pos == 0) && (!file->still_running))
ret = 1;
break;
default: /* unknown or supported type - oh dear */
ret=-1;
errno=EBADF;
@ -329,13 +311,11 @@ url_feof(URL_FILE *file)
return ret;
}
size_t
url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
{
size_t want;
switch(file->type)
{
switch(file->type) {
case CFTYPE_FILE:
want=fread(ptr,size,nmemb,file->handle.file);
break;
@ -359,10 +339,7 @@ url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
use_buffer(file,want);
want = want / size; /* number of items - nb correct op - checked
* with glibc code*/
/*printf("(fread) return %d bytes %d left\n", want,file->buffer_pos);*/
want = want / size; /* number of items */
break;
default: /* unknown or supported type - oh dear */
@ -374,14 +351,12 @@ url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
return want;
}
char *
url_fgets(char *ptr, int size, URL_FILE *file)
char *url_fgets(char *ptr, int size, URL_FILE *file)
{
int want = size - 1;/* always need to leave room for zero termination */
int loop;
switch(file->type)
{
switch(file->type) {
case CFTYPE_FILE:
ptr = fgets(ptr,size,file->handle.file);
break;
@ -400,10 +375,8 @@ url_fgets(char *ptr, int size, URL_FILE *file)
/*buffer contains data */
/* look for newline or eof */
for(loop=0;loop < want;loop++)
{
if(file->buffer[loop] == '\n')
{
for(loop=0;loop < want;loop++) {
if(file->buffer[loop] == '\n') {
want=loop+1;/* include newline */
break;
}
@ -415,7 +388,6 @@ url_fgets(char *ptr, int size, URL_FILE *file)
use_buffer(file,want);
/*printf("(fgets) return %d bytes %d left\n", want,file->buffer_pos);*/
break;
default: /* unknown or supported type - oh dear */
@ -427,11 +399,9 @@ url_fgets(char *ptr, int size, URL_FILE *file)
return ptr;/*success */
}
void
url_rewind(URL_FILE *file)
void url_rewind(URL_FILE *file)
{
switch(file->type)
{
switch(file->type) {
case CFTYPE_FILE:
rewind(file->handle.file); /* passthrough */
break;
@ -455,17 +425,13 @@ url_rewind(URL_FILE *file)
default: /* unknown or supported type - oh dear */
break;
}
}
/* Small main program to retrive from a url using fgets and fread saving the
* output to two test files (note the fgets method will corrupt binary files if
* they contain 0 chars */
int
main(int argc, char *argv[])
int main(int argc, char *argv[])
{
URL_FILE *handle;
FILE *outf;
@ -475,32 +441,25 @@ main(int argc, char *argv[])
const char *url;
if(argc < 2)
{
url="http://192.168.7.3/testfile";/* default to testurl */
}
else
{
url=argv[1];/* use passed url */
}
/* copy from url line by line with fgets */
outf=fopen("fgets.test","w+");
if(!outf)
{
if(!outf) {
perror("couldn't open fgets output file\n");
return 1;
}
handle = url_fopen(url, "r");
if(!handle)
{
if(!handle) {
printf("couldn't url_fopen() %s\n", url);
fclose(outf);
return 2;
}
while(!url_feof(handle))
{
while(!url_feof(handle)) {
url_fgets(buffer,sizeof(buffer),handle);
fwrite(buffer,1,strlen(buffer),outf);
}
@ -512,8 +471,7 @@ main(int argc, char *argv[])
/* Copy from url with fread */
outf=fopen("fread.test","w+");
if(!outf)
{
if(!outf) {
perror("couldn't open fread output file\n");
return 1;
}
@ -537,8 +495,7 @@ main(int argc, char *argv[])
/* Test rewind */
outf=fopen("rewind.test","w+");
if(!outf)
{
if(!outf) {
perror("couldn't open fread output file\n");
return 1;
}