Exemplo n.º 1
0
void wi_test_string_numeric_conversions(void) {
	WI_TEST_ASSERT_EQUALS(wi_string_bool(WI_STR("yes")), true, "");
	WI_TEST_ASSERT_EQUALS(wi_string_bool(WI_STR("no")), false, "");
	WI_TEST_ASSERT_EQUALS(wi_string_int32(WI_STR("2147483647")), 2147483647, "");
 	WI_TEST_ASSERT_EQUALS(wi_string_int32(WI_STR("2147483648")), 0, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uint32(WI_STR("4294967295")), 4294967295U, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uint32(WI_STR("4294967296")), 0U, "");
	WI_TEST_ASSERT_EQUALS(wi_string_int64(WI_STR("9223372036854775807")), 9223372036854775807LL, "");
	WI_TEST_ASSERT_EQUALS(wi_string_int64(WI_STR("9223372036854775808")), 0LL, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uint64(WI_STR("18446744073709551615")), 18446744073709551615ULL, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uint64(WI_STR("18446744073709551616")), 0ULL, "");

#ifdef WI_32
	WI_TEST_ASSERT_EQUALS(wi_string_integer(WI_STR("2147483647")), 2147483647, "");
	WI_TEST_ASSERT_EQUALS(wi_string_integer(WI_STR("2147483648")), 0, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uinteger(WI_STR("4294967295")), 4294967295U, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uinteger(WI_STR("4294967296")), 0U, "");
#else
	WI_TEST_ASSERT_EQUALS(wi_string_integer(WI_STR("9223372036854775807")), 9223372036854775807LL, "");
	WI_TEST_ASSERT_EQUALS(wi_string_integer(WI_STR("9223372036854775808")), 0LL, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uinteger(WI_STR("18446744073709551615")), 18446744073709551615ULL, "");
	WI_TEST_ASSERT_EQUALS(wi_string_uinteger(WI_STR("18446744073709551616")), 0ULL, "");
#endif

	WI_TEST_ASSERT_EQUALS_WITH_ACCURACY(wi_string_float(WI_STR("3.40282346e38")), 3.40282346e38F, 0.0001, "");
	WI_TEST_ASSERT_EQUALS_WITH_ACCURACY(wi_string_float(WI_STR("3.40282347e38")), 0.0F, 0.0001, "");
	WI_TEST_ASSERT_EQUALS_WITH_ACCURACY(wi_string_double(WI_STR("1.7976931348623155e308")), 1.7976931348623155e308, 0.0001, "");
	WI_TEST_ASSERT_EQUALS_WITH_ACCURACY(wi_string_double(WI_STR("1.7976931348623160e308")), 0.0, 0.0001, "");
}
Exemplo n.º 2
0
static wi_boolean_t _wi_plist_read_node_to_instance(xmlNodePtr content_node, wi_runtime_instance_t *collection) {
	xmlNodePtr				node;
	wi_string_t				*key = NULL;
	wi_runtime_instance_t	*instance = NULL;
	wi_boolean_t			dictionary;
	
	dictionary = (wi_runtime_id(collection) == wi_dictionary_runtime_id());
	
	for(node = content_node->children; node != NULL; node = node->next) {
		if(node->type == XML_ELEMENT_NODE) {
			if(strcmp((const char *) node->name, "key") == 0)
				key = wi_xml_node_content(node);
			else if(strcmp((const char *) node->name, "string") == 0)
				instance = wi_xml_node_content(node);
			else if(strcmp((const char *) node->name, "integer") == 0)
				instance = wi_number_with_integer(wi_string_integer(wi_xml_node_content(node)));
			else if(strcmp((const char *) node->name, "real") == 0)
				instance = wi_number_with_double(wi_string_double(wi_xml_node_content(node)));
			else if(strcmp((const char *) node->name, "true") == 0)
				instance = wi_number_with_bool(true);
			else if(strcmp((const char *) node->name, "false") == 0)
				instance = wi_number_with_bool(false);
			else if(strcmp((const char *) node->name, "date") == 0)
				instance = wi_date_with_rfc3339_string(wi_xml_node_content(node));
			else if(strcmp((const char *) node->name, "data") == 0)
				instance = wi_data_with_base64(wi_xml_node_content(node));
			else if(strcmp((const char *) node->name, "dict") == 0) {
				instance = wi_mutable_dictionary();
				
				if(!_wi_plist_read_node_to_instance(node, instance))
					return false;
			}
			else if(strcmp((const char *) node->name, "array") == 0) {
				instance = wi_mutable_array();
				
				if(!_wi_plist_read_node_to_instance(node, instance))
					return false;
			}
			else {
				wi_error_set_libwired_error_with_format(WI_ERROR_PLIST_READFAILED,
					WI_STR("Unhandled node \"%s\""), node->name);
				
				return false;
			}
		}
			
		if(instance) {
			if(dictionary)
				wi_mutable_dictionary_set_data_for_key(collection, instance, key);
			else
				wi_mutable_array_add_data(collection, instance);
			
			instance = NULL;
			key = NULL;
		}
	}
	
	return true;
}
Exemplo n.º 3
0
static void wr_msg_200(wi_array_t *arguments) {
	wi_string_t		*password;
	double			protocol;
	
	protocol = wi_string_double(wr_protocol_version_string);
	
	wr_server = wr_server_init(wr_server_alloc());
	wr_server->version = wi_retain(WI_ARRAY(arguments, 0));
	wr_server->protocol = wi_string_double(WI_ARRAY(arguments, 1));
	wr_server->name = wi_retain(WI_ARRAY(arguments, 2));
	wr_server->description = wi_retain(WI_ARRAY(arguments, 3));
	wr_server->startdate = wi_date_init_with_rfc3339_string(wi_date_alloc(), WI_ARRAY(arguments, 4));
	wr_server->files = wi_string_uint32(WI_ARRAY(arguments, 5));
	wr_server->size = wi_string_uint64(WI_ARRAY(arguments, 6));

	wr_draw_divider();
	
	if(!wr_logged_in) {
		if(wr_server->protocol > protocol) {
			wr_wprintf_prefix(wr_console_window, WI_STR("Server protocol version %.1f may not be fully compatible with client protocol version %.1f"),
				wr_server->protocol, protocol);
		}

		wr_send_command(WI_STR("CLIENT %#@"), wr_client_version_string);
		wr_send_command(WI_STR("NICK %#@"), wr_nick);
		wr_send_command(WI_STR("STATUS %#@"), wr_status);
		wr_send_command(WI_STR("ICON %u%c%#@"), 0, WR_FIELD_SEPARATOR, wr_icon);
		wr_send_command(WI_STR("USER %#@"), wr_login ? wr_login : WI_STR("guest"));
		
		if(wr_password && wi_string_length(wr_password) > 0)
			password = wi_string_sha1(wr_password);
		else
			password = NULL;

		wr_send_command(WI_STR("PASS %#@"), password);
		
		wr_send_command(WI_STR("WHO %u"), 1);

		wr_logged_in = true;
	}
}
Exemplo n.º 4
0
wi_boolean_t wi_p7_message_get_double_for_name(wi_p7_message_t *p7_message, wi_p7_double_t *value, wi_string_t *field_name) {
	wi_string_t		*string;
	unsigned char	*binary;
	
	if(p7_message->serialization == WI_P7_BINARY) {
		if(!_wi_p7_message_get_binary_buffer_for_reading_for_name(p7_message, field_name, &binary, NULL))
			return false;
		
		*value = _wi_p7_message_ieee754_to_double(binary);
	} else {
		string = _wi_p7_message_xml_value_for_name(p7_message, field_name);
		
		if(!string)
			return false;
		
		*value = wi_string_double(string);
	}
	
	return true;
}
Exemplo n.º 5
0
void wi_p7_message_deserialize(wi_p7_message_t *p7_message, wi_p7_serialization_t serialization) {
	xmlDocPtr					doc;
	xmlNodePtr					root_node, field_node;
	wi_string_t					*field_name, *field_value;
	wi_p7_spec_message_t		*message;
	wi_p7_spec_field_t			*field;
	wi_uuid_t					*uuid;
	wi_date_t					*date;
	wi_data_t					*data;
	
	if(serialization == WI_P7_BINARY) {
		p7_message->binary_id = wi_read_swap_big_to_host_int32(p7_message->binary_buffer, 0);
		
		message = wi_p7_spec_message_with_id(p7_message->spec, p7_message->binary_id);
		
		if(message)
			p7_message->name = wi_retain(wi_p7_spec_message_name(message));
	} else {
		p7_message->binary_capacity	= _WI_P7_MESSAGE_BINARY_BUFFER_INITIAL_SIZE;
		p7_message->binary_buffer	= wi_malloc(p7_message->binary_capacity);
		p7_message->binary_size		= WI_P7_MESSAGE_BINARY_HEADER_SIZE;
		
		doc							= xmlParseDoc((xmlChar *) wi_string_cstring(p7_message->xml_string));
		root_node					= xmlDocGetRootElement(doc);
		
		if(root_node) {
			wi_p7_message_set_name(p7_message, wi_xml_node_attribute_with_name(root_node, WI_STR("name")));
			
			for(field_node = root_node->children; field_node != NULL; field_node = field_node->next) {
				if(field_node->type == XML_ELEMENT_NODE) {
					if(wi_is_equal(wi_xml_node_name(field_node), WI_STR("field"))) {
						field_name		= wi_xml_node_attribute_with_name(field_node, WI_STR("name"));
						field_value		= wi_xml_node_content(field_node);
						field			= wi_p7_spec_field_with_name(p7_message->spec, field_name);
						
						if(!field_name || !field_value || !field)
							continue;
						
						switch(wi_p7_spec_type_id(wi_p7_spec_field_type(field))) {
							case WI_P7_BOOL:
								wi_p7_message_set_bool_for_name(p7_message, wi_string_bool(field_value), field_name);
								break;
								
							case WI_P7_ENUM:
								wi_p7_message_set_enum_for_name(p7_message, wi_string_uint32(field_value), field_name);
								break;
								
							case WI_P7_INT32:
								wi_p7_message_set_int32_for_name(p7_message, wi_string_int32(field_value), field_name);
								break;
								
							case WI_P7_UINT32:
								wi_p7_message_set_uint32_for_name(p7_message, wi_string_uint32(field_value), field_name);
								break;
								
							case WI_P7_INT64:
								wi_p7_message_set_int64_for_name(p7_message, wi_string_int64(field_value), field_name);
								break;
								
							case WI_P7_UINT64:
								wi_p7_message_set_uint64_for_name(p7_message, wi_string_uint64(field_value), field_name);
								break;
								
							case WI_P7_DOUBLE:
								wi_p7_message_set_double_for_name(p7_message, wi_string_double(field_value), field_name);
								break;
								
							case WI_P7_STRING:
								wi_p7_message_set_string_for_name(p7_message, field_value, field_name);
								break;
								
							case WI_P7_UUID:
								uuid = wi_uuid_with_string(field_value);

								if(uuid)
									wi_p7_message_set_uuid_for_name(p7_message, uuid, field_name);
								break;
								
							case WI_P7_DATE:
								date = wi_date_with_rfc3339_string(field_value);
								
								if(date)
									wi_p7_message_set_date_for_name(p7_message, date, field_name);
								break;
								
							case WI_P7_DATA:
								data = wi_autorelease(wi_data_init_with_base64(wi_data_alloc(), field_value));
								
								if(data)
									wi_p7_message_set_data_for_name(p7_message, data, field_name);
								break;
							
							case WI_P7_OOBDATA:
								wi_p7_message_set_oobdata_for_name(p7_message, wi_string_uint64(field_value), field_name);
								break;
							
							case WI_P7_LIST:
								WI_ASSERT(0, "Can't deserialize XML with lists at the moment");
								break;
						}
					}
				}
			}
		}
		
		xmlFreeDoc(doc);
	}
}
Exemplo n.º 6
0
wi_boolean_t _wi_settings_set_time_interval(wi_settings_t *settings, uint32_t index, wi_string_t *name, wi_string_t *value) {
	*(wi_time_interval_t *) settings->spec[index].setting = wi_string_double(value);
	
	return true;
}
Exemplo n.º 7
0
static wi_runtime_instance_t * _wi_config_instance_for_setting(wi_config_t *config, wi_string_t *name, wi_string_t *value, wi_config_type_t *type) {
	struct passwd		*user;
	struct group		*group;
	struct servent		*servent;
	wi_uinteger_t		port;
	uint32_t			uid, gid;
	
	if(!wi_dictionary_contains_key(config->types, name)) {
		wi_error_set_libwired_error(WI_ERROR_SETTINGS_UNKNOWNSETTING);
		
		return NULL;
	}
	
	*type = wi_number_int32(wi_dictionary_data_for_key(config->types, name));
	
	switch(*type) {
		case WI_CONFIG_INTEGER:
			return wi_number_with_integer(wi_string_integer(value));
			break;
			
		case WI_CONFIG_BOOL:
			return wi_number_with_bool(wi_string_bool(value));
			break;
			
		case WI_CONFIG_STRING:
		case WI_CONFIG_STRINGLIST:
		case WI_CONFIG_PATH:
			return value;
			break;
			
		case WI_CONFIG_USER:
			user = getpwnam(wi_string_cstring(value));
			
			if(!user) {
				uid = wi_string_uint32(value);
				
				if(uid != 0 || wi_is_equal(value, WI_STR("0")))
					user = getpwuid(uid);
			}
			
			if(!user) {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_NOSUCHUSER);
				
				return NULL;
			}
			
			return wi_number_with_int32(user->pw_uid);
			break;
			
		case WI_CONFIG_GROUP:
			group = getgrnam(wi_string_cstring(value));
			
			if(!group) {
				gid = wi_string_uint32(value);
				
				if(gid != 0 || wi_is_equal(value, WI_STR("0")))
					group = getgrgid(gid);
			}
			
			if(!group) {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_NOSUCHGROUP);
				
				return NULL;
			}
			
			return wi_number_with_int32(group->gr_gid);
			break;
			
		case WI_CONFIG_PORT:
			port = wi_string_uinteger(value);
			
			if(port > 65535) {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_INVALIDPORT);
				
				return NULL;
			}
			
			if(port == 0) {
				servent = getservbyname(wi_string_cstring(value), "tcp");
				
				if(!servent) {
					wi_error_set_libwired_error(WI_ERROR_SETTINGS_NOSUCHSERVICE);
					
					return NULL;
				}
				
				port = servent->s_port;
			}
			
			return wi_number_with_int32(port);
			break;
			
		case WI_CONFIG_REGEXP:
			return wi_autorelease(wi_regexp_init_with_string(wi_regexp_alloc(), value));
			break;
			
		case WI_CONFIG_TIME_INTERVAL:
			return wi_number_with_double(wi_string_double(value));
			break;
	}
	
	return NULL;
}