Пример #1
0
static void wr_command_save(wi_array_t *arguments) {
	wi_file_t		*file;
	wi_string_t		*path, *login, *password, *port;
	
	path = wi_user_home();
	path = wi_string_by_appending_path_component(path, WI_STR(WB_WIREBOT_USER_PATH));
	path = wi_string_by_appending_path_component(path, WI_ARRAY(arguments, 0));
	
	file = wi_file_for_writing(path);
	
	if(!file) {
		wr_printf_prefix(WI_STR("save: %@: %m"), path);
		
		return;
	}
	
	wi_file_write_format(file, WI_STR("charset %@\n"), wi_string_encoding_charset(wr_client_string_encoding));
	wi_file_write_format(file, WI_STR("timestamp %@\n"), wr_timestamp_format);
	wi_file_write_format(file, WI_STR("nick %@\n"), wr_nick);
	
	if(wi_string_length(wr_status) > 0)
		wi_file_write_format(file, WI_STR("status %@\n"), wr_status);
	
	if(wr_icon_path)
		wi_file_write_format(file, WI_STR("icon %@\n"), wr_icon_path);
	
	if(wr_connected) {
		if(wi_string_length(wi_p7_socket_user_name(wr_p7_socket)) > 0)
			login = wi_string_with_format(WI_STR("-l %@"), wi_p7_socket_user_name(wr_p7_socket));
		else
			login = NULL;
		
		if(wi_string_length(wr_password) > 0)
			password = wi_string_with_format(WI_STR("-p %@"), wr_password);
		else
			password = NULL;
		
		if(wi_address_port(wi_socket_address(wr_socket)) != WR_PORT)
			port = wi_string_with_format(WI_STR("-P %u"), wi_address_port(wi_socket_address(wr_socket)));
		else
			port = NULL;
		
		wi_file_write_format(file, WI_STR("open %#@ %#@ %#@ %@\n"),
			login, password, port, wi_address_string(wi_socket_address(wr_socket)));
	}
	
	wr_printf_prefix(WI_STR("save: \"%@\" saved"), path);
}
Пример #2
0
static wi_boolean_t _wi_config_write_setting_to_file(wi_config_t *config, wi_string_t *name, wi_file_t *file) {
	wi_runtime_instance_t		*value;
	wi_config_type_t			type;
	struct passwd				*user;
	struct group				*group;
	wi_uinteger_t				i, count;
	
	if(!wi_dictionary_contains_key(config->types, name)) {
		wi_error_set_libwired_error(WI_ERROR_SETTINGS_UNKNOWNSETTING);

		return false;
	}
	
	type = wi_number_int32(wi_dictionary_data_for_key(config->types, name));
	value = wi_dictionary_data_for_key(config->values, name);
	
	if(!value)
		return false;
	
	switch(type) {
		case WI_CONFIG_INTEGER:
			wi_file_write_format(file, WI_STR("%@ = %d\n"), name, wi_number_integer(value));
			break;
			
		case WI_CONFIG_BOOL:
			wi_file_write_format(file, WI_STR("%@ = %@\n"), name, wi_number_bool(value) ? WI_STR("yes") : WI_STR("no"));
			break;
			
		case WI_CONFIG_STRING:
			wi_file_write_format(file, WI_STR("%@ = %@\n"), name, value);
			break;
			
		case WI_CONFIG_STRINGLIST:
			count = wi_array_count(value);
			
			for(i = 0; i < count; i++)
				wi_file_write_format(file, WI_STR("%@ = %@\n"), name, WI_ARRAY(value, i));
			break;
			
		case WI_CONFIG_PATH:
			wi_file_write_format(file, WI_STR("%@ = %@\n"), name, value);
			break;
			
		case WI_CONFIG_USER:
			user = getpwuid(wi_number_int32(value));
			
			if(user)
				wi_file_write_format(file, WI_STR("%@ = %s\n"), name, user->pw_name);
			else
				wi_file_write_format(file, WI_STR("%@ = %d\n"), name, wi_number_int32(value));
			break;
			
		case WI_CONFIG_GROUP:
			group = getgrgid(wi_number_int32(value));
			
			if(group)
				wi_file_write_format(file, WI_STR("%@ = %s\n"), name, group->gr_name);
			else
				wi_file_write_format(file, WI_STR("%@ = %d\n"), name, wi_number_int32(value));
			break;
			
		case WI_CONFIG_PORT:
			wi_file_write_format(file, WI_STR("%@ = %d\n"), name, wi_number_int32(value));
			break;
			
		case WI_CONFIG_REGEXP:
			wi_file_write_format(file, WI_STR("%@ = %@\n"), name, wi_regexp_string(value));
			break;
			
		case WI_CONFIG_TIME_INTERVAL:
			wi_file_write_format(file, WI_STR("%@ = %.2f\n"), name, wi_number_double(value));
			break;
	}
	
	return true;
}
Пример #3
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;
}