wi_string_t * wi_file_read_to_string(wi_file_t *file, wi_string_t *separator) { wi_string_t *string, *totalstring = NULL; wi_uinteger_t index, length; _WI_FILE_ASSERT_OPEN(file); while((string = wi_file_read(file, WI_FILE_BUFFER_SIZE))) { if(!totalstring) totalstring = wi_string_init(wi_string_alloc()); index = wi_string_index_of_string(string, separator, 0); if(index == WI_NOT_FOUND) { wi_string_append_string(totalstring, string); } else { length = wi_string_length(string); wi_string_delete_characters_from_index(string, index); wi_string_append_string(totalstring, string); wi_file_seek(file, wi_file_offset(file) - length + index + 1); break; } } return wi_autorelease(totalstring); }
static void wr_msg_400(wi_array_t *arguments) { wi_address_t *address; wr_transfer_t *transfer; transfer = wr_transfers_transfer_with_path(WI_ARRAY(arguments, 0)); if(!transfer) return; address = wi_copy(wr_address); wi_address_set_port(address, wi_address_port(address) + 1); transfer->state = WR_TRANSFER_RUNNING; transfer->offset = wi_string_uint64(WI_ARRAY(arguments, 1)); transfer->transferred = transfer->offset; transfer->key = wi_retain(WI_ARRAY(arguments, 2)); transfer->start_time = wi_time_interval(); transfer->socket = wi_socket_init_with_address(wi_socket_alloc(), address, WI_SOCKET_TCP); wi_socket_set_interactive(transfer->socket, false); if(!wi_socket_connect(transfer->socket, wr_socket_context, 15.0)) { wr_printf_prefix(WI_STR("Could not connect to %@: %m"), wi_address_string(address)); wr_transfer_stop(transfer); goto end; } wi_file_seek(transfer->file, transfer->offset); if(transfer->type == WR_TRANSFER_DOWNLOAD) { wi_socket_set_direction(transfer->socket, WI_SOCKET_READ); wr_runloop_add_socket(transfer->socket, wr_runloop_download_callback); } else { wi_socket_set_direction(transfer->socket, WI_SOCKET_WRITE); wr_runloop_add_socket(transfer->socket, wr_runloop_upload_callback); } wr_send_command_on_socket(transfer->socket, WI_STR("TRANSFER %#@"), transfer->key); wr_printf_prefix(WI_STR("Starting transfer of \"%@\""), transfer->name); wr_draw_transfers(true); end: wi_release(address); }
wi_string_t * wi_file_read_to_string(wi_file_t *file, wi_string_t *separator) { wi_string_t *string; uint32_t index, length; _WI_FILE_ASSERT_OPEN(file); while((string = wi_file_read(file, WI_FILE_BUFFER_SIZE))) { length = wi_string_length(string); index = wi_string_index_of_string(string, separator, 0); if(index == WI_NOT_FOUND) { if(length < WI_FILE_BUFFER_SIZE) return string; } else { wi_string_delete_characters_from_index(string, index); wi_file_seek(file, wi_file_offset(file) - length + index + 1); return string; } } return NULL; }
wi_boolean_t wi_config_write_file(wi_config_t *config) { wi_enumerator_t *enumerator; wi_file_t *file, *tmpfile; wi_string_t *string, *name, *value; wi_mutable_set_t *keys; wi_boolean_t write; file = wi_file_for_updating(config->path); if(!file) { wi_log_err(WI_STR("Could not open %@: %m"), config->path); return false; } tmpfile = wi_file_temporary_file(); wi_lock_lock(config->lock); keys = wi_autorelease(wi_mutable_copy(config->changes)); while((string = wi_file_read_line(file))) { if(wi_string_length(string) == 0) { wi_file_write_format(tmpfile, WI_STR("\n")); } else if(wi_string_has_prefix(string, WI_STR("#"))) { write = true; string = wi_string_substring_from_index(string, 1); if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("#%@\n"), string); } else { write = true; if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("%@\n"), string); } } enumerator = wi_set_data_enumerator(keys); while((name = wi_enumerator_next_data(enumerator))) _wi_config_write_setting_to_file(config, name, tmpfile); wi_mutable_set_remove_all_data(config->changes); wi_lock_unlock(config->lock); wi_file_truncate(file, 0); wi_file_seek(tmpfile, 0); while((string = wi_file_read_line(tmpfile))) wi_file_write_format(file, WI_STR("%@\n"), string); return true; }