X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=internals%2F_PDCLIB_io.h;h=27c33e7dd7a50d360ee58710bacfee8213ed524d;hb=d7f375a09a9912bb18ad42f1442fbf64311bfed6;hp=78e747c0edcc1df81a8184ca7a74372f4ef90a5a;hpb=af7b48b211498ff4ef7aba6db89b90b760cdd3d5;p=pdclib diff --git a/internals/_PDCLIB_io.h b/internals/_PDCLIB_io.h index 78e747c..27c33e7 100644 --- a/internals/_PDCLIB_io.h +++ b/internals/_PDCLIB_io.h @@ -1,20 +1,21 @@ -#ifndef __PDCLIB_IO_H -#define __PDCLIB_IO_H __PDCLIB_IO_H -#include "_PDCLIB_int.h" -#include "_PDCLIB_threadconfig.h" - -/* PDCLib internal I/O logic <_PDCLIB_io.h> +/* PDCLib I/O support <_PDCLIB_io.h> This file is part of the Public Domain C Library (PDCLib). Permission is granted to use, modify, and / or redistribute at will. */ +#ifndef __PDCLIB_IO_H +#define __PDCLIB_IO_H __PDCLIB_IO_H + +#include "_PDCLIB_int.h" +#include "_PDCLIB_threadconfig.h" + /* Flags for representing mode (see fopen()). Note these must fit the same status field as the _IO?BF flags in and the internal flags below. */ #define _PDCLIB_FREAD 8u #define _PDCLIB_FWRITE 16u -#define _PDCLIB_FAPPEND 32u +#define _PDCLIB_FAPPEND 32u #define _PDCLIB_FRW 64u #define _PDCLIB_FBIN 128u @@ -41,7 +42,7 @@ union _PDCLIB_fd #endif void * pointer; _PDCLIB_uintptr_t uval; - _PDCLIB_intptr_t sval; + _PDCLIB_intptr_t sval; }; /******************************************************************************/ @@ -179,6 +180,9 @@ struct _PDCLIB_file _PDCLIB_size_t bufsize; /* Size of buffer */ _PDCLIB_size_t bufidx; /* Index of current position in buffer */ _PDCLIB_size_t bufend; /* Index of last pre-read character in buffer */ +#ifdef _PDCLIB_NEED_EOL_TRANSLATION + _PDCLIB_size_t bufnlexp; /* Current position of buffer newline expansion */ +#endif _PDCLIB_size_t ungetidx; /* Number of ungetc()'ed characters */ unsigned char * ungetbuf; /* ungetc() buffer */ unsigned int status; /* Status flags; see above */ @@ -206,21 +210,43 @@ static inline _PDCLIB_size_t _PDCLIB_getchars( char * out, _PDCLIB_size_t n, { while ( stream->bufidx != stream->bufend && i != n) { - c = (unsigned char) - ( out[ i++ ] = stream->buffer[ stream->bufidx++ ] ); + c = (unsigned char) stream->buffer[ stream->bufidx++ ]; +#ifdef _PDCLIB_NEED_EOL_TRANSLATION + if ( !( stream->status & _PDCLIB_FBIN ) && c == '\r' ) + { + if ( stream->bufidx == stream->bufend ) + break; + + if ( stream->buffer[ stream->bufidx ] == '\n' ) + { + c = '\n'; + stream->bufidx++; + } + } +#endif + out[ i++ ] = c; + if( c == stopchar ) return i; } - if ( stream->bufidx == stream->bufend ) + if ( i != n ) { if( _PDCLIB_fillbuffer( stream ) == -1 ) { - return i; + break; } } } +#ifdef _PDCLIB_NEED_EOL_TRANSLATION + if ( i != n && stream->bufidx != stream->bufend ) + { + // we must have EOF'd immediately after a \r + out[ i++ ] = stream->buffer[ stream->bufidx++ ]; + } +#endif + return i; }