Esempio n. 1
0
void wi_test_set_runtime_functions(void) {
    wi_set_t            *set1;
    wi_mutable_set_t    *set2;
    
    set1 = wi_set_with_data(WI_STR("foo"), WI_STR("bar"), NULL);
    set2 = wi_autorelease(wi_mutable_copy(set1));
    
    WI_TEST_ASSERT_EQUAL_INSTANCES(set1, set2, "");
    WI_TEST_ASSERT_EQUALS(wi_hash(set1), wi_hash(set2), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id(set1), wi_set_runtime_id(), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id(set2), wi_set_runtime_id(), "");
    WI_TEST_ASSERT_TRUE(wi_runtime_options(set1) & WI_RUNTIME_OPTION_IMMUTABLE, "");
    WI_TEST_ASSERT_TRUE(wi_runtime_options(set2) & WI_RUNTIME_OPTION_MUTABLE, "");
    
    wi_mutable_set_remove_data(set2, WI_STR("bar"));
    wi_mutable_set_add_data(set2, WI_STR("baz"));
    
    WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(set1, set2, "");
    
    wi_mutable_set_remove_data(set2, WI_STR("baz"));
    
    WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(set1, set2, "");

    set2 = wi_autorelease(wi_set_init_with_capacity_and_callbacks(wi_mutable_set_alloc(), 0, false, wi_set_null_callbacks));
    
    wi_mutable_set_add_data(set2, (void *) 1);
    wi_mutable_set_add_data(set2, (void *) 2);
    
    WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(set1), WI_STR("foo"), 0), WI_NOT_FOUND, "");
    WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(set2), WI_STR("0x1"), 0), WI_NOT_FOUND, "");
}
Esempio n. 2
0
void wi_test_runtime_functions(void) {
	_wi_runtimetest_t			*runtimetest1, *runtimetest2;
	_wi_mutable_runtimetest_t	*runtimetest3;
	
	_wi_runtimetest_deallocs = 0;
	
	runtimetest1 = _wi_runtimetest_init_with_value(_wi_runtimetest_alloc(), 42);
	runtimetest2 = wi_copy(runtimetest1);
	
	WI_TEST_ASSERT_TRUE(runtimetest1 == runtimetest2, "");
	
	wi_release(runtimetest2);

	runtimetest3 = wi_mutable_copy(runtimetest1);
	
	WI_TEST_ASSERT_TRUE(runtimetest1 != runtimetest3, "");
	WI_TEST_ASSERT_EQUALS(runtimetest1->value, runtimetest3->value, "");
	WI_TEST_ASSERT_TRUE(wi_is_equal(runtimetest1, runtimetest3), "");
	
	runtimetest3->value++;
	
	WI_TEST_ASSERT_FALSE(wi_is_equal(runtimetest1, runtimetest3), "");
	
	wi_release(runtimetest3);
	
	WI_TEST_ASSERT_EQUALS(_wi_runtimetest_deallocs, 1U, "");
	WI_TEST_ASSERT_EQUAL_INSTANCES(wi_description(runtimetest1), WI_STR("value=42"), "");
	WI_TEST_ASSERT_EQUALS(wi_hash(runtimetest1), 42U, "");
	
	wi_release(runtimetest1);
}
Esempio n. 3
0
static wi_mutable_array_t * _wi_terminal_buffer_lines_for_string(wi_terminal_buffer_t *buffer, wi_string_t *string) {
	wi_enumerator_t			*enumerator;
	wi_mutable_array_t		*array;
	wi_mutable_string_t		*newstring;
 	wi_string_t				*line, *subline;
	wi_size_t				size;
	wi_uinteger_t			index;
	
	array		= wi_array_init(wi_mutable_array_alloc());
	size		= wi_terminal_size(buffer->terminal);
	enumerator	= wi_array_data_enumerator(wi_string_components_separated_by_string(string, WI_STR("\n")));
	
	while((line = wi_enumerator_next_data(enumerator))) {
		if(wi_terminal_width_of_string(buffer->terminal, line) < size.width) {
			wi_mutable_array_add_data(array, line);
		} else {
			newstring = wi_mutable_copy(line);
			
			do {
				index		= wi_terminal_index_of_string_for_width(buffer->terminal, newstring, size.width);
				subline		= wi_string_substring_to_index(newstring, index);

				wi_mutable_array_add_data(array, subline);
				wi_mutable_string_delete_characters_to_index(newstring, wi_string_length(subline));
			} while(wi_terminal_width_of_string(buffer->terminal, newstring) >= size.width);
			
			wi_mutable_array_add_data(array, newstring);
			wi_release(newstring);
		}
	}
	
	return wi_autorelease(array);
}
Esempio n. 4
0
void wi_test_set_mutation(void) {
    wi_mutable_set_t     *set1, *set2;
    
    set1 = wi_set_init_with_capacity_and_callbacks(wi_mutable_set_alloc(), 0, true, wi_set_null_callbacks);
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 0U, "");
    
    wi_mutable_set_add_data(set1, WI_STR("foo"));
    wi_mutable_set_add_data(set1, WI_STR("bar"));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, "");
    
    wi_mutable_set_add_data(set1, WI_STR("foo"));
    wi_mutable_set_add_data(set1, WI_STR("bar"));

    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 2U, "");
    
    wi_mutable_set_remove_data(set1, WI_STR("foo"));
    wi_mutable_set_remove_data(set1, WI_STR("bar"));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, "");
    
    wi_mutable_set_remove_data(set1, WI_STR("foo"));
    wi_mutable_set_remove_data(set1, WI_STR("bar"));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, "");

    wi_mutable_set_add_data(set1, WI_STR("foo"));
    wi_mutable_set_add_data(set1, WI_STR("bar"));
    
    set2 = wi_mutable_copy(set1);

    wi_mutable_set_add_data(set2, WI_STR("baz"));

    wi_mutable_set_set_set(set1, set2);

    WI_TEST_ASSERT_EQUAL_INSTANCES(set1, set2, "");
    
    wi_mutable_set_remove_all_data(set1);
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, "");
    
    wi_mutable_set_add_data_from_array(set1, wi_array_with_data(WI_STR("foo"), WI_STR("bar"), NULL));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, "");
}
Esempio n. 5
0
wi_data_t * wi_data_by_appending_data(wi_data_t *data, wi_data_t *append_data) {
	wi_mutable_data_t		*newdata;
	
	newdata = wi_mutable_copy(data);
	wi_mutable_data_append_bytes(newdata, append_data->bytes, append_data->length);
	
	wi_runtime_make_immutable(data);
	
	return wi_autorelease(newdata);
}
Esempio n. 6
0
wi_string_t * wi_terminal_string_by_adjusting_to_fit_width(wi_terminal_t *terminal, wi_string_t *string) {
	wi_mutable_string_t		*newstring;
	
	newstring = wi_mutable_copy(string);

	wi_terminal_adjust_string_to_fit_width(terminal, newstring);
	
	wi_runtime_make_immutable(newstring);
	
	return newstring;
}
Esempio n. 7
0
wi_string_t * wi_time_interval_rfc3339_string(wi_time_interval_t interval) {
    wi_mutable_string_t     *string;

    string = wi_mutable_copy(wi_time_interval_string_with_format(interval, WI_STR("%Y-%m-%dT%H:%M:%S%z")));

    wi_mutable_string_insert_string_at_index(string, WI_STR(":"), 22);

    wi_runtime_make_immutable(string);

    return wi_autorelease(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;
}
Esempio n. 9
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);
}
Esempio n. 10
0
void wi_test_runtime_invalid(void) {
    WI_TEST_ASSERT_NULL(wi_runtime_class_with_name(WI_STR("foo")), "");
    WI_TEST_ASSERT_NULL(wi_runtime_class_with_id(1337), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id_for_class(NULL), WI_RUNTIME_ID_NULL, "");
    
    WI_TEST_ASSERT_NULL(wi_runtime_class("foo"), "");
    WI_TEST_ASSERT_NULL(wi_runtime_class_name("foo"), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id("foo"), WI_RUNTIME_ID_NULL, "");

    WI_TEST_ASSERT_NULL(wi_retain(NULL), "");
    WI_TEST_ASSERT_EQUALS(wi_retain_count(NULL), 0U, "");
    WI_TEST_ASSERT_NULL(wi_copy(NULL), "");
    WI_TEST_ASSERT_NULL(wi_mutable_copy(NULL), "");
    WI_TEST_ASSERT_TRUE(wi_is_equal(NULL, NULL), "");
    WI_TEST_ASSERT_FALSE(wi_is_equal(NULL, "foo"), "");
    WI_TEST_ASSERT_FALSE(wi_is_equal(wi_array(), wi_dictionary()), "");
    WI_TEST_ASSERT_NULL(wi_description(NULL), "");
    WI_TEST_ASSERT_EQUALS(wi_hash(NULL), 0U, "");
}
Esempio n. 11
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);
}
Esempio n. 12
0
void wi_test_indexset_runtime_functions(void) {
    wi_indexset_t           *indexset1;
    wi_mutable_indexset_t   *indexset2;
    
    indexset1 = wi_indexset_with_indexes_in_range(wi_make_range(1, 3));
    indexset2 = wi_autorelease(wi_mutable_copy(indexset1));
    
    WI_TEST_ASSERT_EQUAL_INSTANCES(indexset1, indexset2, "");
    WI_TEST_ASSERT_EQUALS(wi_hash(indexset1), wi_hash(indexset2), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id(indexset1), wi_indexset_runtime_id(), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id(indexset2), wi_indexset_runtime_id(), "");
    WI_TEST_ASSERT_TRUE(wi_runtime_options(indexset1) & WI_RUNTIME_OPTION_IMMUTABLE, "");
    WI_TEST_ASSERT_TRUE(wi_runtime_options(indexset2) & WI_RUNTIME_OPTION_MUTABLE, "");
    
    wi_mutable_indexset_remove_index(indexset2, 1);
    wi_mutable_indexset_add_index(indexset2, 4);
    
    WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(indexset1, indexset2, "");
    
    WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(indexset2), WI_STR("4"), 0), WI_NOT_FOUND, "");
}
Esempio n. 13
0
wi_date_t * wi_date_init_with_rfc3339_string(wi_date_t *date, wi_string_t *string) {
    wi_mutable_string_t     *fullstring;
    wi_string_t             *timezone;

    if(wi_string_length(string) >= 19) {
        fullstring  = wi_autorelease(wi_mutable_copy(string));
        timezone    = wi_string_substring_from_index(fullstring, 19);
        
        if(wi_is_equal(timezone, WI_STR("Z"))) {
            wi_mutable_string_delete_characters_in_range(fullstring, wi_make_range(19, 1));

            return wi_date_init_with_string(date, fullstring, WI_STR("%Y-%m-%dT%H:%M:%S"));
        }
        else if(wi_string_length(timezone) == 6) {
            wi_mutable_string_delete_characters_in_range(fullstring, wi_make_range(22, 1));

            return wi_date_init_with_string(date, fullstring, WI_STR("%Y-%m-%dT%H:%M:%S%z"));
        }
    }
    
    wi_release(date);

    return NULL;
}
Esempio n. 14
0
wi_boolean_t wi_config_read_file(wi_config_t *config) {
	wi_enumerator_t				*enumerator;
	wi_runtime_instance_t		*instance;
	wi_file_t					*file;
	wi_mutable_array_t			*array;
	wi_mutable_dictionary_t		*previous_values;
	wi_string_t					*string, *name, *value;
	wi_config_type_t			type;
	
	file = wi_file_for_reading(config->path);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %m"),
			config->path);
		
		return false;
	}
	
	wi_log_info(WI_STR("Reading %@"), config->path);
	
	config->line = 0;
	
	wi_lock_lock(config->lock);
	
	previous_values = config->values;
	
	config->values = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	if(config->defaults) {
		enumerator = wi_dictionary_key_enumerator(config->defaults);
		
		while((name = wi_enumerator_next_data(enumerator))) {
//			instance = wi_mutable_copy(wi_dictionary_data_for_key(config->defaults, name));
			instance = wi_dictionary_data_for_key(config->defaults, name);
			if(wi_runtime_id(instance) == wi_array_runtime_id())
				instance = wi_autorelease(wi_mutable_copy(instance));
			wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
//			wi_release(instance);
		}
	}
	
	while((string = wi_file_read_line(file))) {
		config->line++;

		if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#"))) {
			if(_wi_config_parse_string(config, string, &name, &value)) {
				instance = _wi_config_instance_for_setting(config, name, value, &type);
				
				if(instance) {
					wi_log_debug(WI_STR("  %@ = %@"), name, value);
					
					if(type == WI_CONFIG_STRINGLIST) {
						array = wi_dictionary_data_for_key(config->values, name);
						
						if(!array) {
							array = wi_mutable_array();
							
							wi_mutable_dictionary_set_data_for_key(config->values, array, name);
						}
						
						wi_mutable_array_add_data(array, instance);
					} else {
						wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
					}
				} else {
					_wi_config_log_error(config, name);
				}
			} else {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR);
				
				_wi_config_log_error(config, string);
			}
		}
	}
	
	enumerator = wi_dictionary_key_enumerator(config->values);
	
	while((name = wi_enumerator_next_data(enumerator))) {
		instance = wi_dictionary_data_for_key(config->values, name);
		
		if(!previous_values || !wi_is_equal(instance, wi_dictionary_data_for_key(previous_values, name)))
			wi_mutable_set_add_data(config->changes, name);
	}
	
	wi_release(previous_values);

	wi_lock_unlock(config->lock);

	return true;
}
Esempio n. 15
0
static void wd_transfers_update_queue(void) {
	wi_mutable_set_t		*users;
	wi_mutable_array_t		*sorted_users, *transfers_queue, *failed_transfers;
	wd_transfer_t			*transfer;
	wd_user_t				*user;
	wi_uinteger_t			position;
	wi_uinteger_t			i, count;
	wi_uinteger_t			total_downloads, total_uploads, user_downloads, user_uploads, active_downloads, active_uploads;
	wi_boolean_t			queue;
	
	wi_array_rdlock(wd_transfers);
	
	total_downloads		= wd_settings.totaldownloads;
	user_downloads		= wd_settings.clientdownloads;
	total_uploads		= wd_settings.totaluploads;
	user_uploads		= wd_settings.clientuploads;
	active_downloads	= 0;
	active_uploads		= 0;
	
	failed_transfers	= wi_array_init(wi_mutable_array_alloc());

	users				= wi_set_init(wi_mutable_set_alloc());
	count				= wi_array_count(wd_transfers);
	
	for(i = 0; i < count; i++) {
		transfer = WI_ARRAY(wd_transfers, i);
		
		if(wd_transfer_state(transfer) == WD_TRANSFER_QUEUED) {
			wi_mutable_array_add_data(wd_user_transfers_queue(transfer->user), transfer);
			wi_mutable_set_add_data(users, transfer->user);
		}
		
		wd_user_clear_downloads(transfer->user);
		wd_user_clear_uploads(transfer->user);
	}
	
	for(i = 0; i < count; i++) {
		transfer = WI_ARRAY(wd_transfers, i);
		
		if(wd_transfer_state(transfer) == WD_TRANSFER_RUNNING) {
			if(transfer->type == WD_TRANSFER_DOWNLOAD) {
				active_downloads++;
				wd_user_increase_downloads(transfer->user);
			} else {
				active_uploads++;
				wd_user_increase_uploads(transfer->user);
			}
		}
	}

	count = wi_set_count(users);
	
	if(count > 0) {
		sorted_users = wi_autorelease(wi_mutable_copy(wi_set_all_data(users)));
		
		wi_mutable_array_sort(sorted_users, wd_transfers_compare_user);
		
		position = 1;
		
		while(count > 0) {
			for(i = 0; i < count; i++) {
				user = WI_ARRAY(sorted_users, i);
				transfers_queue = wd_user_transfers_queue(user);
				transfer = WI_ARRAY(transfers_queue, 0);
				
				if(transfer->type == WD_TRANSFER_DOWNLOAD) {
					queue = ((total_downloads > 0 && active_downloads >= total_downloads) ||
							 (user_downloads > 0 && wd_user_downloads(transfer->user) >= user_downloads));
				} else {
					queue = ((total_uploads > 0 && active_uploads >= total_uploads) ||
							 (user_uploads > 0 && wd_user_uploads(transfer->user) >= user_uploads));
				}
				
				if(queue) {
					if(transfer->queue != position) {
						transfer->queue = position;
						
						wd_user_lock_socket(transfer->user);
						wd_sreply(wd_user_socket(transfer->user), 401, WI_STR("%#@%c%u"),
								  transfer->path,	WD_FIELD_SEPARATOR,
								  transfer->queue);
						wd_user_unlock_socket(transfer->user);
					}

					position++;
				} else {
					transfer->queue = 0;
					transfer->waiting_time = wi_time_interval();
					
					wd_transfer_set_state(transfer, WD_TRANSFER_WAITING);
					
					if(wd_transfer_open(transfer)) {
						wd_transfer_create_timer(transfer);
						
						wd_user_lock_socket(transfer->user);
						wd_sreply(wd_user_socket(transfer->user), 400, WI_STR("%#@%c%llu%c%#@"),
								  transfer->path,		WD_FIELD_SEPARATOR,
								  transfer->offset,		WD_FIELD_SEPARATOR,
								  transfer->hash);
						wd_user_unlock_socket(transfer->user);
					} else {
						wd_user_lock_socket(transfer->user);
						wd_sreply(wd_user_socket(transfer->user), 500, WI_STR("Command Failed"));
						wd_user_unlock_socket(transfer->user);
						
						wi_mutable_array_add_data(failed_transfers, transfer);
					}
				}
				
				wi_mutable_array_remove_data_at_index(transfers_queue, 0);
				
				if(wi_array_count(transfers_queue) == 0) {
					wi_mutable_array_remove_data_at_index(sorted_users, i);
					
					i--;
					count--;
				}
			}
		}
	}
	
	wi_mutable_array_remove_data_in_array(wd_transfers, failed_transfers);
	wi_array_unlock(wd_transfers);
	
	wi_release(users);
	wi_release(failed_transfers);
}
Esempio n. 16
0
static void wr_command_open(wi_array_t *arguments) {
	wi_mutable_array_t		*argv;
	wi_string_t				*login, *password, *host;
	wi_url_t				*url;
	const char				**xargv;
	wi_uinteger_t			port;
	int						ch;
	
	url			= NULL;
	login		= NULL;
	password	= NULL;
	port		= 0;

	argv = wi_autorelease(wi_mutable_copy(arguments));
	
	wi_mutable_array_insert_data_at_index(argv, WI_STR("open"), 0);
	
	xargv = wi_array_create_argv(argv);
	
	wi_getopt_reset();

	while((ch = getopt(wi_array_count(argv), (char **) xargv, "hl:P:p:")) != -1) {
		switch(ch) {
			case 'l':
				login = wi_string_with_cstring(optarg);
				break;

			case 'P':
				port = wi_string_uinteger(wi_string_with_cstring(optarg));
				break;

			case 'p':
				password = wi_string_with_cstring(optarg);
				break;

			case '?':
			case 'h':
			default:
				wr_commands_print_usage_for_command(WI_STR("open"));

				return;
				break;
		}
	}
	
	wi_array_destroy_argv(wi_array_count(argv), xargv);
	
	if((wi_uinteger_t) optind >= wi_array_count(argv)) {
		wr_commands_print_usage_for_command(WI_STR("open"));
		
		return;
	}
	
	url = wi_autorelease(wi_url_init_with_string(wi_url_alloc(), WI_ARRAY(argv, optind)));
	host = wi_url_host(url);
	
	if(port == 0)
		port = wi_url_port(url);
	
	if(wi_string_length(host) == 0) {
		wr_commands_print_usage_for_command(WI_STR("open"));
		
		return;
	}
	
	wr_client_connect(host, port, login, password);
}
Esempio n. 17
0
static void wd_transfers_queue_thread(wi_runtime_instance_t *argument) {
	wi_pool_t					*pool;
	wi_enumerator_t				*enumerator;
	wi_mutable_dictionary_t		*key_queues;
	wi_mutable_array_t			*key_queue, *keys;
	wi_string_t					*key;
	wd_transfer_t				*transfer;
	wd_account_t				*account;
	wi_uinteger_t				user_downloads, user_uploads, user_transfers;
	wi_uinteger_t				new_position, position, queue, i, count, longest_queue;
	
	pool = wi_pool_init(wi_pool_alloc());
	
	key_queues = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	while(true) {
		wi_mutable_dictionary_remove_all_data(key_queues);

		wi_condition_lock_lock_when_condition(wd_transfers_queue_lock, 1, 0.0);
		wi_array_rdlock(wd_transfers);
		
		longest_queue	= 0;
		enumerator		= wi_array_data_enumerator(wd_transfers);
		
		while((transfer = wi_enumerator_next_data(enumerator))) {
			wi_condition_lock_lock(transfer->queue_lock);
			
			if(transfer->state == WD_TRANSFER_QUEUED && transfer->queue != 0) {
				key_queue = wi_dictionary_data_for_key(key_queues, transfer->key);
				
				if(!key_queue) {
					key_queue = wi_mutable_array();
					
					wi_mutable_dictionary_set_data_for_key(key_queues, key_queue, transfer->key);
				}
				
				wi_mutable_array_add_data(key_queue, transfer);
				
				if(wi_array_count(key_queue) > longest_queue)
					longest_queue = wi_array_count(key_queue);
			}
			
			wi_condition_lock_unlock(transfer->queue_lock);
		}
		
		keys		= wi_autorelease(wi_mutable_copy(wi_dictionary_keys_sorted_by_value(key_queues, wd_transfers_queue_compare)));
		position	= 1;
		count		= wi_array_count(keys);
		
		while(longest_queue > 0) {
			for(i = 0; i < count; i++) {
				key			= WI_ARRAY(keys, i);
				key_queue	= wi_dictionary_data_for_key(key_queues, key);
				
				if(wi_array_count(key_queue) > 0) {
					transfer	= WI_ARRAY(key_queue, 0);
					account		= wd_user_account(transfer->user);

					wi_lock_lock(wd_transfers_status_lock);
					
					if(transfer->type == WD_TRANSFER_DOWNLOAD) {
						wi_dictionary_rdlock(wd_transfers_user_downloads);
						
						user_downloads	= wd_account_transfer_download_limit(account);
						user_transfers	= (wi_integer_t) wi_dictionary_data_for_key(wd_transfers_user_downloads, transfer->key);
						queue			= ((wd_transfers_total_downloads > 0 && wd_transfers_active_downloads >= wd_transfers_total_downloads) ||
										   (user_downloads > 0 && user_transfers >= user_downloads));

						wi_dictionary_unlock(wd_transfers_user_downloads);
					} else {
						wi_dictionary_rdlock(wd_transfers_user_uploads);
						
						user_uploads	= wd_account_transfer_upload_limit(account);
						user_transfers	= (wi_integer_t) wi_dictionary_data_for_key(wd_transfers_user_uploads, transfer->key);
						queue			= ((wd_transfers_total_uploads > 0 && wd_transfers_active_uploads >= wd_transfers_total_uploads) ||
										   (user_uploads > 0 && user_transfers >= user_uploads));

						wi_dictionary_unlock(wd_transfers_user_uploads);
					}
					
					wi_lock_unlock(wd_transfers_status_lock);
					
					if(queue)
						new_position = position++;
					else
						new_position = 0;
					
					if(new_position != (wi_uinteger_t) transfer->queue) {
						if(new_position == 0)
							wd_transfers_add_or_remove_transfer(transfer, true);
						
						wi_condition_lock_lock(transfer->queue_lock);
						transfer->queue = new_position;
						wi_condition_lock_unlock_with_condition(transfer->queue_lock, 1);
					}
					
					wi_mutable_array_remove_data_at_index(key_queue, 0);
				}
			}
				
			longest_queue--;
		}
		
		wi_array_unlock(wd_transfers);
		wi_condition_lock_unlock_with_condition(wd_transfers_queue_lock, 0);
		
		wi_pool_drain(pool);
	}
	
	wi_release(key_queues);
	wi_release(pool);
}
Esempio n. 18
0
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;
}
Esempio n. 19
0
void wi_test_indexset_mutation(void) {
    wi_mutable_indexset_t     *indexset1, *indexset2;
    
    indexset1 = wi_mutable_indexset();
    
    WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 0U, "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 1), "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 2), "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 3), "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 0U, "");
    
    wi_mutable_indexset_add_index(indexset1, 1);
    wi_mutable_indexset_add_indexes(indexset1, wi_indexset_with_index(2));
    wi_mutable_indexset_add_indexes_in_range(indexset1, wi_make_range(3, 1));
    
    WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 3U, "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset1, 1), "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset1, 2), "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset1, 3), "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 3U, "");
    
    wi_mutable_indexset_remove_index(indexset1, 1);
    wi_mutable_indexset_remove_indexes(indexset1, wi_indexset_with_index(2));
    wi_mutable_indexset_remove_indexes_in_range(indexset1, wi_make_range(3, 1));
    
    WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 0U, "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 1), "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 2), "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 3), "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 0U, "");

    wi_mutable_indexset_add_indexes_in_range(indexset1, wi_make_range(1, 3));
    
    indexset2 = wi_autorelease(wi_mutable_copy(indexset1));

    wi_mutable_indexset_add_index(indexset2, 5);
    wi_mutable_indexset_add_index(indexset2, 4);

    WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset2), 5U, "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 1), "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 2), "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 3), "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 4), "");
    WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 5), "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset2), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset2), 5U, "");
    
    wi_mutable_indexset_set_indexes(indexset1, indexset2);

    WI_TEST_ASSERT_EQUAL_INSTANCES(indexset1, indexset2, "");
    
    wi_mutable_indexset_remove_all_indexes(indexset1);
    
    WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 0U, "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 1), "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 2), "");
    WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 3), "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 0U, "");
}