void response_msg_open(int stream_num, char *msg_type) { char *msg1 = "{\"msg_type\":\""; char *msg2 = "\", \"msg\":"; stream_write_string(stream_num, msg1, strlen(msg1)); stream_write_string(stream_num, msg_type, strlen(msg_type)); stream_write_string(stream_num, msg2, strlen(msg2)); }
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 ) ); }
int hashify_generate_preamble(stream_t* output_file, string_t output_filename) { //Read and preserve everything before #pragma once in case it contains header comments to be preserved char line_buffer[HASHIFY_LINEBUFFER_LENGTH]; size_t capacity = 1024; string_t preamble = string_allocate(0, capacity); stream_t* prev_file = stream_open(STRING_ARGS(output_filename), STREAM_IN); memset(line_buffer, 0, sizeof(line_buffer)); while (prev_file && !stream_eos(prev_file)) { string_t line; string_const_t stripped_line; line = stream_read_line_buffer(prev_file, line_buffer, sizeof(line_buffer), '\n'); stripped_line = string_strip(STRING_ARGS(line), STRING_CONST("\n\r")); if ((string_find_string(STRING_ARGS(stripped_line), STRING_CONST("pragma"), 0) != STRING_NPOS) && (string_find_string(STRING_ARGS(stripped_line), STRING_CONST("once"), 0) != STRING_NPOS)) break; if (preamble.length + stripped_line.length + 1 >= capacity) { size_t newcapacity = capacity + 1024 + stripped_line.length; preamble.str = memory_reallocate(preamble.str, newcapacity, 0, capacity); capacity = newcapacity; } preamble = string_append(STRING_ARGS(preamble), capacity, STRING_ARGS(stripped_line)); if (line.length < sizeof(line_buffer)) preamble = string_append(STRING_ARGS(preamble), capacity, STRING_CONST(STRING_NEWLINE)); } stream_deallocate(prev_file); stream_seek(output_file, 0, STREAM_SEEK_BEGIN); if (preamble.length) stream_write_string(output_file, STRING_ARGS(preamble)); stream_write_string(output_file, STRING_CONST( "#pragma once\n\n" "#include <foundation/hash.h>\n\n" "/* ****** AUTOMATICALLY GENERATED, DO NOT EDIT ******\n" " Edit corresponding definitions file and rerun\n" " the foundation hashify tool to update this file */\n\n" )); string_deallocate(preamble.str); return 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_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 config_write( stream_t* stream, hash_t filter_section ) { config_section_t* csection; config_key_t* bucket; int key, ib, bsize; stream_set_binary( stream, false ); //TODO: If random access stream, update section if available, else append at end of stream //if( stream_is_sequential( stream ) ) { stream_write_format( stream, "[%s]", hash_to_string( filter_section ) ); stream_write_endl( stream ); csection = config_section( filter_section, false ); if( csection ) for( key = 0; key < CONFIG_KEY_BUCKETS; ++key ) { bucket = csection->key[ key ]; if( bucket ) for( ib = 0, bsize = array_size( bucket ); ib < bsize; ++ib ) { stream_write_format( stream, "\t%s\t\t\t\t= ", hash_to_string( bucket[ib].name ) ); switch( bucket[ib].type ) { case CONFIGVALUE_BOOL: stream_write_bool( stream, bucket[ib].bval ); break; case CONFIGVALUE_INT: stream_write_int64( stream, bucket[ib].ival ); break; case CONFIGVALUE_REAL: #if FOUNDATION_PLATFORM_REALSIZE == 64 stream_write_float64( stream, bucket[ib].rval ); #else stream_write_float32( stream, bucket[ib].rval ); #endif break; case CONFIGVALUE_STRING: case CONFIGVALUE_STRING_CONST: case CONFIGVALUE_STRING_VAR: case CONFIGVALUE_STRING_CONST_VAR: stream_write_string( stream, bucket[ib].sval ); break; default: break; } stream_write_endl( stream ); } } } }
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 ) ); }
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_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); } }
void stream_write_format(stream_t* stream, const char* format, size_t format_length, ...) { va_list list; string_t buffer; va_start(list, format_length); buffer = string_allocate_vformat(format, format_length, list); va_end(list); stream_write_string(stream, buffer.str, buffer.length); string_deallocate(buffer.str); }
void stream_print(int stream_num, type_t type, const void *data, bool append_comma) { char buff[32]; switch (type) { case TYPE_BOOL: case TYPE_UINT8: sprintf(buff, "%u", *(uint8_t *)data); break; case TYPE_UINT16: sprintf(buff, "%u", *(uint16_t *)data); break; case TYPE_UINT32: sprintf(buff, "%lu", *(uint32_t *)data); break; case TYPE_INT8: sprintf(buff, "%d", *(int8_t *)data); break; case TYPE_INT: sprintf(buff, "%d", *(int *)data); break; case TYPE_INT16: sprintf(buff, "%d", *(int16_t *)data); break; case TYPE_INT32: sprintf(buff, "%ld", *(int32_t *)data); break; case TYPE_FLOAT: //sprintf(buff, "%f", *(float *)data); dtostrf((*(float *)data), NULL, 2, buff); break; case TYPE_STRING: stream_write_string(stream_num, (char *)data, strlen(data)); return; default: break; } stream_write_string(stream_num, buff, strlen(buff)); if(append_comma) stream_write(stream_num, ','); }
void stream_write_format( stream_t* stream, const char* format, ... ) { va_list list; char* buffer; FOUNDATION_ASSERT( format ); va_start( list, format ); buffer = string_vformat( format, list ); va_end( list ); stream_write_string( stream, buffer ); string_deallocate( buffer ); }
int hashify_generate_preamble( stream_t* output_file ) { //Read and preserve everything before #pragma once in case it contains header comments to be preserved char line_buffer[HASHIFY_LINEBUFFER_LENGTH]; char* preamble = 0; while( !stream_eos( output_file ) ) { char* line; uint64_t read; read = stream_read_line_buffer( output_file, line_buffer, HASHIFY_LINEBUFFER_LENGTH-1, '\n' ); line = string_strip( line_buffer, "\n\r" ); if( ( string_find_string( line, "pragma", 0 ) != STRING_NPOS ) && ( string_find_string( line, "once", 0 ) != STRING_NPOS ) ) break; preamble = string_append( preamble, line ); if( read < HASHIFY_LINEBUFFER_LENGTH ) preamble = string_append( preamble, "\n" ); } stream_seek( output_file, 0, STREAM_SEEK_BEGIN ); stream_write_string( output_file, preamble ); stream_write_string( output_file, "#pragma once\n\n" "#include <foundation/foundation.h>\n\n" "/* ****** AUTOMATICALLY GENERATED, DO NOT EDIT ******\n" " Edit corresponding definitions file and rerun\n" " the foundation hashify tool to update this file */\n\n" ); string_deallocate( preamble ); return 0; }
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); } }
int console_write_string(const char *s) { return stream_write_string(_get_stream(), s); }
DECLARE_TEST( fs, file ) { char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); char* copypath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); stream_t* teststream = 0; if( !fs_is_directory( environment_temporary_directory() ) ) fs_make_directory( environment_temporary_directory() ); if( fs_is_directory( testpath ) ) fs_remove_directory( testpath ); fs_remove_file( testpath ); if( fs_is_directory( copypath ) ) fs_remove_directory( copypath ); fs_remove_file( copypath ); teststream = fs_open_file( testpath, STREAM_IN ); EXPECT_EQ( teststream, 0 ); EXPECT_FALSE( fs_is_file( testpath ) ); teststream = fs_open_file( testpath, STREAM_IN | STREAM_OUT ); EXPECT_NE( teststream, 0 ); EXPECT_TRUE( fs_is_file( testpath ) ); stream_deallocate( teststream ); fs_remove_file( testpath ); teststream = fs_open_file( testpath, STREAM_IN ); EXPECT_EQ( teststream, 0 ); EXPECT_FALSE( fs_is_file( testpath ) ); teststream = fs_open_file( testpath, STREAM_OUT ); EXPECT_NE( teststream, 0 ); EXPECT_TRUE( fs_is_file( testpath ) ); stream_deallocate( teststream ); teststream = 0; fs_copy_file( testpath, copypath ); EXPECT_TRUE( fs_is_file( copypath ) ); fs_remove_file( copypath ); EXPECT_FALSE( fs_is_file( copypath ) ); teststream = fs_open_file( testpath, STREAM_OUT ); EXPECT_NE( teststream, 0 ); EXPECT_TRUE( fs_is_file( testpath ) ); stream_write_string( teststream, "testing testing" ); stream_deallocate( teststream ); teststream = 0; fs_copy_file( testpath, copypath ); EXPECT_TRUE( fs_is_file( copypath ) ); fs_remove_file( copypath ); EXPECT_FALSE( fs_is_file( copypath ) ); stream_deallocate( teststream ); string_deallocate( testpath ); string_deallocate( copypath ); return 0; }
DECLARE_TEST( fs, monitor ) { char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); char* filetestpath = path_merge( testpath, string_from_int_static( random64(), 0, 0 ) ); stream_t* test_stream; event_stream_t* stream; event_block_t* block; event_t* event; stream = fs_event_stream(); if( fs_is_file( testpath ) ) fs_remove_file( testpath ); if( !fs_is_directory( testpath ) ) fs_make_directory( testpath ); if( fs_is_file( filetestpath ) ) fs_remove_file( filetestpath ); stream_deallocate( fs_open_file( filetestpath, STREAM_OUT ) ); fs_remove_file( filetestpath ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); fs_monitor( testpath ); thread_sleep( 1000 ); stream_deallocate( fs_open_file( filetestpath, STREAM_OUT ) ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_NE( event, 0 ); EXPECT_EQ( event->system, SYSTEM_FOUNDATION ); EXPECT_EQ( event->id, FOUNDATIONEVENT_FILE_CREATED ); EXPECT_STREQ( event->payload, filetestpath ); event = event_next( block, event ); EXPECT_EQ( event, 0 ); test_stream = fs_open_file( filetestpath, STREAM_IN | STREAM_OUT ); stream_write_string( test_stream, filetestpath ); stream_deallocate( test_stream ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_NE( event, 0 ); EXPECT_EQ( event->system, SYSTEM_FOUNDATION ); EXPECT_EQ( event->id, FOUNDATIONEVENT_FILE_MODIFIED ); EXPECT_STREQ( event->payload, filetestpath ); event = event_next( block, event ); EXPECT_EQ( event, 0 ); fs_remove_file( filetestpath ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_NE( event, 0 ); EXPECT_EQ( event->system, SYSTEM_FOUNDATION ); EXPECT_EQ( event->id, FOUNDATIONEVENT_FILE_DELETED ); EXPECT_STREQ( event->payload, filetestpath ); event = event_next( block, event ); EXPECT_EQ( event, 0 ); fs_unmonitor( testpath ); thread_sleep( 1000 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); stream_deallocate( fs_open_file( filetestpath, STREAM_OUT ) ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); fs_remove_file( filetestpath ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); fs_remove_directory( testpath ); string_deallocate( testpath ); string_deallocate( filetestpath ); return 0; }
void response_msg_close(int stream_num) { char *msg3 = "}\r\n"; stream_write_string(stream_num, msg3, strlen(msg3)); }
void response_msg_append_205(int stream_num) { char *msg = ", \"status\": 205"; stream_write_string(stream_num, msg, strlen(msg)); }