From b8f241c2c51ca00f981e42a3539da3a65dbcbd7d Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Sun, 27 Jan 2019 12:23:51 +0000 Subject: [PATCH] Progress, not working --- src/init.c | 4 +-- src/io/fopen.c | 48 +++++++++++++++++++++++++++--------- src/io/fopen.h | 16 ++++++++++-- src/memory/consspaceobject.h | 1 - 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/init.c b/src/init.c index e8a33a9..8f278bf 100644 --- a/src/init.c +++ b/src/init.c @@ -130,7 +130,7 @@ int main( int argc, char *argv[] ) { fwide( stdin, 1 ); fwide( stdout, 1 ); fwide( stderr, 1 ); - fwide( sink, 1 ); + fwide( sink->handle.file, 1 ); bind_value( L"*in*", make_read_stream( file_to_url_file(stdin) ) ); bind_value( L"*out*", make_write_stream( file_to_url_file(stdout) ) ); bind_value( L"*log*", make_write_stream( file_to_url_file(stderr) ) ); @@ -200,7 +200,7 @@ int main( int argc, char *argv[] ) { debug_dump_object( oblist, DEBUG_BOOTSTRAP ); if ( dump_at_end ) { - dump_pages( stdout ); + dump_pages( file_to_url_file(stdout) ); } return ( 0 ); diff --git a/src/io/fopen.c b/src/io/fopen.c index d13250f..14c95e8 100644 --- a/src/io/fopen.c +++ b/src/io/fopen.c @@ -3,8 +3,11 @@ * * adapted from https://curl.haxx.se/libcurl/c/fopen.html. * + * Modifications to read/write wide character streams by + * Simon Brooke. + * * Copyright (c) 2003, 2017 Simtec Electronics - * Some portions (c) 2017 Simon Brooke + * Some portions (c) 2019 Simon Brooke * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -41,11 +44,6 @@ #include #include -/* - * wide characters - */ -#include -#include #include "fopen.h" @@ -177,8 +175,11 @@ static int use_buffer(URL_FILE *file, size_t want) /* 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 */ @@ -187,6 +188,7 @@ static int use_buffer(URL_FILE *file, size_t want) (file->buffer_pos - want)); file->buffer_pos -= want; + // TODO: something to adjust the wide_cursor } return 0; } @@ -424,18 +426,40 @@ URL_FILE * file_to_url_file( FILE* f) { return result; } - -wint_t url_fgetwc(URL_FILE *file) { +/** + * 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; - switch(file->type) { + switch(input->type) { case CFTYPE_FILE: - fwide( file->handle.file, 1 ); /* wide characters */ - result = fgetc(file->handle.file); /* passthrough */ + fwide( input->handle.file, 1 ); /* wide characters */ + result = fgetc(input->handle.file); /* passthrough */ break; case CFTYPE_CURL: - url_fread(&result, sizeof(wint_t), 1, file); + if (input.buffer_len != 0) { + if ( input.wide_buffer == NULL) { + /* not initialised */ + input.wide_buffer = calloc( input.buffer_len, sizeof(wint_t)); + } + + 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); + } + + result = input.wide_buffer[input.wide_cursor] ++; + + /* do something to fread (advance) one utf character */ + } break; } diff --git a/src/io/fopen.h b/src/io/fopen.h index 9874ac7..83ea5a8 100644 --- a/src/io/fopen.h +++ b/src/io/fopen.h @@ -3,8 +3,12 @@ * * adapted from https://curl.haxx.se/libcurl/c/fopen.html. * + * + * Modifications to read/write wide character streams by + * Simon Brooke. + * * Copyright (c) 2003, 2017 Simtec Electronics - * Some portions (c) 2017 Simon Brooke + * Some portions (c) 2019 Simon Brooke * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -33,6 +37,12 @@ #ifndef __fopen_h #define __fopen_h +#include +/* + * wide characters + */ +#include +#include enum fcurl_type_e { CFTYPE_NONE = 0, @@ -49,8 +59,10 @@ struct fcurl_data } handle; /* handle */ char *buffer; /* buffer to store cached data*/ - size_t buffer_len; /* currently allocated buffers length */ + 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 */ }; diff --git a/src/memory/consspaceobject.h b/src/memory/consspaceobject.h index 8db8099..b3f587c 100644 --- a/src/memory/consspaceobject.h +++ b/src/memory/consspaceobject.h @@ -16,7 +16,6 @@ */ #include #include -#include #include "fopen.h"