Exemple #1
0
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);
}
Exemple #2
0
static wi_string_t * _wi_p7_message_description(wi_runtime_instance_t *instance) {
	wi_p7_message_t			*p7_message = instance;
	wi_enumerator_t			*enumerator;
	wi_dictionary_t			*fields;
	wi_mutable_string_t		*string;
	wi_string_t				*field_name, *field_value;
	
	string = wi_mutable_string_with_format(WI_STR("<%@ %p>{name = %@, buffer = %@, fields = (\n"),
        wi_runtime_class_name(p7_message),
        p7_message,
		p7_message->name,
		wi_data_with_bytes_no_copy(p7_message->binary_buffer, p7_message->binary_size, false));
	
	fields		= wi_p7_message_fields(p7_message);
	enumerator	= wi_dictionary_key_enumerator(fields);
	
	while((field_name = wi_enumerator_next_data(enumerator))) {
		field_value = wi_dictionary_data_for_key(fields, field_name);

		wi_mutable_string_append_format(string, WI_STR("    %@ = %@\n"),
			field_name, field_value);
	}
	
	wi_mutable_string_append_string(string, WI_STR(")}"));
	
	wi_runtime_make_immutable(string);
	
	return string;
}
Exemple #3
0
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);
}
Exemple #4
0
static wi_string_t * _wi_set_description(wi_runtime_instance_t *instance) {
	wi_set_t				*set = instance;
	_wi_set_bucket_t		*bucket;
	wi_mutable_string_t		*string;
	wi_string_t				*description;
	wi_uinteger_t			i;

	string = wi_mutable_string_with_format(WI_STR("<%@ %p>{count = %lu, mutable = %u, values = (\n"),
		wi_runtime_class_name(set),
		set,
		set->data_count,
		wi_runtime_options(set) & WI_RUNTIME_OPTION_MUTABLE ? 1 : 0);

	for(i = 0; i < set->buckets_count; i++) {
		for(bucket = set->buckets[i]; bucket; bucket = bucket->next) {
			if(set->callbacks.description)
				description = (*set->callbacks.description)(bucket->data);
			else
				description = wi_string_with_format(WI_STR("%p"), bucket->data);

			wi_mutable_string_append_format(string, WI_STR("    %@\n"), description);
		}
	}
	
	wi_mutable_string_append_string(string, WI_STR(")}"));
	
	wi_runtime_make_immutable(string);

	return string;
}
static wi_string_t * _wi_dictionary_description(wi_runtime_instance_t *instance) {
    wi_dictionary_t             *dictionary = instance;
    _wi_dictionary_bucket_t     *bucket;
    wi_mutable_string_t         *string;
    wi_string_t                 *key_description, *value_description;
    wi_uinteger_t               i;

    string = wi_mutable_string_with_format(WI_STR("<%@ %p>{count = %lu, mutable = %u, values = (\n"),
                                           wi_runtime_class_name(dictionary),
                                           dictionary,
                                           dictionary->key_count,
                                           wi_runtime_options(dictionary) & WI_RUNTIME_OPTION_MUTABLE ? 1 : 0);

    for(i = 0; i < dictionary->buckets_count; i++) {
        for(bucket = dictionary->buckets[i]; bucket; bucket = bucket->next) {
            if(dictionary->key_callbacks.description)
                key_description = (*dictionary->key_callbacks.description)(bucket->key);
            else
                key_description = wi_string_with_format(WI_STR("%p"), bucket->key);

            if(dictionary->value_callbacks.description)
                value_description = (*dictionary->value_callbacks.description)(bucket->data);
            else
                value_description = wi_string_with_format(WI_STR("%p"), bucket->data);

            wi_mutable_string_append_format(string, WI_STR("    %@: %@\n"), key_description, value_description);
        }
    }

    wi_mutable_string_append_string(string, WI_STR(")}"));

    wi_runtime_make_immutable(string);

    return string;
}
Exemple #6
0
void wr_draw_transfers(wi_boolean_t force) {
	static wi_time_interval_t	update;
	wi_enumerator_t				*enumerator;
	wi_mutable_string_t			*string;
    wi_string_t					*status;
	wr_transfer_t				*transfer;
	wi_time_interval_t			interval;
	wi_uinteger_t				i = 0;
	
	interval = wi_time_interval();
	
	if(!force && interval - update < 1.0)
		return;
	
	update = interval;
	
	wi_terminal_set_scroll(wr_terminal, wi_make_range(1 + wi_array_count(wr_transfers),
													  wi_terminal_size(wr_terminal).height - 4));
	
	enumerator = wi_array_data_enumerator(wr_transfers);
	
	while((transfer = wi_enumerator_next_data(enumerator))) {
		wi_terminal_move(wr_terminal, wi_make_point(0, i + 1));
		wi_terminal_clear_line(wr_terminal);
		
		if(transfer->state == WR_TRANSFER_RUNNING && interval - transfer->start_time > 0.0) {
			transfer->speed = ((double) transfer->total_transferred - transfer->total_offset) / (interval - transfer->start_time);
			
			status = wi_string_with_format(WI_STR("%@/%@, %@/s"),
                                           wr_files_string_for_size(transfer->total_transferred),
                                           wr_files_string_for_size(transfer->total_size),
                                           wr_files_string_for_size(transfer->speed));
		}
		else if(transfer->state == WR_TRANSFER_QUEUED) {
			status = wi_string_with_format(WI_STR("queued at %u"),
                                           transfer->queue);
		}
		else {
			status = wi_string_with_cstring("waiting");
		}
		
		string = wi_mutable_string_with_format(WI_STR("%u %3.0f%%  %@"),
                                               transfer->tid,
                                               transfer->total_size > 0
                                               ? 100 * ((double) transfer->total_transferred / (double) transfer->total_size)
                                               : 0,
                                               transfer->name);
		
		wi_terminal_adjust_string_to_fit_width(wr_terminal, string);
		wi_mutable_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(status));
		wi_mutable_string_append_string(string, status);
		
		wi_terminal_printf(wr_terminal, WI_STR("%@"), string);
		
		i++;
	}
	
	wr_terminal_reset_location();
}
Exemple #7
0
wi_boolean_t wi_terminal_buffer_printf(wi_terminal_buffer_t *buffer, wi_string_t *fmt, ...) {
	wi_enumerator_t	*enumerator;
	wi_array_t		*array;
	wi_string_t		*string, *line;
	wi_point_t		location;
	va_list			ap;
	wi_boolean_t	result = false;
	
	va_start(ap, fmt);
	string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
	va_end(ap);
	
	array = _wi_terminal_buffer_lines_for_string(buffer, string);

	if(buffer->terminal->active_buffer == buffer) {
		if(buffer->line == wi_array_count(buffer->linebuffer)) {
			location = wi_terminal_location(buffer->terminal);
			wi_terminal_move(buffer->terminal, wi_make_point(0, wi_terminal_scroll(buffer->terminal).length));
			
			enumerator = wi_array_data_enumerator(array);
			
			while((line = wi_enumerator_next_data(enumerator))) {
				_wi_terminal_puts(buffer->terminal, WI_STR("\n"));
				_wi_terminal_puts(buffer->terminal, line);
				
				buffer->line++;
			}
			
			wi_terminal_move(buffer->terminal, location);
			
			result = true;
		}
	}
	
	if(wi_string_length(buffer->textbuffer) > 0)
		wi_mutable_string_append_string(buffer->textbuffer, WI_STR("\n"));

	wi_mutable_string_append_string(buffer->textbuffer, string);
	
	wi_mutable_array_add_data_from_array(buffer->linebuffer, array);
	
	wi_release(string);
	
	return result;
}
Exemple #8
0
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;
}
Exemple #10
0
void wi_terminal_adjust_string_to_fit_width(wi_terminal_t *terminal, wi_mutable_string_t *string) {
	wi_uinteger_t	index;
	
	if(wi_terminal_width_of_string(terminal, string) > terminal->co) {
		index = wi_terminal_index_of_string_for_width(terminal, string, terminal->co);
		
		wi_mutable_string_delete_characters_from_index(string, index);
	} else {
		while(wi_terminal_width_of_string(terminal, string) < terminal->co)
			wi_mutable_string_append_string(string, WI_STR(" "));
	}
}
Exemple #11
0
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("/"));
}
Exemple #12
0
static void wr_command_log(wi_array_t *arguments) {
	wi_mutable_string_t		*string;
	wi_string_t				*path;
	
	path = wi_user_home();
	path = wi_string_by_appending_path_component(path, WI_ARRAY(arguments, 0));

	string = wi_mutable_copy(wi_terminal_buffer_string(wr_window_buffer(wr_current_window)));

	wi_mutable_string_append_string(string, WI_STR("\n"));
	
	if(!wi_string_write_to_file(string, path))
		wr_printf_prefix(WI_STR("log: %@: %m"), path);
	else
		wr_printf_prefix(WI_STR("log: \"%@\" saved"), path);
	
	wi_release(string);
}
Exemple #13
0
static wi_string_t * _wi_number_description(wi_runtime_instance_t *instance) {
	wi_number_t				*number = instance;
	wi_mutable_string_t		*string;
	
	string = wi_mutable_string_with_format(WI_STR("<%@ %p>{value = "),
		wi_runtime_class_name(number),
		number);
	
	if(_wi_number_is_float(number))
		wi_mutable_string_append_format(string, WI_STR("%f"), wi_number_double(number));
	else
		wi_mutable_string_append_format(string, WI_STR("%lld"), wi_number_int64(number));
	
	wi_mutable_string_append_string(string, WI_STR("}"));
	
	wi_runtime_make_immutable(string);
	
	return string;
}
Exemple #14
0
static wi_string_t * _wi_data_description(wi_runtime_instance_t *instance) {
	wi_data_t				*data = instance;
	wi_mutable_string_t		*string;
	const unsigned char		*bytes;
	wi_uinteger_t			i;
	
	string		= wi_mutable_string();
	bytes		= data->bytes;
	
	for(i = 0; i < data->length; i++) {
		if(i > 0 && i % 4 == 0)
			wi_mutable_string_append_string(string, WI_STR(" "));

		wi_mutable_string_append_format(string, WI_STR("%02X"), bytes[i]);
	}
	
	wi_runtime_make_immutable(string);
	
	return string;
}
Exemple #15
0
wi_string_t * wi_socket_read_to_string(wi_socket_t *socket, wi_time_interval_t timeout, wi_string_t *separator) {
	wi_string_t		*string, *substring;
	wi_uinteger_t	index;
	
	index = wi_string_index_of_string(socket->buffer, separator, 0);
	
	if(index != WI_NOT_FOUND) {
		substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator));
		
		wi_mutable_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring)));

		return substring;
	}
	
	while((string = wi_socket_read_string(socket, timeout))) {
		if(wi_string_length(string) == 0)
			return string;

		wi_mutable_string_append_string(socket->buffer, string);

		index = wi_string_index_of_string(socket->buffer, separator, 0);
		
		if(index == WI_NOT_FOUND) {
			if(wi_string_length(socket->buffer) > _WI_SOCKET_BUFFER_MAX_SIZE) {
				wi_error_set_libwired_error_with_format(WI_ERROR_SOCKET_OVERFLOW, WI_STR("Buffer is %u bytes"), wi_string_length(socket->buffer));
				
				wi_mutable_string_set_string(socket->buffer, WI_STR(""));
			
				return NULL;
			}
		} else {
			substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator));
			
			wi_mutable_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring)));
			
			return substring;
		}
	}
	
	return NULL;
}
Exemple #16
0
void wr_draw_divider(void) {
	wi_enumerator_t			*enumerator;
	wi_mutable_string_t		*string, *action;
	wi_string_t				*position;
	wr_window_t				*window;
	wi_size_t				size;
	wi_range_t				scroll;
	wi_point_t				location;
	const char				*color;
	wi_uinteger_t			users, line, lines, windows;
	
	string = wi_mutable_string_with_format(WI_STR("%s%@"), WR_PREFIX, wr_nick);
	
	if(wr_connected && wr_server) {
		users = wi_array_count(wr_chat_users(wr_console_window->chat));
		
		wi_mutable_string_append_format(string, WI_STR(" - %@ - %u %@"),
										wr_server_name(wr_server),
										users,
										users == 1
											? WI_STR("user")
											: WI_STR("users"));
	}
	
	action = wi_mutable_string();
	windows = 0;
	
	enumerator = wi_array_data_enumerator(wr_windows);
	
	while((window = wi_enumerator_next_data(enumerator))) {
		switch(window->status) {
			case WR_WINDOW_STATUS_ACTION:
				color = WR_INTERFACE_COLOR;
				break;

			case WR_WINDOW_STATUS_CHAT:
				color = WR_STATUS_COLOR;
				break;

			case WR_WINDOW_STATUS_HIGHLIGHT:
				color = WR_HIGHLIGHT_COLOR;
				break;
				
			default:
				color = NULL;
				break;
		}
		
		if(color) {
			if(windows > 0)
				wi_mutable_string_append_string(action, WI_STR(","));
			
			wi_mutable_string_append_format(action, WI_STR("%s%s%s%u%s%s"),
				WR_TERMINATE_COLOR,
				WR_INTERFACE_COLOR,
				color,
				window->wid,
				WR_TERMINATE_COLOR,
				WR_INTERFACE_COLOR);
			
			windows++;
		}
	}

	if(windows > 0)
		wi_mutable_string_append_format(string, WI_STR(" [%@]"), action);
	
	line = wi_terminal_buffer_current_line(wr_current_window->buffer);
	lines = wi_terminal_buffer_lines(wr_current_window->buffer);
	scroll = wi_terminal_scroll(wr_terminal);
	
	if(lines == 0 || line == lines)
		position = NULL;
	else if(line <= scroll.length)
		position = wi_string_with_cstring("TOP");
	else
		position = wi_string_with_format(WI_STR("%.0f%%"), 100 * ((double) (line - scroll.length)  / (double) lines));
	
	wi_terminal_adjust_string_to_fit_width(wr_terminal, string);
	
	if(position) {
		wi_mutable_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(position));
		wi_mutable_string_append_string(string, position);
	}

	size = wi_terminal_size(wr_terminal);

	location = wi_terminal_location(wr_terminal);
	wi_terminal_move_printf(wr_terminal, wi_make_point(0, size.height - 3), WI_STR("%s%@%s"),
		WR_INTERFACE_COLOR,
		string,
		WR_TERMINATE_COLOR);
	wi_terminal_move(wr_terminal, location);
}