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; }
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, ""); }
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 }
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; }
void wr_chats_add_chat(wr_chat_t *chat) { wi_hash_set_data_for_key(wr_chats, chat, wi_number_with_integer(chat->cid)); }
wr_user_t * wr_chat_user_with_uid(wr_chat_t *chat, wr_uid_t uid) { return wi_hash_data_for_key(chat->users_hash, wi_number_with_integer(uid)); }
void wr_chat_remove_user(wr_chat_t *chat, wr_user_t *user) { wi_array_remove_data(chat->users_array, user); wi_hash_remove_data_for_key(chat->users_hash, wi_number_with_integer(wr_user_id(user))); }
void wr_chat_add_user(wr_chat_t *chat, wr_user_t *user) { wi_array_add_data(chat->users_array, user); wi_hash_set_data_for_key(chat->users_hash, user, wi_number_with_integer(wr_user_id(user))); }
wr_chat_t * wr_chats_chat_with_cid(wr_cid_t cid) { return wi_hash_data_for_key(wr_chats, wi_number_with_integer(cid)); }
void wr_chats_remove_chat(wr_chat_t *chat) { wi_hash_remove_data_for_key(wr_chats, wi_number_with_integer(chat->cid)); }
void wr_chats_add_chat(wr_chat_t *chat) { wi_mutable_dictionary_set_data_for_key(wr_chats, chat, wi_number_with_integer(chat->cid)); }
wr_user_t * wr_chat_user_with_uid(wr_chat_t *chat, wr_uid_t uid) { return wi_dictionary_data_for_key(chat->users_dictionary, wi_number_with_integer(uid)); }
void wr_chat_remove_user(wr_chat_t *chat, wr_user_t *user) { wi_mutable_array_remove_data(chat->users_array, user); wi_mutable_dictionary_remove_data_for_key(chat->users_dictionary, wi_number_with_integer(wr_user_id(user))); }
void wr_chats_remove_chat(wr_chat_t *chat) { wi_mutable_dictionary_remove_data_for_key(wr_chats, wi_number_with_integer(chat->cid)); }