From 83accb2be4ca526ce763d7bdf0f0debfb7d0c0b6 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Thu, 31 Jan 2019 22:39:32 +0000 Subject: [PATCH] #13: Fixed --- src/io/fopen.c | 14 +++++++----- src/io/io.c | 60 +++++++++++++++++++++++++++++--------------------- src/utils.c | 33 +++++++++++++++++++++++++++ src/utils.h | 15 +++++++++++++ 4 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/src/io/fopen.c b/src/io/fopen.c index d5e4cd6..d3ece5c 100644 --- a/src/io/fopen.c +++ b/src/io/fopen.c @@ -51,8 +51,9 @@ #ifdef FOPEN_STANDALONE CURLSH *io_share; #else -#include "io.h" #include "consspaceobject.h" +#include "io.h" +#include "utils.h" #endif @@ -210,10 +211,9 @@ URL_FILE *url_fopen( const char *url, const char *operation ) { return NULL; file->handle.file = fopen( url, operation ); - if ( file->handle.file ) - file->type = CFTYPE_FILE; /* marked as URL */ - - else { + if ( file->handle.file ) { + file->type = CFTYPE_FILE; /* marked as file */ + } else if ( index_of(':', url ) > -1 ) { file->type = CFTYPE_CURL; /* marked as URL */ file->handle.curl = curl_easy_init( ); @@ -247,7 +247,11 @@ URL_FILE *url_fopen( const char *url, const char *operation ) { file = NULL; } + } else { + file->type = CFTYPE_NONE; + /* not a file, and doesn't look like a URL. */ } + return file; } diff --git a/src/io/io.c b/src/io/io.c index 1f7191c..1cf3c9e 100644 --- a/src/io/io.c +++ b/src/io/io.c @@ -34,6 +34,7 @@ #include "integer.h" #include "intern.h" #include "lispops.h" +#include "utils.h" /** * The sharing hub for all connections. TODO: Ultimately this probably doesn't @@ -253,27 +254,6 @@ lisp_close( struct stack_frame *frame, struct cons_pointer frame_pointer, return result; } -int index_of( char c, char *s ) { - int i; - - for ( i = 0; s[i] != c && s[i] != 0; i++ ); - - return s[i] == c ? i : -1; -} - -char *trim( char *s ) { - int i; - - for ( i = strlen( s ); ( isblank( s[i] ) || iscntrl( s[i] ) ) && i >= 0; - i-- ) { - s[i] = '\0'; - } - for ( i = 0; ( isblank( s[i] ) || iscntrl( s[i] ) ) && s[i] != '\0'; i++ ); - - return ( char * ) &s[i]; -} - - struct cons_pointer add_meta_integer( struct cons_pointer meta, wchar_t *key, long int value ) { return @@ -289,6 +269,13 @@ struct cons_pointer add_meta_string( struct cons_pointer meta, wchar_t *key, /* \todo something goes wrong here: I sometimes get junk characters on the * end of the string. */ mbstowcs( buffer, value, strlen( value ) ); + + /* hack: get rid of 32766 as a junk character, to see whether there are + * others. */ + for (int i = 0; i < wcslen( buffer); i++) { + if (buffer[i] == (wchar_t)32766) buffer[i] = (wchar_t)0; + } + return make_cons( make_cons( c_string_to_lisp_keyword( key ), c_string_to_lisp_string( buffer ) ), meta ); } @@ -398,10 +385,6 @@ void collect_meta( struct cons_pointer stream, char *url ) { ( intmax_t ) statbuf.st_size ); meta = add_meta_time( meta, L"modified", &statbuf.st_mtime ); - - /* this is destructive change before the cell is released into the - * wild, and consequently permissible, just. */ - cell->payload.stream.meta = meta; } break; case CFTYPE_CURL: @@ -412,6 +395,10 @@ void collect_meta( struct cons_pointer stream, char *url ) { curl_easy_setopt( s->handle.curl, CURLOPT_HEADERDATA, stream ); break; } + + /* this is destructive change before the cell is released into the + * wild, and consequently permissible, just. */ + cell->payload.stream.meta = meta; } @@ -440,6 +427,29 @@ lisp_open( struct stack_frame *frame, struct cons_pointer frame_pointer, if ( nilp( frame->arg[1] ) ) { URL_FILE *stream = url_fopen( url, "r" ); + + debug_printf( DEBUG_IO, + L"lisp_open: stream @ %d, stream type = %d, stream handle = %d\n", + (int) &stream, (int)stream->type, (int)stream->handle.file); + + switch (stream->type) { + case CFTYPE_NONE: + return make_exception( + c_string_to_lisp_string( L"Could not open stream"), + frame_pointer); + break; + case CFTYPE_FILE: + if (stream->handle.file == NULL) { + return make_exception( + c_string_to_lisp_string( L"Could not open file"), + frame_pointer); + } + break; + case CFTYPE_CURL: + /* can't tell whether a URL is bad without reading it */ + break; + } + result = make_read_stream( stream, NIL ); } else { // TODO: anything more complex is a problem for another day. diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..5b22516 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,33 @@ +/* + * utils.c + * + * little generally useful functions which aren't in any way special to PSSE. + * + * (c) 2019 Simon Brooke + * Licensed under GPL version 2.0, or, at your option, any later version. + */ + +#include +#include +#include + + +int index_of( char c, char *s ) { + int i; + + for ( i = 0; s[i] != c && s[i] != 0; i++ ); + + return s[i] == c ? i : -1; +} + +char *trim( char *s ) { + int i; + + for ( i = strlen( s ); ( isblank( s[i] ) || iscntrl( s[i] ) ) && i >= 0; + i-- ) { + s[i] = '\0'; + } + for ( i = 0; ( isblank( s[i] ) || iscntrl( s[i] ) ) && s[i] != '\0'; i++ ); + + return ( char * ) &s[i]; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..e56fd6e --- /dev/null +++ b/src/utils.h @@ -0,0 +1,15 @@ +/* + * utils.h + * + * little generally useful functions which aren't in any way special to PSSE. + * + * (c) 2019 Simon Brooke + * Licensed under GPL version 2.0, or, at your option, any later version. + */ + +#ifndef __psse_utils_h +#define __psse_utils_h + +int index_of( char c, char *s ); +char *trim( char *s ); +#endif