void stream_write_string( stream_t* stream, const char* str ) { if( str ) stream_write( stream, str, string_length( str ) + ( stream_is_binary( stream ) ? 1 : 0 ) ); else if( stream_is_binary( stream ) ) stream_write( stream, &str, 1 ); //Points to null, so safe to use as ref value for null terminator char }
void stream_write_uint8( stream_t* stream, uint8_t data ) { if( stream_is_binary( stream ) ) stream_write( stream, &data, 1 ); else stream_write_string( stream, string_from_uint_static( (uint32_t)data, false, 0, 0 ) ); }
void stream_write_uint8(stream_t* stream, uint8_t data) { if (stream_is_binary(stream)) stream_write(stream, &data, 1); else { string_const_t value = string_from_uint_static((uint32_t)data, false, 0, 0); stream_write_string(stream, value.str, value.length); } }
void stream_write_string(stream_t* stream, const char* str, size_t length) { if (str && length) stream_write(stream, str, length); if (stream_is_binary(stream)) { char nullstr = 0; stream_write(stream, &nullstr, 1); } }
void stream_write_uint64( stream_t* stream, uint64_t data ) { if( stream_is_binary( stream ) ) { if( stream && stream->swap ) data = byteorder_swap64( data ); stream_write( stream, &data, 8 ); } else stream_write_string( stream, string_from_uint_static( data, false, 0, 0 ) ); }
void stream_write_int32( stream_t* stream, int32_t data ) { if( stream_is_binary( stream ) ) { if( stream && stream->swap ) byteorder_swap( &data, 4 ); stream_write( stream, &data, 4 ); } else stream_write_string( stream, string_from_int_static( data, 0, 0 ) ); }
void stream_write_float64( stream_t* stream, float64_t data ) { if( stream_is_binary( stream ) ) { if( stream && stream->swap ) byteorder_swap( &data, 8 ); stream_write( stream, &data, 8 ); } else stream_write_string( stream, string_from_real_static( (real)data, 0, 0, 0 ) ); }
uint8_t stream_read_uint8(stream_t* stream) { uint8_t value = 0; if (stream_is_binary(stream)) stream_read(stream, &value, 1); else { char buffer[6] = {0}; string_t str = stream_read_string_buffer(stream, buffer, 6); value = (uint8_t)string_to_uint(str.str, str.length, false); } return value; }
void stream_write_uint64(stream_t* stream, uint64_t data) { if (stream_is_binary(stream)) { if (stream->swap) data = byteorder_swap64(data); stream_write(stream, &data, 8); } else { string_const_t value = string_from_uint_static(data, false, 0, 0); stream_write_string(stream, value.str, value.length); } }
void stream_write_int32(stream_t* stream, int32_t data) { if (stream_is_binary(stream)) { if (stream->swap) data = (int32_t)byteorder_swap32((uint32_t)data); stream_write(stream, &data, 4); } else { string_const_t value = string_from_int_static(data, 0, 0); stream_write_string(stream, value.str, value.length); } }
uint8_t stream_read_uint8( stream_t* stream ) { uint8_t value = 0; if( stream_is_binary( stream ) ) stream_read( stream, &value, 1 ); else { char* str = stream_read_string( stream ); value = (uint8_t)string_to_uint( str, false ); string_deallocate( str ); } return value; }
void stream_write_bool(stream_t* stream, bool data) { if (stream_is_binary(stream)) { char c = (data ? 1 : 0); stream_write(stream, &c, 1); } else { if (data) stream_write(stream, "true", 4); else stream_write(stream, "false", 5); } }
int16_t stream_read_int16(stream_t* stream) { int16_t value = 0; if (stream_is_binary(stream)) { stream_read(stream, &value, 2); if (stream->swap) value = (int16_t)byteorder_swap16((uint16_t)value); } else { char buffer[8] = {0}; string_t str = stream_read_string_buffer(stream, buffer, 8); value = (int16_t)string_to_int(str.str, str.length); } return value; }
uint32_t stream_read_uint32(stream_t* stream) { uint32_t value = 0; if (stream_is_binary(stream)) { stream_read(stream, &value, 4); if (stream->swap) value = byteorder_swap32(value); } else { char buffer[12] = {0}; string_t str = stream_read_string_buffer(stream, buffer, 12); value = (uint32_t)string_to_uint(str.str, str.length, false); } return value; }
uint64_t stream_read_uint64(stream_t* stream) { uint64_t value = 0; if (stream_is_binary(stream)) { stream_read(stream, &value, 8); if (stream->swap) value = byteorder_swap64(value); } else { char buffer[22] = {0}; string_t str = stream_read_string_buffer(stream, buffer, 22); value = string_to_uint64(str.str, str.length, false); } return value; }
float64_t stream_read_float64( stream_t* stream ) { float64_t value = 0; if( stream_is_binary( stream ) ) { stream_read( stream, &value, 8 ); if( stream && stream->swap ) byteorder_swap( &value, 8 ); } else { char* str = stream_read_string( stream ); value = string_to_real( str ); string_deallocate( str ); } return value; }
uint64_t stream_read_uint64( stream_t* stream ) { uint64_t value = 0; if( stream_is_binary( stream ) ) { stream_read( stream, &value, 8 ); if( stream && stream->swap ) value = byteorder_swap64( value ); } else { char* str = stream_read_string( stream ); value = string_to_uint64( str, false ); string_deallocate( str ); } return value; }
int32_t stream_read_int32( stream_t* stream ) { int32_t value = 0; if( stream_is_binary( stream ) ) { stream_read( stream, &value, 4 ); if( stream && stream->swap ) byteorder_swap( &value, 4 ); } else { char* str = stream_read_string( stream ); value = (int32_t)string_to_int( str ); string_deallocate( str ); } return value; }
void stream_write_float64(stream_t* stream, float64_t data) { if (stream_is_binary(stream)) { if (stream->swap) { float64_cast_t cast; cast.fval = data; cast.uival = byteorder_swap64(cast.uival); stream_write(stream, &cast.ival, 8); } else { stream_write(stream, &data, 8); } } else { string_const_t value = string_from_real_static((real)data, 0, 0, 0); stream_write_string(stream, value.str, value.length); } }
bool stream_read_bool(stream_t* stream) { bool value = false; if (stream_is_binary(stream)) { char c = 0; stream_read(stream, &c, 1); value = (c ? true : false); } else { char buffer[6] = {0}; string_t str = stream_read_string_buffer(stream, buffer, 6); value = !(!str.length || string_equal(str.str, str.length, STRING_CONST("false")) || (string_equal(str.str, str.length, STRING_CONST("0")))); } return value; }
float64_t stream_read_float64(stream_t* stream) { float64_t value = 0; if (stream_is_binary(stream)) { stream_read(stream, &value, 8); if (stream->swap) { float64_cast_t cast; cast.fval = value; cast.uival = byteorder_swap64(cast.uival); value = cast.fval; } } else { char buffer[64] = {0}; string_t str = stream_read_string_buffer(stream, buffer, 64); value = string_to_float64(str.str, str.length); } return value; }
bool stream_read_bool( stream_t* stream ) { bool value = false; FOUNDATION_ASSERT( stream ); if( stream_is_binary( stream ) ) { char c = 0; stream_read( stream, &c, 1 ); value = ( c ? true : false ); } else { char* str = stream_read_string( stream ); value = !( !str || !string_length( str ) || string_equal( str, "false" ) || ( string_equal( str, "0" ) ) ); string_deallocate( str ); } return value; }
void stream_write_endl( stream_t* stream ) { if( !stream_is_binary( stream ) ) stream_write( stream, "\n", 1 ); stream_flush( stream ); }
string_t stream_read_string(stream_t* stream) { char buffer[128]; char* outbuffer = buffer; size_t outsize = sizeof(buffer); size_t cursize = 0; size_t read, i; bool binary = stream_is_binary(stream); if (!(stream->mode & STREAM_IN)) return (string_t) { 0, 0 }; if (stream_is_sequential(stream)) { //Single byte reading since we can't seek backwards (and don't want to block on network sockets) char c; if (!binary) { //Consume whitespace while (!stream_eos(stream)) { read = stream->vtable->read(stream, &c, 1); if (!read) break; if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) { buffer[cursize++] = c; break; } } } if (cursize > 0) { while (!stream_eos(stream)) { read = stream->vtable->read(stream, &c, 1); if (!read) break; if (!c) break; if (!binary && ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))) break; if (cursize + 1 >= outsize) { outsize += 512; if (outbuffer != buffer) { outbuffer = memory_reallocate(outbuffer, outsize, 0, cursize); } else { outbuffer = memory_allocate(0, outsize, 0, MEMORY_PERSISTENT); memcpy(outbuffer, buffer, sizeof(buffer)); } } outbuffer[cursize++] = c; } outbuffer[cursize] = 0; //lint !e661 } } else { if (!binary) { //Consume whitespace while (!stream_eos(stream)) { read = stream->vtable->read(stream, buffer, 16); if (!read) break; for (i = 0; i < read; ++i) { char c = buffer[i]; if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) break; } if (i < read) { stream_seek(stream, (ssize_t)i - (ssize_t)read, STREAM_SEEK_CURRENT); break; } } } while (!stream_eos(stream)) { if (outbuffer != buffer) read = stream->vtable->read(stream, buffer, sizeof(buffer)); else read = stream->vtable->read(stream, buffer + cursize, sizeof(buffer) - cursize); if (!read) break; for (i = 0; i < read; ++i) { char c = buffer[i]; if (!c) break; if (!binary && ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))) break; } if (i) { if (cursize + i >= outsize) { outsize += 512; if (outbuffer != buffer) { outbuffer = memory_reallocate(outbuffer, outsize, 0, cursize); } else { FOUNDATION_ASSERT(cursize == 0); //Or internal assumptions about code flow is incorrect outbuffer = memory_allocate(0, outsize, 0, MEMORY_PERSISTENT); memcpy(outbuffer, buffer, i); } } else if (outbuffer != buffer) memcpy(outbuffer + cursize, buffer, i); cursize += i; } if (i < sizeof(buffer)) { if ((i + 1) < read) stream_seek(stream, (ssize_t)(1 + i) - (ssize_t)read, STREAM_SEEK_CURRENT); break; } } outbuffer[cursize] = 0; } if (outbuffer == buffer) { if (cursize == 0) return (string_t) { 0, 0 }; outbuffer = memory_allocate(0, cursize + 1, 0, MEMORY_PERSISTENT); memcpy(outbuffer, buffer, cursize); outbuffer[cursize] = 0; } return (string_t) { outbuffer, cursize }; }
string_t stream_read_string_buffer(stream_t* stream, char* outbuffer, size_t size) { char buffer[128]; size_t cursize = 0; size_t read, i; bool binary = stream_is_binary(stream); if (!(stream->mode & STREAM_IN) || !outbuffer || !size) { if (outbuffer && size) outbuffer[0] = 0; return (string_t) { outbuffer, 0 }; } --size; if (stream_is_sequential(stream)) { //Single byte reading since we can't seek backwards (and don't want to block on network sockets) char c; if (!binary) { //Consume whitespace while (!stream_eos(stream)) { read = stream->vtable->read(stream, &c, 1); if (!read) break; if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) { outbuffer[cursize++] = c; break; } } } if (cursize > 0) { while (!stream_eos(stream) && (cursize < size)) { read = stream->vtable->read(stream, &c, 1); if (!read) break; if (!c) break; if (!binary && ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))) break; outbuffer[cursize++] = c; } } } else { if (!binary) { //Consume whitespace while (!stream_eos(stream)) { read = stream->vtable->read(stream, buffer, 16); if (!read) break; for (i = 0; i < read; ++i) { char c = buffer[i]; if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) break; } if (i < read) { stream_seek(stream, (ssize_t)i - (ssize_t)read, STREAM_SEEK_CURRENT); break; } } } while (!stream_eos(stream) && (cursize < size)) { read = stream->vtable->read(stream, buffer, 128); if (!read) break; for (i = 0; i < read; ++i) { char c = buffer[i]; if (!c) break; if (!binary && ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))) break; } if (!i) break; if (cursize + i > size) i = size - cursize; memcpy(outbuffer + cursize, buffer, i); cursize += i; if (i < 128) { if ((i + 1) < read) stream_seek(stream, (ssize_t)(1 + i) - (ssize_t)read, STREAM_SEEK_CURRENT); break; } } } if (cursize < size) outbuffer[cursize] = 0; return (string_t) {outbuffer, cursize}; }
char* stream_read_string( stream_t* stream ) { char buffer[128]; char* outbuffer; int outsize = 128; int cursize = 0; int read, i; bool binary = stream_is_binary( stream ); FOUNDATION_ASSERT( stream ); if( !( stream->mode & STREAM_IN ) ) return 0; FOUNDATION_ASSERT( stream->vtable->read ); outbuffer = memory_allocate( outsize, 0, MEMORY_PERSISTENT ); if( stream_is_sequential( stream ) ) { //Single byte reading since we can't seek backwards (and don't want to block on network sockets) char c; if( !binary ) { //Consume whitespace while( !stream_eos( stream ) ) { read = (int)stream->vtable->read( stream, &c, 1 ); if( !read ) break; if( ( c != ' ' ) && ( c != '\n' ) && ( c != '\r' ) && ( c != '\t' ) ) { outbuffer[cursize++] = c; break; } } } while( !stream_eos( stream ) ) { read = (int)stream->vtable->read( stream, &c, 1 ); if( !read ) break; if( !c ) break; if( !binary && ( ( c == ' ' ) || ( c == '\n' ) || ( c == '\r' ) || ( c == '\t' ) ) ) break; if( cursize + 1 > outsize ) { outsize += 512; outbuffer = memory_reallocate( outbuffer, outsize, 0, cursize ); } outbuffer[cursize++] = c; } outbuffer[cursize] = 0; } else { if( !binary ) { //Consume whitespace while( !stream_eos( stream ) ) { read = (int)stream->vtable->read( stream, buffer, 16 ); if( !read ) break; for( i = 0; i < read; ++i ) { char c = buffer[i]; if( ( c != ' ' ) && ( c != '\n' ) && ( c != '\r' ) && ( c != '\t' ) ) break; } if( i < read ) { stream_seek( stream, i - read, STREAM_SEEK_CURRENT ); break; } } } while( !stream_eos( stream ) ) { read = (int)stream->vtable->read( stream, buffer, 128 ); if( !read ) break; for( i = 0; i < read; ++i ) { char c = buffer[i]; if( !c ) break; if( !binary && ( ( c == ' ' ) || ( c == '\n' ) || ( c == '\r' ) || ( c == '\t' ) ) ) break; } if( i ) { if( cursize + i > outsize ) { outsize += 512; outbuffer = memory_reallocate( outbuffer, outsize, 0, cursize ); } memcpy( outbuffer + cursize, buffer, i ); cursize += i; } if( i < 128 ) { if( ( i + 1 ) < read ) stream_seek( stream, 1 + i - read, STREAM_SEEK_CURRENT ); break; } } outbuffer[cursize] = 0; } return outbuffer; }
uint64_t stream_read_string_buffer( stream_t* stream, char* outbuffer, uint64_t size ) { char buffer[128]; int cursize = 0; int read, i; bool binary = stream_is_binary( stream ); FOUNDATION_ASSERT( stream ); if( !( stream->mode & STREAM_IN ) || !size ) return 0; FOUNDATION_ASSERT( stream->vtable->read ); --size; //TODO: Implement per-byte reading for sequential streams if( !binary ) { //Consume whitespace while( !stream_eos( stream ) ) { read = (int)stream->vtable->read( stream, buffer, 16 ); if( !read ) break; for( i = 0; i < read; ++i ) { char c = buffer[i]; if( ( c != ' ' ) && ( c != '\n' ) && ( c != '\r' ) && ( c != '\t' ) ) break; } if( i < read ) { stream_seek( stream, i - read, STREAM_SEEK_CURRENT ); break; } } } while( !stream_eos( stream ) && ( cursize < (int)size ) ) { read = (int)stream->vtable->read( stream, buffer, 128 ); if( !read ) break; for( i = 0; i < read; ++i ) { char c = buffer[i]; if( !c ) break; if( !binary && ( ( c == ' ' ) || ( c == '\n' ) || ( c == '\r' ) || ( c == '\t' ) ) ) break; } if( !i ) break; if( cursize + i > (int)size ) i = (int)size - cursize; memcpy( outbuffer + cursize, buffer, i ); cursize += i; if( i < 128 ) { if( ( i + 1 ) < read ) stream_seek( stream, 1 + i - read, STREAM_SEEK_CURRENT ); break; } } outbuffer[cursize] = 0; return cursize; }