Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
void wi_test_number(void) {
	WI_TEST_ASSERT_TRUE(wi_number_bool(wi_number_with_bool(true)), "");
	WI_TEST_ASSERT_FALSE(wi_number_bool(wi_number_with_bool(false)), "");
	WI_TEST_ASSERT_EQUALS(wi_number_int32(wi_number_with_int32(2147483647)), 2147483647, "");
	WI_TEST_ASSERT_EQUALS((uint32_t) wi_number_int32(wi_number_with_int32(4294967295U)), 4294967295U, "");
	WI_TEST_ASSERT_EQUALS(wi_number_int64(wi_number_with_int64(9223372036854775807LL)), 9223372036854775807LL, "");
	WI_TEST_ASSERT_EQUALS((uint64_t) wi_number_int64(wi_number_with_int64(18446744073709551615ULL)), 18446744073709551615ULL, "");

#ifdef WI_32
	WI_TEST_ASSERT_EQUALS(wi_number_integer(wi_number_with_integer(2147483647)), 2147483647, "");
	WI_TEST_ASSERT_EQUALS((wi_uinteger_t) wi_number_integer(wi_number_with_integer(4294967295U)), 4294967295U, "");
#else
	WI_TEST_ASSERT_EQUALS(wi_number_integer(wi_number_with_integer(9223372036854775807LL)), 9223372036854775807LL, "");
	WI_TEST_ASSERT_EQUALS((wi_uinteger_t) wi_number_integer(wi_number_with_integer(18446744073709551615ULL)), 18446744073709551615ULL, "");
#endif
	
	WI_TEST_ASSERT_EQUALS_WITH_ACCURACY(wi_number_float(wi_number_with_float(3.40282346e38)), 3.40282346e38F, 0.0001, "");
	WI_TEST_ASSERT_EQUALS_WITH_ACCURACY(wi_number_double(wi_number_with_double(1.7976931348623155e308)), 1.7976931348623155e308, 0.0001, "");
}
Ejemplo n.º 3
0
wi_number_t * wi_p7_message_number_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) {
	wi_p7_int32_t			p7_int32;
	wi_p7_int64_t			p7_int64;
	wi_p7_double_t			p7_double;
	wi_p7_type_t			type_id;
	uint32_t				field_id;
	
	if(p7_message->serialization == WI_P7_BINARY) {
		field_id = wi_p7_spec_field_id(p7_message->spec, field_name);
		
		if(field_id == WI_P7_SPEC_FIELD_ID_NULL)
			return NULL;
		
		type_id = wi_p7_spec_field_type(p7_message->spec, field_id);
	} else {
		type_id = _wi_p7_message_xml_type_for_name(p7_message, field_name);
	}
	
	switch(type_id) {
		case WI_P7_INT32:
			if(wi_p7_message_get_int32_for_name(p7_message, &p7_int32, field_name))
				return wi_number_with_int32(p7_int32);
			break;
		
		case WI_P7_INT64:
			if(wi_p7_message_get_int64_for_name(p7_message, &p7_int64, field_name))
				return wi_number_with_int64(p7_int64);
			break;
		
		case WI_P7_DOUBLE:
			if(wi_p7_message_get_double_for_name(p7_message, &p7_double, field_name))
				return wi_number_with_double(p7_double);
			break;
		
		default:
			break;
	}
	
	return NULL;
}
Ejemplo n.º 4
0
void wi_test_plist(void) {
#ifdef WI_PLIST
	wi_string_t			*string1, *string2;
	wi_dictionary_t		*dictionary1, *dictionary2;
	
	string1 = wi_autorelease(wi_string_init_with_contents_of_file(wi_string_alloc(), wi_string_by_appending_path_component(wi_test_fixture_path, WI_STR("wi-plist-tests-1.plist"))));

	dictionary1 = wi_plist_instance_for_string(string1);
	
	WI_TEST_ASSERT_NOT_NULL(dictionary1, "%m");
	
	dictionary2 = wi_dictionary_with_data_and_keys(
		WI_STR("hello world"),
			WI_STR("string"),
		wi_number_with_bool(true),
			WI_STR("true"),
		wi_number_with_bool(false),
			WI_STR("false"),
		wi_number_with_integer(42),
			WI_STR("integer"),
		wi_number_with_double(3.14),
			WI_STR("real"),
		wi_date_with_rfc3339_string(WI_STR("2008-06-01T17:28:00Z")),
			WI_STR("date"),
		wi_data_with_base64(wi_string_base64(WI_STR("hello world"))),
			WI_STR("data"),
		wi_dictionary_with_data_and_keys(WI_STR("value1"), WI_STR("key1"), WI_STR("value2"), WI_STR("key2"), NULL),
			WI_STR("dict"),
		wi_array_with_data(WI_STR("value1"), WI_STR("value2"), NULL),
			WI_STR("array"),
		NULL);
	
	WI_TEST_ASSERT_EQUAL_INSTANCES(dictionary1, dictionary2, "");
	
	string2 = wi_plist_string_for_instance(dictionary2);
	
	WI_TEST_ASSERT_EQUAL_INSTANCES(string1, string2, "");
#endif
}
Ejemplo n.º 5
0
wi_number_t * wi_p7_message_number_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) {
	wi_p7_spec_field_t		*field;
	wi_p7_spec_type_t		*type;
	wi_p7_boolean_t			p7_bool;
	wi_p7_enum_t			p7_enum;
	wi_p7_int32_t			p7_int32;
	wi_p7_uint32_t			p7_uint32;
	wi_p7_int64_t			p7_int64;
	wi_p7_uint64_t			p7_uint64;
	wi_p7_double_t			p7_double;
	
	field = wi_p7_spec_field_with_name(p7_message->spec, field_name);
	
	if(!field) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No id found for field \"%@\""), field_name);
		
		return NULL;
	}
	
	type = wi_p7_spec_field_type(field);
	
	if(!type) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No type found for field \"%@\""), field_name);
		
		return NULL;
	}
	
	switch(wi_p7_spec_type_id(type)) {
		case WI_P7_BOOL:
			if(wi_p7_message_get_bool_for_name(p7_message, &p7_bool, field_name))
				return wi_number_with_bool(p7_bool);
			break;
		
		case WI_P7_ENUM:
			if(wi_p7_message_get_enum_for_name(p7_message, &p7_enum, field_name))
				return wi_number_with_int32(p7_enum);
			break;
			
		case WI_P7_INT32:
			if(wi_p7_message_get_int32_for_name(p7_message, &p7_int32, field_name))
				return wi_number_with_int32(p7_int32);
			break;
		
		case WI_P7_UINT32:
			if(wi_p7_message_get_uint32_for_name(p7_message, &p7_uint32, field_name))
				return wi_number_with_int32(p7_uint32);
			break;
		
		case WI_P7_INT64:
			if(wi_p7_message_get_int64_for_name(p7_message, &p7_int64, field_name))
				return wi_number_with_int64(p7_int64);
			break;
		
		case WI_P7_UINT64:
			if(wi_p7_message_get_uint64_for_name(p7_message, &p7_uint64, field_name))
				return wi_number_with_int64(p7_uint64);
			break;
		
		case WI_P7_DOUBLE:
			if(wi_p7_message_get_double_for_name(p7_message, &p7_double, field_name))
				return wi_number_with_double(p7_double);
			break;
		
		default:
			wi_error_set_libwired_error_with_format(WI_ERROR_P7_INVALIDARGUMENT,
				WI_STR("Field \"%@\" is not a number"), field_name);
			break;
	}
	
	return NULL;
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
0
wi_dictionary_t * wi_sqlite3_fetch_statement_results(wi_sqlite3_database_t *database, wi_sqlite3_statement_t *statement) {
	wi_mutable_dictionary_t		*results;
	wi_runtime_instance_t		*instance;
	int							i, count, length, result;
	
	wi_recursive_lock_lock(database->lock);
	
	result = sqlite3_step(statement->statement);
	
	switch(result) {
		case SQLITE_DONE:
			results = wi_dictionary();

			sqlite3_finalize(statement->statement);
			statement->statement = NULL;
			break;
			
		case SQLITE_ROW:
			results			= wi_mutable_dictionary();
			count			= sqlite3_column_count(statement->statement);
			
			for(i = 0; i < count; i++) {
				switch(sqlite3_column_type(statement->statement, i)) {
					case SQLITE_INTEGER:
						instance	= wi_number_with_int64(sqlite3_column_int64(statement->statement, i));
						break;
						
					case SQLITE_FLOAT:
						instance	= wi_number_with_double(sqlite3_column_double(statement->statement, i));
						break;
						
					case SQLITE_TEXT:
						instance	= wi_string_with_cstring((const char *) sqlite3_column_text(statement->statement, i));
						break;
						
					case SQLITE_BLOB:
						length		= sqlite3_column_bytes(statement->statement, i);
						instance	= wi_data_with_bytes(sqlite3_column_blob(statement->statement, i), length);
						break;
						
					case SQLITE_NULL:
						instance	= wi_null();
						break;
					
					default:
						instance	= NULL;
						break;
				}
				
				if(instance)
					wi_mutable_dictionary_set_data_for_key(results, instance, wi_string_with_cstring(sqlite3_column_name(statement->statement, i)));
			}
	
			wi_runtime_make_immutable(results);
			break;
			
		default:
			wi_error_set_sqlite3_error_with_description(database->database, wi_description(statement));

			sqlite3_finalize(statement->statement);
			statement->statement = NULL;

			results = NULL;
			break;
	}
	
	wi_recursive_lock_unlock(database->lock);
		
	return results;
}