Compiles, most tests break
This commit is contained in:
parent
b8f241c2c5
commit
0e11adea1c
22 changed files with 902 additions and 714 deletions
832
src/io/fopen.c
832
src/io/fopen.c
|
|
@ -6,6 +6,9 @@
|
|||
* Modifications to read/write wide character streams by
|
||||
* Simon Brooke.
|
||||
*
|
||||
* NOTE THAT: for my purposes, I'm only interested in wide characters,
|
||||
* and I always read them one character at a time.
|
||||
*
|
||||
* Copyright (c) 2003, 2017 Simtec Electronics
|
||||
* Some portions (c) 2019 Simon Brooke <simon@journeyman.cc>
|
||||
*
|
||||
|
|
@ -34,14 +37,13 @@
|
|||
* This example requires libcurl 7.9.7 or later.
|
||||
*/
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifndef WIN32
|
||||
# include <sys/time.h>
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
|
|
@ -51,362 +53,376 @@
|
|||
static CURLM *multi_handle;
|
||||
|
||||
/* curl calls this routine to get more data */
|
||||
static size_t write_callback(char *buffer,
|
||||
size_t size,
|
||||
size_t nitems,
|
||||
void *userp)
|
||||
{
|
||||
char *newbuff;
|
||||
size_t rembuff;
|
||||
static size_t write_callback( char *buffer,
|
||||
size_t size, size_t nitems, void *userp ) {
|
||||
char *newbuff;
|
||||
size_t rembuff;
|
||||
|
||||
URL_FILE *url = (URL_FILE *)userp;
|
||||
size *= nitems;
|
||||
URL_FILE *url = ( URL_FILE * ) userp;
|
||||
size *= nitems;
|
||||
|
||||
rembuff = url->buffer_len - url->buffer_pos; /* remaining space in buffer */
|
||||
rembuff = url->buffer_len - url->buffer_pos; /* remaining space in buffer */
|
||||
|
||||
if(size > rembuff) {
|
||||
/* not enough space in buffer */
|
||||
newbuff = realloc(url->buffer, url->buffer_len + (size - rembuff));
|
||||
if(newbuff == NULL) {
|
||||
fprintf(stderr, "callback buffer grow failed\n");
|
||||
size = rembuff;
|
||||
if ( size > rembuff ) {
|
||||
/* not enough space in buffer */
|
||||
newbuff = realloc( url->buffer, url->buffer_len + ( size - rembuff ) );
|
||||
if ( newbuff == NULL ) {
|
||||
fprintf( stderr, "callback buffer grow failed\n" );
|
||||
size = rembuff;
|
||||
} else {
|
||||
/* realloc succeeded increase buffer size */
|
||||
url->buffer_len += size - rembuff;
|
||||
url->buffer = newbuff;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* realloc succeeded increase buffer size*/
|
||||
url->buffer_len += size - rembuff;
|
||||
url->buffer = newbuff;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(&url->buffer[url->buffer_pos], buffer, size);
|
||||
url->buffer_pos += size;
|
||||
memcpy( &url->buffer[url->buffer_pos], buffer, size );
|
||||
url->buffer_pos += size;
|
||||
|
||||
return size;
|
||||
return size;
|
||||
}
|
||||
|
||||
/* use to attempt to fill the read buffer up to requested number of bytes */
|
||||
static int fill_buffer(URL_FILE *file, size_t want)
|
||||
{
|
||||
fd_set fdread;
|
||||
fd_set fdwrite;
|
||||
fd_set fdexcep;
|
||||
struct timeval timeout;
|
||||
int rc;
|
||||
CURLMcode mc; /* curl_multi_fdset() return code */
|
||||
static int fill_buffer( URL_FILE * file, size_t want ) {
|
||||
fd_set fdread;
|
||||
fd_set fdwrite;
|
||||
fd_set fdexcep;
|
||||
struct timeval timeout;
|
||||
int rc;
|
||||
CURLMcode mc; /* curl_multi_fdset() return code */
|
||||
|
||||
/* only attempt to fill buffer if transactions still running and buffer
|
||||
* doesn't exceed required size already
|
||||
*/
|
||||
if((!file->still_running) || (file->buffer_pos > want))
|
||||
return 0;
|
||||
/* only attempt to fill buffer if transactions still running and buffer
|
||||
* doesn't exceed required size already
|
||||
*/
|
||||
if ( ( !file->still_running ) || ( file->buffer_pos > want ) )
|
||||
return 0;
|
||||
|
||||
/* attempt to fill buffer */
|
||||
do {
|
||||
int maxfd = -1;
|
||||
long curl_timeo = -1;
|
||||
/* attempt to fill buffer */
|
||||
do {
|
||||
int maxfd = -1;
|
||||
long curl_timeo = -1;
|
||||
|
||||
FD_ZERO(&fdread);
|
||||
FD_ZERO(&fdwrite);
|
||||
FD_ZERO(&fdexcep);
|
||||
FD_ZERO( &fdread );
|
||||
FD_ZERO( &fdwrite );
|
||||
FD_ZERO( &fdexcep );
|
||||
|
||||
/* set a suitable timeout to fail on */
|
||||
timeout.tv_sec = 60; /* 1 minute */
|
||||
timeout.tv_usec = 0;
|
||||
/* set a suitable timeout to fail on */
|
||||
timeout.tv_sec = 60; /* 1 minute */
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
curl_multi_timeout(multi_handle, &curl_timeo);
|
||||
if(curl_timeo >= 0) {
|
||||
timeout.tv_sec = curl_timeo / 1000;
|
||||
if(timeout.tv_sec > 1)
|
||||
timeout.tv_sec = 1;
|
||||
else
|
||||
timeout.tv_usec = (curl_timeo % 1000) * 1000;
|
||||
}
|
||||
curl_multi_timeout( multi_handle, &curl_timeo );
|
||||
if ( curl_timeo >= 0 ) {
|
||||
timeout.tv_sec = curl_timeo / 1000;
|
||||
if ( timeout.tv_sec > 1 )
|
||||
timeout.tv_sec = 1;
|
||||
else
|
||||
timeout.tv_usec = ( curl_timeo % 1000 ) * 1000;
|
||||
}
|
||||
|
||||
/* get file descriptors from the transfers */
|
||||
mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
|
||||
/* get file descriptors from the transfers */
|
||||
mc = curl_multi_fdset( multi_handle, &fdread, &fdwrite, &fdexcep,
|
||||
&maxfd );
|
||||
|
||||
if(mc != CURLM_OK) {
|
||||
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
|
||||
break;
|
||||
}
|
||||
if ( mc != CURLM_OK ) {
|
||||
fprintf( stderr, "curl_multi_fdset() failed, code %d.\n", mc );
|
||||
break;
|
||||
}
|
||||
|
||||
/* On success the value of maxfd is guaranteed to be >= -1. We call
|
||||
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
|
||||
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
|
||||
to sleep 100ms, which is the minimum suggested value in the
|
||||
curl_multi_fdset() doc. */
|
||||
/* On success the value of maxfd is guaranteed to be >= -1. We call
|
||||
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
|
||||
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
|
||||
to sleep 100ms, which is the minimum suggested value in the
|
||||
curl_multi_fdset() doc. */
|
||||
|
||||
if(maxfd == -1) {
|
||||
if ( maxfd == -1 ) {
|
||||
#ifdef _WIN32
|
||||
Sleep(100);
|
||||
rc = 0;
|
||||
Sleep( 100 );
|
||||
rc = 0;
|
||||
#else
|
||||
/* Portable sleep for platforms other than Windows. */
|
||||
struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
|
||||
rc = select(0, NULL, NULL, NULL, &wait);
|
||||
/* Portable sleep for platforms other than Windows. */
|
||||
struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
|
||||
rc = select( 0, NULL, NULL, NULL, &wait );
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
/* Note that on some platforms 'timeout' may be modified by select().
|
||||
If you need access to the original value save a copy beforehand. */
|
||||
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
|
||||
}
|
||||
} else {
|
||||
/* Note that on some platforms 'timeout' may be modified by select().
|
||||
If you need access to the original value save a copy beforehand. */
|
||||
rc = select( maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout );
|
||||
}
|
||||
|
||||
switch(rc) {
|
||||
case -1:
|
||||
/* select error */
|
||||
break;
|
||||
switch ( rc ) {
|
||||
case -1:
|
||||
/* select error */
|
||||
break;
|
||||
|
||||
case 0:
|
||||
default:
|
||||
/* timeout or readable/writable sockets */
|
||||
curl_multi_perform(multi_handle, &file->still_running);
|
||||
break;
|
||||
}
|
||||
} while(file->still_running && (file->buffer_pos < want));
|
||||
return 1;
|
||||
case 0:
|
||||
default:
|
||||
/* timeout or readable/writable sockets */
|
||||
curl_multi_perform( multi_handle, &file->still_running );
|
||||
break;
|
||||
}
|
||||
} while ( file->still_running && ( file->buffer_pos < want ) );
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* use to remove want bytes from the front of a files buffer */
|
||||
static int use_buffer(URL_FILE *file, size_t want)
|
||||
{
|
||||
/* sort out buffer */
|
||||
if((file->buffer_pos - want) <= 0) {
|
||||
/* ditch buffer - write will recreate */
|
||||
free(file->buffer);
|
||||
file->buffer = NULL;
|
||||
free(file->wide_buffer);
|
||||
file->wide_buffer = NULL;
|
||||
file->buffer_pos = 0;
|
||||
file->buffer_len = 0;
|
||||
file->wide_cursor = 0;
|
||||
}
|
||||
else {
|
||||
/* move rest down make it available for later */
|
||||
memmove(file->buffer,
|
||||
&file->buffer[want],
|
||||
(file->buffer_pos - want));
|
||||
static int use_buffer( URL_FILE * file, size_t want ) {
|
||||
/* sort out buffer */
|
||||
if ( ( file->buffer_pos - want ) <= 0 ) {
|
||||
/* ditch buffer - write will recreate */
|
||||
free( file->buffer );
|
||||
file->buffer = NULL;
|
||||
file->buffer_pos = 0;
|
||||
file->buffer_len = 0;
|
||||
} else {
|
||||
/* move rest down make it available for later */
|
||||
memmove( file->buffer,
|
||||
&file->buffer[want], ( file->buffer_pos - want ) );
|
||||
|
||||
file->buffer_pos -= want;
|
||||
// TODO: something to adjust the wide_cursor
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
URL_FILE *url_fopen(const char *url, const char *operation)
|
||||
{
|
||||
/* this code could check for URLs or types in the 'url' and
|
||||
basically use the real fopen() for standard files */
|
||||
|
||||
URL_FILE *file;
|
||||
(void)operation;
|
||||
|
||||
file = calloc(1, sizeof(URL_FILE));
|
||||
if(!file)
|
||||
return NULL;
|
||||
|
||||
file->handle.file = fopen(url, operation);
|
||||
if(file->handle.file)
|
||||
file->type = CFTYPE_FILE; /* marked as URL */
|
||||
|
||||
else {
|
||||
file->type = CFTYPE_CURL; /* marked as URL */
|
||||
file->handle.curl = curl_easy_init();
|
||||
|
||||
curl_easy_setopt(file->handle.curl, CURLOPT_URL, url);
|
||||
curl_easy_setopt(file->handle.curl, CURLOPT_WRITEDATA, file);
|
||||
curl_easy_setopt(file->handle.curl, CURLOPT_VERBOSE, 0L);
|
||||
curl_easy_setopt(file->handle.curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||
|
||||
if(!multi_handle)
|
||||
multi_handle = curl_multi_init();
|
||||
|
||||
curl_multi_add_handle(multi_handle, file->handle.curl);
|
||||
|
||||
/* lets start the fetch */
|
||||
curl_multi_perform(multi_handle, &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 */
|
||||
curl_multi_remove_handle(multi_handle, file->handle.curl);
|
||||
|
||||
/* cleanup */
|
||||
curl_easy_cleanup(file->handle.curl);
|
||||
|
||||
free(file);
|
||||
|
||||
file = NULL;
|
||||
file->buffer_pos -= want;
|
||||
}
|
||||
}
|
||||
return file;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int url_fclose(URL_FILE *file)
|
||||
{
|
||||
int ret = 0;/* default is good return */
|
||||
/**
|
||||
* consume one wide character on the buffer of this file.
|
||||
*
|
||||
* @param file the url or file from which the character is consumed.
|
||||
*/
|
||||
static int use_one_wide( URL_FILE * file ) {
|
||||
int c = ( int ) file->buffer[file->buffer_pos];
|
||||
size_t count = 0;
|
||||
|
||||
switch(file->type) {
|
||||
case CFTYPE_FILE:
|
||||
ret = fclose(file->handle.file); /* passthrough */
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
/* make sure the easy handle is not in the multi handle anymore */
|
||||
curl_multi_remove_handle(multi_handle, file->handle.curl);
|
||||
|
||||
/* cleanup */
|
||||
curl_easy_cleanup(file->handle.curl);
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
ret = EOF;
|
||||
errno = EBADF;
|
||||
break;
|
||||
}
|
||||
|
||||
free(file->buffer);/* free any allocated buffer space */
|
||||
free(file);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int url_feof(URL_FILE *file)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
switch(file->type) {
|
||||
case CFTYPE_FILE:
|
||||
ret = feof(file->handle.file);
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
if((file->buffer_pos == 0) && (!file->still_running))
|
||||
ret = 1;
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
ret = -1;
|
||||
errno = EBADF;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
|
||||
{
|
||||
size_t want;
|
||||
|
||||
switch(file->type) {
|
||||
case CFTYPE_FILE:
|
||||
want = fread(ptr, size, nmemb, file->handle.file);
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
want = nmemb * size;
|
||||
|
||||
fill_buffer(file, want);
|
||||
|
||||
/* check if there's data in the buffer - if not fill_buffer()
|
||||
* either errored or EOF */
|
||||
if(!file->buffer_pos)
|
||||
return 0;
|
||||
|
||||
/* ensure only available data is considered */
|
||||
if(file->buffer_pos < want)
|
||||
want = file->buffer_pos;
|
||||
|
||||
/* xfer data to caller */
|
||||
memcpy(ptr, file->buffer, want);
|
||||
|
||||
use_buffer(file, want);
|
||||
|
||||
want = want / size; /* number of items */
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
want = 0;
|
||||
errno = EBADF;
|
||||
break;
|
||||
|
||||
}
|
||||
return want;
|
||||
}
|
||||
|
||||
char *url_fgets(char *ptr, size_t size, URL_FILE *file)
|
||||
{
|
||||
size_t want = size - 1;/* always need to leave room for zero termination */
|
||||
size_t loop;
|
||||
|
||||
switch(file->type) {
|
||||
case CFTYPE_FILE:
|
||||
ptr = fgets(ptr, (int)size, file->handle.file);
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
fill_buffer(file, want);
|
||||
|
||||
/* check if there's data in the buffer - if not fill either errored or
|
||||
* EOF */
|
||||
if(!file->buffer_pos)
|
||||
return NULL;
|
||||
|
||||
/* ensure only available data is considered */
|
||||
if(file->buffer_pos < want)
|
||||
want = file->buffer_pos;
|
||||
|
||||
/*buffer contains data */
|
||||
/* look for newline or eof */
|
||||
for(loop = 0; loop < want; loop++) {
|
||||
if(file->buffer[loop] == '\n') {
|
||||
want = loop + 1;/* include newline */
|
||||
break;
|
||||
}
|
||||
/* The value of each individual byte indicates its UTF-8 function, as follows:
|
||||
*
|
||||
* 00 to 7F hex (0 to 127): first and only byte of a sequence.
|
||||
* 80 to BF hex (128 to 191): continuing byte in a multi-byte sequence.
|
||||
* C2 to DF hex (194 to 223): first byte of a two-byte sequence.
|
||||
* E0 to EF hex (224 to 239): first byte of a three-byte sequence.
|
||||
* F0 to FF hex (240 to 255): first byte of a four-byte sequence.
|
||||
*/
|
||||
if ( c <= '0x07' ) {
|
||||
count = 1;
|
||||
} else if ( c >= '0xc2' && c <= '0xdf' ) {
|
||||
count = 2;
|
||||
} else if ( c >= '0xe0' && c <= '0xef' ) {
|
||||
count = 3;
|
||||
} else if ( c >= '0xf0' && c <= '0xff' ) {
|
||||
count = 4;
|
||||
}
|
||||
|
||||
/* xfer data to caller */
|
||||
memcpy(ptr, file->buffer, want);
|
||||
ptr[want] = 0;/* always null terminate */
|
||||
|
||||
use_buffer(file, want);
|
||||
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
ptr = NULL;
|
||||
errno = EBADF;
|
||||
break;
|
||||
}
|
||||
|
||||
return ptr;/*success */
|
||||
return use_buffer( file, c );
|
||||
}
|
||||
|
||||
void url_rewind(URL_FILE *file)
|
||||
{
|
||||
switch(file->type) {
|
||||
case CFTYPE_FILE:
|
||||
rewind(file->handle.file); /* passthrough */
|
||||
break;
|
||||
URL_FILE *url_fopen( const char *url, const char *operation ) {
|
||||
/* this code could check for URLs or types in the 'url' and
|
||||
basically use the real fopen() for standard files */
|
||||
|
||||
case CFTYPE_CURL:
|
||||
/* halt transaction */
|
||||
curl_multi_remove_handle(multi_handle, file->handle.curl);
|
||||
URL_FILE *file;
|
||||
( void ) operation;
|
||||
|
||||
/* restart */
|
||||
curl_multi_add_handle(multi_handle, file->handle.curl);
|
||||
file = calloc( 1, sizeof( URL_FILE ) );
|
||||
if ( !file )
|
||||
return NULL;
|
||||
|
||||
/* ditch buffer - write will recreate - resets stream pos*/
|
||||
free(file->buffer);
|
||||
file->buffer = NULL;
|
||||
file->buffer_pos = 0;
|
||||
file->buffer_len = 0;
|
||||
file->handle.file = fopen( url, operation );
|
||||
if ( file->handle.file )
|
||||
file->type = CFTYPE_FILE; /* marked as URL */
|
||||
|
||||
break;
|
||||
else {
|
||||
file->type = CFTYPE_CURL; /* marked as URL */
|
||||
file->handle.curl = curl_easy_init( );
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
break;
|
||||
}
|
||||
curl_easy_setopt( file->handle.curl, CURLOPT_URL, url );
|
||||
curl_easy_setopt( file->handle.curl, CURLOPT_WRITEDATA, file );
|
||||
curl_easy_setopt( file->handle.curl, CURLOPT_VERBOSE, 0L );
|
||||
curl_easy_setopt( file->handle.curl, CURLOPT_WRITEFUNCTION,
|
||||
write_callback );
|
||||
|
||||
if ( !multi_handle )
|
||||
multi_handle = curl_multi_init( );
|
||||
|
||||
curl_multi_add_handle( multi_handle, file->handle.curl );
|
||||
|
||||
/* lets start the fetch */
|
||||
curl_multi_perform( multi_handle, &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 */
|
||||
curl_multi_remove_handle( multi_handle, file->handle.curl );
|
||||
|
||||
/* cleanup */
|
||||
curl_easy_cleanup( file->handle.curl );
|
||||
|
||||
free( file );
|
||||
|
||||
file = NULL;
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
int url_fclose( URL_FILE * file ) {
|
||||
int ret = 0; /* default is good return */
|
||||
|
||||
switch ( file->type ) {
|
||||
case CFTYPE_FILE:
|
||||
ret = fclose( file->handle.file ); /* passthrough */
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
/* make sure the easy handle is not in the multi handle anymore */
|
||||
curl_multi_remove_handle( multi_handle, file->handle.curl );
|
||||
|
||||
/* cleanup */
|
||||
curl_easy_cleanup( file->handle.curl );
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
ret = EOF;
|
||||
errno = EBADF;
|
||||
break;
|
||||
}
|
||||
|
||||
free( file->buffer ); /* free any allocated buffer space */
|
||||
free( file );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int url_feof( URL_FILE * file ) {
|
||||
int ret = 0;
|
||||
|
||||
switch ( file->type ) {
|
||||
case CFTYPE_FILE:
|
||||
ret = feof( file->handle.file );
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
if ( ( file->buffer_pos == 0 ) && ( !file->still_running ) )
|
||||
ret = 1;
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
ret = -1;
|
||||
errno = EBADF;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t url_fread( void *ptr, size_t size, size_t nmemb, URL_FILE * file ) {
|
||||
size_t want;
|
||||
|
||||
switch ( file->type ) {
|
||||
case CFTYPE_FILE:
|
||||
want = fread( ptr, size, nmemb, file->handle.file );
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
want = nmemb * size;
|
||||
|
||||
fill_buffer( file, want );
|
||||
|
||||
/* check if there's data in the buffer - if not fill_buffer()
|
||||
* either errored or EOF */
|
||||
if ( !file->buffer_pos )
|
||||
return 0;
|
||||
|
||||
/* ensure only available data is considered */
|
||||
if ( file->buffer_pos < want )
|
||||
want = file->buffer_pos;
|
||||
|
||||
/* xfer data to caller */
|
||||
memcpy( ptr, file->buffer, want );
|
||||
|
||||
use_buffer( file, want );
|
||||
|
||||
want = want / size; /* number of items */
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
want = 0;
|
||||
errno = EBADF;
|
||||
break;
|
||||
|
||||
}
|
||||
return want;
|
||||
}
|
||||
|
||||
char *url_fgets( char *ptr, size_t size, URL_FILE * file ) {
|
||||
size_t want = size - 1; /* always need to leave room for zero termination */
|
||||
size_t loop;
|
||||
|
||||
switch ( file->type ) {
|
||||
case CFTYPE_FILE:
|
||||
ptr = fgets( ptr, ( int ) size, file->handle.file );
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
fill_buffer( file, want );
|
||||
|
||||
/* check if there's data in the buffer - if not fill either errored or
|
||||
* EOF */
|
||||
if ( !file->buffer_pos )
|
||||
return NULL;
|
||||
|
||||
/* ensure only available data is considered */
|
||||
if ( file->buffer_pos < want )
|
||||
want = file->buffer_pos;
|
||||
|
||||
/*buffer contains data */
|
||||
/* look for newline or eof */
|
||||
for ( loop = 0; loop < want; loop++ ) {
|
||||
if ( file->buffer[loop] == '\n' ) {
|
||||
want = loop + 1; /* include newline */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* xfer data to caller */
|
||||
memcpy( ptr, file->buffer, want );
|
||||
ptr[want] = 0; /* always null terminate */
|
||||
|
||||
use_buffer( file, want );
|
||||
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
ptr = NULL;
|
||||
errno = EBADF;
|
||||
break;
|
||||
}
|
||||
|
||||
return ptr; /*success */
|
||||
}
|
||||
|
||||
void url_rewind( URL_FILE * file ) {
|
||||
switch ( file->type ) {
|
||||
case CFTYPE_FILE:
|
||||
rewind( file->handle.file ); /* passthrough */
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
/* halt transaction */
|
||||
curl_multi_remove_handle( multi_handle, file->handle.curl );
|
||||
|
||||
/* restart */
|
||||
curl_multi_add_handle( multi_handle, file->handle.curl );
|
||||
|
||||
/* ditch buffer - write will recreate - resets stream pos */
|
||||
free( file->buffer );
|
||||
file->buffer = NULL;
|
||||
file->buffer_pos = 0;
|
||||
file->buffer_len = 0;
|
||||
|
||||
break;
|
||||
|
||||
default: /* unknown or supported type - oh dear */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -415,153 +431,79 @@ void url_rewind(URL_FILE *file)
|
|||
* @param f the file to be wrapped;
|
||||
* @return the new handle, or null if no such handle could be allocated.
|
||||
*/
|
||||
URL_FILE * file_to_url_file( FILE* f) {
|
||||
URL_FILE * result = (URL_FILE *)malloc(sizeof(URL_FILE));
|
||||
URL_FILE *file_to_url_file( FILE * f ) {
|
||||
URL_FILE *result = ( URL_FILE * ) malloc( sizeof( URL_FILE ) );
|
||||
|
||||
if ( result != NULL) {
|
||||
result->type = CFTYPE_FILE,
|
||||
result->handle.file = f;
|
||||
}
|
||||
if ( result != NULL ) {
|
||||
result->type = CFTYPE_FILE, result->handle.file = f;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get one wide character from the buffer.
|
||||
*
|
||||
* @param file the stream to read from;
|
||||
* @return the next wide character on the stream, or zero if no more.
|
||||
*/
|
||||
wint_t url_fgetwc(URL_FILE *input) {
|
||||
wint_t result = 0;
|
||||
wint_t url_fgetwc( URL_FILE * input ) {
|
||||
wint_t result = -1;
|
||||
|
||||
switch(input->type) {
|
||||
case CFTYPE_FILE:
|
||||
fwide( input->handle.file, 1 ); /* wide characters */
|
||||
result = fgetc(input->handle.file); /* passthrough */
|
||||
break;
|
||||
switch ( input->type ) {
|
||||
case CFTYPE_FILE:
|
||||
fwide( input->handle.file, 1 ); /* wide characters */
|
||||
result = fgetwc( input->handle.file ); /* passthrough */
|
||||
break;
|
||||
|
||||
case CFTYPE_CURL:
|
||||
if (input.buffer_len != 0) {
|
||||
if ( input.wide_buffer == NULL) {
|
||||
/* not initialised */
|
||||
input.wide_buffer = calloc( input.buffer_len, sizeof(wint_t));
|
||||
}
|
||||
case CFTYPE_CURL:{
|
||||
wchar_t *wbuff = calloc( 2, sizeof( wchar_t ) );
|
||||
char *cbuff = calloc( 5, sizeof( char ) );
|
||||
|
||||
size_t len = wcslen(input.wide_buffer);
|
||||
if (input.still_running ||
|
||||
len == 0 ||
|
||||
len >= input.wide_cursor) {
|
||||
/* refresh the wide buffer */
|
||||
mbstowcs(input.wide_buffer, input.buffer, input.buffer_pos);
|
||||
}
|
||||
url_fread( cbuff, sizeof( char ), 4, input );
|
||||
mbstowcs( wbuff, cbuff, 1 );
|
||||
result = wbuff[0];
|
||||
use_one_wide( input );
|
||||
|
||||
result = input.wide_buffer[input.wide_cursor] ++;
|
||||
|
||||
/* do something to fread (advance) one utf character */
|
||||
free( cbuff );
|
||||
free( wbuff );
|
||||
}
|
||||
break;
|
||||
case CFTYPE_NONE:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* #define FGETSFILE "fgets.test" */
|
||||
/* #define FREADFILE "fread.test" */
|
||||
/* #define REWINDFILE "rewind.test" */
|
||||
wint_t url_ungetwc( wint_t wc, URL_FILE * input ) {
|
||||
wint_t result = -1;
|
||||
|
||||
/* /\* Small main program to retrieve 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[]) */
|
||||
/* { */
|
||||
/* URL_FILE *handle; */
|
||||
/* FILE *outf; */
|
||||
switch ( input->type ) {
|
||||
case CFTYPE_FILE:
|
||||
fwide( input->handle.file, 1 ); /* wide characters */
|
||||
result = fgetwc( input->handle.file ); /* passthrough */
|
||||
break;
|
||||
|
||||
/* size_t nread; */
|
||||
/* char buffer[256]; */
|
||||
/* const char *url; */
|
||||
case CFTYPE_CURL:{
|
||||
wchar_t *wbuff = calloc( 2, sizeof( wchar_t ) );
|
||||
char *cbuff = calloc( 5, sizeof( char ) );
|
||||
|
||||
/* if(argc < 2) */
|
||||
/* url = "http://192.168.7.3/testfile";/\* default to testurl *\/ */
|
||||
/* else */
|
||||
/* url = argv[1];/\* use passed url *\/ */
|
||||
wbuff[0] = wc;
|
||||
result = wcstombs( cbuff, wbuff, 1 );
|
||||
|
||||
/* /\* copy from url line by line with fgets *\/ */
|
||||
/* outf = fopen(FGETSFILE, "wb+"); */
|
||||
/* if(!outf) { */
|
||||
/* perror("couldn't open fgets output file\n"); */
|
||||
/* return 1; */
|
||||
/* } */
|
||||
input->buffer_pos -= strlen( cbuff );
|
||||
|
||||
/* handle = url_fopen(url, "r"); */
|
||||
/* if(!handle) { */
|
||||
/* printf("couldn't url_fopen() %s\n", url); */
|
||||
/* fclose(outf); */
|
||||
/* return 2; */
|
||||
/* } */
|
||||
free( cbuff );
|
||||
free( wbuff );
|
||||
|
||||
/* while(!url_feof(handle)) { */
|
||||
/* url_fgets(buffer, sizeof(buffer), handle); */
|
||||
/* fwrite(buffer, 1, strlen(buffer), outf); */
|
||||
/* } */
|
||||
result = result > 0 ? wc : result;
|
||||
break;
|
||||
case CFTYPE_NONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* url_fclose(handle); */
|
||||
|
||||
/* fclose(outf); */
|
||||
|
||||
|
||||
/* /\* Copy from url with fread *\/ */
|
||||
/* outf = fopen(FREADFILE, "wb+"); */
|
||||
/* if(!outf) { */
|
||||
/* perror("couldn't open fread output file\n"); */
|
||||
/* return 1; */
|
||||
/* } */
|
||||
|
||||
/* handle = url_fopen("testfile", "r"); */
|
||||
/* if(!handle) { */
|
||||
/* printf("couldn't url_fopen() testfile\n"); */
|
||||
/* fclose(outf); */
|
||||
/* return 2; */
|
||||
/* } */
|
||||
|
||||
/* do { */
|
||||
/* nread = url_fread(buffer, 1, sizeof(buffer), handle); */
|
||||
/* fwrite(buffer, 1, nread, outf); */
|
||||
/* } while(nread); */
|
||||
|
||||
/* url_fclose(handle); */
|
||||
|
||||
/* fclose(outf); */
|
||||
|
||||
|
||||
/* /\* Test rewind *\/ */
|
||||
/* outf = fopen(REWINDFILE, "wb+"); */
|
||||
/* if(!outf) { */
|
||||
/* perror("couldn't open fread output file\n"); */
|
||||
/* return 1; */
|
||||
/* } */
|
||||
|
||||
/* handle = url_fopen("testfile", "r"); */
|
||||
/* if(!handle) { */
|
||||
/* printf("couldn't url_fopen() testfile\n"); */
|
||||
/* fclose(outf); */
|
||||
/* return 2; */
|
||||
/* } */
|
||||
|
||||
/* nread = url_fread(buffer, 1, sizeof(buffer), handle); */
|
||||
/* fwrite(buffer, 1, nread, outf); */
|
||||
/* url_rewind(handle); */
|
||||
|
||||
/* buffer[0]='\n'; */
|
||||
/* fwrite(buffer, 1, 1, outf); */
|
||||
|
||||
/* nread = url_fread(buffer, 1, sizeof(buffer), handle); */
|
||||
/* fwrite(buffer, 1, nread, outf); */
|
||||
|
||||
/* url_fclose(handle); */
|
||||
|
||||
/* fclose(outf); */
|
||||
|
||||
/* return 0;/\* all done *\/ */
|
||||
/* } */
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@
|
|||
* Modifications to read/write wide character streams by
|
||||
* Simon Brooke.
|
||||
*
|
||||
* NOTE THAT: for my purposes, I'm only interested in wide characters,
|
||||
* and I always read them one character at a time.
|
||||
*
|
||||
* Copyright (c) 2003, 2017 Simtec Electronics
|
||||
* Some portions (c) 2019 Simon Brooke <simon@journeyman.cc>
|
||||
*
|
||||
|
|
@ -44,41 +47,41 @@
|
|||
#include <wchar.h>
|
||||
#include <wctype.h>
|
||||
|
||||
#define url_fwprintf(f, ...) ((f->type = CFTYPE_FILE) ? fwprintf( f->handle.file, __VA_ARGS__) : -1)
|
||||
#define url_fputws(ws, f) ((f->type = CFTYPE_FILE) ? fputws(ws, f->handle.file) : 0)
|
||||
#define url_fputwc(wc, f) ((f->type = CFTYPE_FILE) ? fputwc(wc, f->handle.file) : 0)
|
||||
|
||||
enum fcurl_type_e {
|
||||
CFTYPE_NONE = 0,
|
||||
CFTYPE_FILE = 1,
|
||||
CFTYPE_CURL = 2
|
||||
CFTYPE_NONE = 0,
|
||||
CFTYPE_FILE = 1,
|
||||
CFTYPE_CURL = 2
|
||||
};
|
||||
|
||||
struct fcurl_data
|
||||
{
|
||||
enum fcurl_type_e type; /* type of handle */
|
||||
union {
|
||||
CURL *curl;
|
||||
FILE *file;
|
||||
} handle; /* handle */
|
||||
struct fcurl_data {
|
||||
enum fcurl_type_e type; /* type of handle */
|
||||
union {
|
||||
CURL *curl;
|
||||
FILE *file;
|
||||
} handle; /* handle */
|
||||
|
||||
char *buffer; /* buffer to store cached data*/
|
||||
wchar_t *wide_buffer; /* wide character buffer */
|
||||
size_t buffer_len; /* currently allocated buffer's length */
|
||||
size_t buffer_pos; /* end of data in buffer*/
|
||||
size_t wide_cursor; /* cursor into the wide buffer */
|
||||
int still_running; /* Is background url fetch still in progress */
|
||||
char *buffer; /* buffer to store cached data */
|
||||
size_t buffer_len; /* currently allocated buffer's length */
|
||||
size_t buffer_pos; /* cursor into in buffer */
|
||||
int still_running; /* Is background url fetch still in progress */
|
||||
};
|
||||
|
||||
typedef struct fcurl_data URL_FILE;
|
||||
|
||||
/* exported functions */
|
||||
URL_FILE *url_fopen(const char *url, const char *operation);
|
||||
int url_fclose(URL_FILE *file);
|
||||
int url_feof(URL_FILE *file);
|
||||
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file);
|
||||
char *url_fgets(char *ptr, size_t size, URL_FILE *file);
|
||||
void url_rewind(URL_FILE *file);
|
||||
|
||||
wint_t url_fgetwc(URL_FILE *file);
|
||||
URL_FILE * file_to_url_file( FILE* f);
|
||||
|
||||
URL_FILE *url_fopen( const char *url, const char *operation );
|
||||
int url_fclose( URL_FILE * file );
|
||||
int url_feof( URL_FILE * file );
|
||||
size_t url_fread( void *ptr, size_t size, size_t nmemb, URL_FILE * file );
|
||||
char *url_fgets( char *ptr, size_t size, URL_FILE * file );
|
||||
void url_rewind( URL_FILE * file );
|
||||
|
||||
wint_t url_fgetwc( URL_FILE * file );
|
||||
wint_t url_ungetwc( wint_t wc, URL_FILE * input );
|
||||
URL_FILE *file_to_url_file( FILE * f );
|
||||
|
||||
#endif
|
||||
|
|
|
|||
177
src/io/io.c
Normal file
177
src/io/io.c
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* io.c
|
||||
*
|
||||
* Communication between PSSE and the outside world, via libcurl.
|
||||
*
|
||||
* (c) 2019 Simon Brooke <simon@journeyman.cc>
|
||||
* Licensed under GPL version 2.0, or, at your option, any later version.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "conspage.h"
|
||||
#include "consspaceobject.h"
|
||||
#include "debug.h"
|
||||
#include "fopen.h"
|
||||
#include "lispops.h"
|
||||
|
||||
/**
|
||||
* Convert this lisp string-like-thing (also works for symbols, and, later
|
||||
* keywords) into a UTF-8 string. NOTE that the returned value has been
|
||||
* malloced and must be freed. TODO: candidate to moving into a utilities
|
||||
* file.
|
||||
*
|
||||
* @param s the lisp string or symbol;
|
||||
* @return the c string.
|
||||
*/
|
||||
char *lisp_string_to_c_string( struct cons_pointer s ) {
|
||||
char *result = NULL;
|
||||
|
||||
if ( stringp( s ) || symbolp( s ) ) {
|
||||
int len = 0;
|
||||
|
||||
for ( struct cons_pointer c = s; !nilp( c );
|
||||
c = pointer2cell( c ).payload.string.cdr ) {
|
||||
len++;
|
||||
}
|
||||
|
||||
wchar_t *buffer = calloc( len + 1, sizeof( wchar_t ) );
|
||||
/* worst case, one wide char = four utf bytes */
|
||||
result = calloc( ( len * 4 ) + 1, sizeof( char ) );
|
||||
|
||||
int i = 0;
|
||||
for ( struct cons_pointer c = s; !nilp( c );
|
||||
c = pointer2cell( c ).payload.string.cdr ) {
|
||||
buffer[i++] = pointer2cell( c ).payload.string.character;
|
||||
}
|
||||
|
||||
wcstombs( result, buffer, len );
|
||||
free( buffer );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function, sort-of: close the file indicated by my first arg, and return
|
||||
* nil. If the first arg is not a stream, does nothing. All other args are
|
||||
* ignored.
|
||||
*
|
||||
* * (close stream)
|
||||
*
|
||||
* @param frame my stack_frame.
|
||||
* @param frame_pointer a pointer to my stack_frame.
|
||||
* @param env my environment.
|
||||
* @return T if the stream was successfully closed, else NIL.
|
||||
*/
|
||||
struct cons_pointer
|
||||
lisp_close( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env ) {
|
||||
struct cons_pointer result = NIL;
|
||||
|
||||
if ( readp( frame->arg[0] ) || writep( frame->arg[0] ) ) {
|
||||
if ( url_fclose( pointer2cell( frame->arg[0] ).payload.stream.stream )
|
||||
== 0 ) {
|
||||
result = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function: return a stream open on the URL indicated by the first argument;
|
||||
* if a second argument is present and is non-nil, open it for reading. At
|
||||
* present, further arguments are ignored and there is no mechanism to open
|
||||
* to append, or error if the URL is faulty or indicates an unavailable
|
||||
* resource.
|
||||
*
|
||||
* * (read-char stream)
|
||||
*
|
||||
* @param frame my stack_frame.
|
||||
* @param frame_pointer a pointer to my stack_frame.
|
||||
* @param env my environment.
|
||||
* @return a string of one character, namely the next available character
|
||||
* on my stream, if any, else NIL.
|
||||
*/
|
||||
struct cons_pointer
|
||||
lisp_open( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env ) {
|
||||
struct cons_pointer result = NIL;
|
||||
|
||||
if ( stringp( frame->arg[0] ) ) {
|
||||
char *url = lisp_string_to_c_string( frame->arg[0] );
|
||||
|
||||
if ( nilp( frame->arg[1] ) ) {
|
||||
result = make_read_stream( url_fopen( url, "r" ) );
|
||||
} else {
|
||||
// TODO: anything more complex is a problem for another day.
|
||||
result = make_write_stream( url_fopen( url, "w" ) );
|
||||
}
|
||||
|
||||
free( url );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function: return the next character from the stream indicated by arg 0;
|
||||
* further arguments are ignored.
|
||||
*
|
||||
* * (read-char stream)
|
||||
*
|
||||
* @param frame my stack_frame.
|
||||
* @param frame_pointer a pointer to my stack_frame.
|
||||
* @param env my environment.
|
||||
* @return a string of one character, namely the next available character
|
||||
* on my stream, if any, else NIL.
|
||||
*/
|
||||
struct cons_pointer
|
||||
lisp_read_char( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env ) {
|
||||
struct cons_pointer result = NIL;
|
||||
|
||||
if ( readp( frame->arg[0] ) ) {
|
||||
result =
|
||||
make_string( url_fgetwc
|
||||
( pointer2cell( frame->arg[0] ).payload.stream.
|
||||
stream ), NIL );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function: return a string representing all characters from the stream
|
||||
* indicated by arg 0; further arguments are ignored.
|
||||
*
|
||||
* * (slurp stream)
|
||||
*
|
||||
* @param frame my stack_frame.
|
||||
* @param frame_pointer a pointer to my stack_frame.
|
||||
* @param env my environment.
|
||||
* @return a string of one character, namely the next available character
|
||||
* on my stream, if any, else NIL.
|
||||
*/
|
||||
struct cons_pointer
|
||||
lisp_slurp( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env ) {
|
||||
struct cons_pointer result = NIL;
|
||||
struct cons_pointer cdr = NIL;
|
||||
|
||||
if ( readp( frame->arg[0] ) ) {
|
||||
URL_FILE *stream = pointer2cell( frame->arg[0] ).payload.stream.stream;
|
||||
|
||||
for ( wint_t c = url_fgetwc( stream ); c != -1;
|
||||
c = url_fgetwc( stream ) ) {
|
||||
cdr = make_string( ( ( wchar_t ) c ), cdr );
|
||||
|
||||
if ( nilp( result ) ) {
|
||||
result = cdr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
28
src/io/io.h
Normal file
28
src/io/io.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
/*
|
||||
* io.h
|
||||
*
|
||||
* Communication between PSSE and the outside world, via libcurl.
|
||||
*
|
||||
* (c) 2019 Simon Brooke <simon@journeyman.cc>
|
||||
* Licensed under GPL version 2.0, or, at your option, any later version.
|
||||
*/
|
||||
|
||||
#ifndef __psse_io_h
|
||||
#define __psse_io_h
|
||||
|
||||
struct cons_pointer
|
||||
lisp_close( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env );
|
||||
struct cons_pointer
|
||||
lisp_open( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env );
|
||||
struct cons_pointer
|
||||
lisp_read_char( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env );
|
||||
struct cons_pointer
|
||||
lisp_slurp( struct stack_frame *frame, struct cons_pointer frame_pointer,
|
||||
struct cons_pointer env );
|
||||
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue