src/lib/ppc405lib/string_stream.c
// Effectively write all data to a circular buffer with wrapping semantics.
Effectively write all data to a circular buffer with wrapping semantics.
Effectively write all data to a circular buffer with wrapping semantics .
static int
wrapping_swrite(FILE* stream, const void* buf, size_t size, size_t* written)
{
    StringStream *string = (StringStream *)stream;
    size_t wrote;
    int rc;
    if (size >= string->size) { 
        string->next_read = 0;
        string->next_write = 0;
        string->flags |= STRING_STREAM_FULL;
        memcpy((void *)string->data, buf + (size - string->size), size);
    } else {
        if (string->flags & STRING_STREAM_FULL) {
            wrote = 0;
            rc = -EAGAIN;
        } else {
            rc = circular_swrite(stream, buf, size, &wrote);
        }
        if (rc) {
            string->flags &= ~STRING_STREAM_FULL;
            rc = circular_swrite(stream, buf + wrote, size - wrote, &wrote);
            if (rc) {
                SSX_PANIC(STRING_STREAM_BUG);
            }
            string->next_read = string->next_write;
            string->flags |= STRING_STREAM_FULL;
        }
    }
    *written = size;
    return 0;
}
static int wrapping_swrite ( FILE * stream , const void * buf , size_t size , size_t * written ) { StringStream * string = ( StringStream * ) stream ; size_t wrote ; int rc ; if ( size >= string -> size ) { string -> next_read = 0 ; string -> next_write = 0 ; string -> flags |= STRING_STREAM_FULL ; memcpy ( ( void * ) string -> data , buf + ( size - string -> size ) , size ) ; } else { if ( string -> flags & STRING_STREAM_FULL ) { wrote = 0 ; rc = - EAGAIN ; } else { rc = circular_swrite ( stream , buf , size , & wrote ) ; } if ( rc ) { string -> flags &= ~ STRING_STREAM_FULL ; rc = circular_swrite ( stream , buf + wrote , size - wrote , & wrote ) ; if ( rc ) { SSX_PANIC ( STRING_STREAM_BUG ) ; } string -> next_read = string -> next_write ; string -> flags |= STRING_STREAM_FULL ; } } * written = size ; return 0 ; }
Effectively write all data to a circular buffer with wrapping semantics.
Effectively write all data to a circular buffer with wrapping semantics .
// If size of data >= size of buffer // If the amount of data will fill or overflow the entire buffer size // then we effectively fill the buffer with the final bytes of data. // If the string is not full, try to fill it with the // circular_swrite(). // The string is full and we need to overflow. We know that size // is less than the buffer size, and the next_read == next_write. // Mark the stream not full so a new circular write will work, and // at the end reset the full condition.