wi_string_t * wi_file_read_to_string(wi_file_t *file, wi_string_t *separator) { wi_mutable_string_t *totalstring = NULL; wi_string_t *string; 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_mutable_string_alloc()); index = wi_string_index_of_string(string, separator, 0); if(index == WI_NOT_FOUND) { wi_mutable_string_append_string(totalstring, string); } else { length = wi_string_length(string); wi_mutable_string_append_string(totalstring, wi_string_substring_to_index(string, index)); wi_file_seek(file, wi_file_offset(file) - length + index + 1); break; } } wi_runtime_make_immutable(string); return wi_autorelease(totalstring); }
wi_terminal_buffer_t * wi_terminal_buffer_init_with_terminal(wi_terminal_buffer_t *buffer, wi_terminal_t *terminal) { buffer->terminal = terminal; buffer->textbuffer = wi_string_init_with_capacity(wi_mutable_string_alloc(), _WI_TERMINAL_TEXTBUFFER_CAPACITY); buffer->linebuffer = wi_array_init_with_capacity(wi_mutable_array_alloc(), _WI_TERMINAL_LINEBUFFER_CAPACITY); return buffer; }
wi_socket_t * wi_socket_init_with_address(wi_socket_t *_socket, wi_address_t *address, wi_socket_type_t type) { _socket->address = wi_copy(address); _socket->close = true; _socket->buffer = wi_string_init_with_capacity(wi_mutable_string_alloc(), WI_SOCKET_BUFFER_SIZE); _socket->type = type; _socket->sd = socket(wi_address_family(_socket->address), _socket->type, 0); if(_socket->sd < 0) { wi_error_set_errno(errno); wi_release(_socket); return NULL; } if(!_wi_socket_set_option_int(_socket, SOL_SOCKET, SO_REUSEADDR, 1)) { wi_release(_socket); return NULL; } #ifdef SO_REUSEPORT if(!_wi_socket_set_option_int(_socket, SOL_SOCKET, SO_REUSEPORT, 1)) { wi_release(_socket); return NULL; } #endif return _socket; }
wi_mutable_string_t * wi_mutable_string_with_format(wi_string_t *fmt, ...) { wi_mutable_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_mutable_string_alloc(), fmt, ap); va_end(ap); return wi_autorelease(string); }
wi_string_t * wi_file_read_to_end_of_file(wi_file_t *file) { wi_mutable_string_t *string; char buffer[WI_FILE_BUFFER_SIZE]; wi_integer_t bytes; string = wi_string_init(wi_mutable_string_alloc()); while((bytes = wi_file_read_buffer(file, buffer, sizeof(buffer)))) wi_mutable_string_append_bytes(string, buffer, bytes); wi_runtime_make_immutable(string); return wi_autorelease(string); }
void wi_error_set_libwired_error_with_string(int code, wi_string_t *string) { wi_string_t *errorstring; errorstring = wi_string_init_with_cstring(wi_mutable_string_alloc(), _wi_error_strings[code]); if(wi_string_length(string) > 0) { if(wi_string_length(errorstring) > 0) wi_mutable_string_append_string(errorstring, WI_STR(": ")); wi_mutable_string_append_string(errorstring, string); } wi_error_set_error_with_string(WI_ERROR_DOMAIN_LIBWIRED, code, errorstring); wi_release(errorstring); }
wi_string_t * wi_socket_read_string(wi_socket_t *socket, wi_time_interval_t timeout) { wi_mutable_string_t *string; char buffer[WI_SOCKET_BUFFER_SIZE]; int bytes = -1; string = wi_autorelease(wi_string_init_with_capacity(wi_mutable_string_alloc(), WI_SOCKET_BUFFER_SIZE)); bytes = _wi_socket_read_buffer(socket, timeout, buffer, WI_SOCKET_BUFFER_SIZE); if(bytes <= 0) return NULL; wi_mutable_string_append_bytes(string, buffer, bytes); wi_runtime_make_immutable(string); return string; }
void wi_error_set_libwired_error_with_string(int code, wi_string_t *string) { wi_error_t *error; error = _wi_error_get_error(); error->domain = WI_ERROR_DOMAIN_LIBWIRED; error->code = code; wi_release(error->string); error->string = wi_string_init_with_cstring(wi_mutable_string_alloc(), _wi_error_strings[error->code]); if(wi_string_length(string) > 0) { if(wi_string_length(error->string) > 0) wi_mutable_string_append_string(error->string, WI_STR(": ")); wi_mutable_string_append_string(error->string, string); } }
wi_string_encoding_t * wi_string_encoding_init_with_charset(wi_string_encoding_t *encoding, wi_string_t *charset, wi_string_encoding_options_t options) { encoding->charset = wi_copy(charset); encoding->target_encoding = wi_mutable_copy(charset); encoding->utf8_encoding = wi_string_init_with_utf8_string(wi_mutable_string_alloc(), "UTF-8"); encoding->options = options; if(options & WI_STRING_ENCODING_IGNORE) { wi_mutable_string_append_string(encoding->target_encoding, WI_STR("//IGNORE")); wi_mutable_string_append_string(encoding->utf8_encoding, WI_STR("//IGNORE")); } if(options & WI_STRING_ENCODING_TRANSLITERATE) { wi_mutable_string_append_string(encoding->target_encoding, WI_STR("//TRANSLIT")); wi_mutable_string_append_string(encoding->utf8_encoding, WI_STR("//TRANSLIT")); } return encoding; }
wi_boolean_t wr_send_command_on_socket(wi_socket_t *socket, wi_string_t *fmt, ...) { wi_mutable_string_t *string; wi_integer_t result; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_mutable_string_alloc(), fmt, ap); va_end(ap); wi_mutable_string_convert_encoding(string, wr_client_string_encoding, wr_server_string_encoding); result = wi_socket_write_format(socket, 15.0, WI_STR("%@%c"), string, WR_MESSAGE_SEPARATOR); if(result <= 0) wr_printf_prefix(WI_STR("Could not write to server: %m")); wi_release(string); return (result > 0); }
void wr_draw_header(void) { wi_mutable_string_t *topic; wi_size_t size; if(wr_current_window->topic) topic = wi_mutable_copy(wr_topic_topic(wr_current_window->topic)); else topic = wi_string_init(wi_mutable_string_alloc()); wi_terminal_adjust_string_to_fit_width(wr_terminal, topic); size = wi_terminal_size(wr_terminal); wi_terminal_move_printf(wr_terminal, wi_make_point(0, 0), WI_STR("%s%@%s"), WR_INTERFACE_COLOR, topic, WR_TERMINATE_COLOR); wi_terminal_move(wr_terminal, wi_make_point(rl_point % size.width, size.height - 2)); wi_release(topic); }
wi_string_t * wi_file_read(wi_file_t *file, wi_uinteger_t length) { wi_mutable_string_t *string; char buffer[WI_FILE_BUFFER_SIZE]; wi_integer_t bytes = -1; _WI_FILE_ASSERT_OPEN(file); string = wi_string_init_with_capacity(wi_mutable_string_alloc(), length); while(length > sizeof(buffer)) { bytes = wi_file_read_buffer(file, buffer, sizeof(buffer)); if(bytes <= 0) goto end; wi_mutable_string_append_bytes(string, buffer, bytes); length -= bytes; } if(length > 0) { bytes = wi_file_read_buffer(file, buffer, sizeof(buffer)); if(bytes <= 0) goto end; wi_mutable_string_append_bytes(string, buffer, bytes); } end: if(bytes <= 0) { wi_release(string); string = NULL; } wi_runtime_make_immutable(string); return wi_autorelease(string); }
void _wi_url_regenerate_string(wi_url_t *url) { wi_release(url->string); url->string = wi_string_init_with_format(wi_mutable_string_alloc(), WI_STR("%#@://"), url->scheme); if(url->user && wi_string_length(url->user) > 0) { wi_mutable_string_append_format(url->string, WI_STR("%#@"), url->user); if(url->password && wi_string_length(url->password) > 0) wi_mutable_string_append_format(url->string, WI_STR(":%#@"), url->password); wi_mutable_string_append_string(url->string, WI_STR("@")); } wi_mutable_string_append_format(url->string, WI_STR("%#@"), url->host); if(url->port > 0) wi_mutable_string_append_format(url->string, WI_STR(":%lu"), url->port); if(url->path) wi_mutable_string_append_string(url->string, url->path); else wi_mutable_string_append_string(url->string, WI_STR("/")); }
wi_mutable_string_t * wi_mutable_string(void) { return wi_autorelease(wi_string_init(wi_mutable_string_alloc())); }
wi_socket_t * wi_socket_init_with_descriptor(wi_socket_t *socket, int sd) { socket->sd = sd; socket->buffer = wi_string_init_with_capacity(wi_mutable_string_alloc(), WI_SOCKET_BUFFER_SIZE); return socket; }