wi_boolean_t wi_p7_message_set_number_for_name(wi_p7_message_t *p7_message, wi_number_t *number, wi_string_t *field_name) { if(!number) number = wi_number_with_int32(0); if(wi_number_type(number) == WI_NUMBER_BOOL) { return wi_p7_message_set_bool_for_name(p7_message, wi_number_bool(number), field_name); } else { switch(wi_number_storage_type(number)) { case WI_NUMBER_STORAGE_INT32: return wi_p7_message_set_int32_for_name(p7_message, wi_number_int32(number), field_name); break; case WI_NUMBER_STORAGE_INT64: return wi_p7_message_set_int64_for_name(p7_message, wi_number_int64(number), field_name); break; case WI_NUMBER_STORAGE_FLOAT: return wi_p7_message_set_double_for_name(p7_message, wi_number_float(number), field_name); break; case WI_NUMBER_STORAGE_DOUBLE: return wi_p7_message_set_double_for_name(p7_message, wi_number_double(number), field_name); break; } } return false; }
static void wd_trackers_register_thread(wi_runtime_instance_t *argument) { wi_pool_t *pool; wi_enumerator_t *enumerator; wi_number_t *number = argument; wd_tracker_t *tracker; wi_boolean_t update; pool = wi_pool_init(wi_pool_alloc()); update = wi_number_bool(number); wi_array_rdlock(wd_trackers); enumerator = wi_array_data_enumerator(wd_trackers); while((tracker = wi_enumerator_next_data(enumerator))) { wd_tracker_register(tracker); if(update && tracker->active) wd_tracker_update(tracker); } wi_array_unlock(wd_trackers); if(update) wi_timer_reschedule(wd_trackers_update_timer, WD_TRACKERS_UPDATE_INTERVAL); wi_release(pool); }
wi_boolean_t wi_config_bool_for_name(wi_config_t *config, wi_string_t *name) { wi_number_t *value; wi_lock_lock(config->lock); value = wi_autorelease(wi_retain(wi_dictionary_data_for_key(config->values, name))); wi_lock_unlock(config->lock); return value ? wi_number_bool(value) : 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, ""); }
wi_boolean_t wi_p7_message_set_number_for_name(wi_p7_message_t *p7_message, wi_number_t *number, wi_string_t *field_name) { wi_p7_spec_field_t *field; wi_p7_spec_type_t *type; if(!number) number = wi_number_with_int32(0); 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 false; } 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 false; } switch(wi_p7_spec_type_id(type)) { case WI_P7_BOOL: return wi_p7_message_set_bool_for_name(p7_message, wi_number_bool(number), field_name); break; case WI_P7_ENUM: return wi_p7_message_set_enum_for_name(p7_message, wi_number_int32(number), field_name); break; case WI_P7_INT32: return wi_p7_message_set_int32_for_name(p7_message, wi_number_int32(number), field_name); break; case WI_P7_UINT32: return wi_p7_message_set_uint32_for_name(p7_message, wi_number_int32(number), field_name); break; case WI_P7_INT64: return wi_p7_message_set_int64_for_name(p7_message, wi_number_int64(number), field_name); break; case WI_P7_UINT64: return wi_p7_message_set_uint64_for_name(p7_message, wi_number_int64(number), field_name); break; case WI_P7_DOUBLE: return wi_p7_message_set_double_for_name(p7_message, wi_number_double(number), field_name); break; default: wi_error_set_libwired_error_with_format(WI_ERROR_P7_INVALIDARGUMENT, WI_STR("Field \"%@\" is not a number"), field_name); break; } return false; }
static wi_boolean_t _wi_plist_write_instance_to_node(wi_runtime_instance_t *instance, xmlNodePtr node) { wi_enumerator_t *enumerator; wi_array_t *keys; wi_runtime_instance_t *value; xmlNodePtr child_node; void *key; wi_runtime_id_t id; wi_number_type_t type; id = wi_runtime_id(instance); if(id == wi_string_runtime_id()) { wi_xml_node_new_child(node, WI_STR("string"), instance); } else if(id == wi_number_runtime_id()) { type = wi_number_type(instance); if(type == WI_NUMBER_BOOL) { if(wi_number_bool(instance)) wi_xml_node_new_child(node, WI_STR("true"), NULL); else wi_xml_node_new_child(node, WI_STR("false"), NULL); } else { if(type == WI_NUMBER_FLOAT || type == WI_NUMBER_DOUBLE) wi_xml_node_new_child(node, WI_STR("real"), wi_number_string(instance)); else wi_xml_node_new_child(node, WI_STR("integer"), wi_number_string(instance)); } } else if(id == wi_data_runtime_id()) { wi_xml_node_new_child(node, WI_STR("data"), wi_data_base64(instance)); } else if(id == wi_date_runtime_id()) { wi_xml_node_new_child(node, WI_STR("date"), wi_date_string_with_format(instance, WI_STR("%Y-%m-%dT%H:%M:%SZ"))); } else if(id == wi_dictionary_runtime_id()) { child_node = wi_xml_node_new_child(node, WI_STR("dict"), NULL); keys = wi_dictionary_all_keys(instance); wi_array_sort(keys, wi_string_compare); enumerator = wi_array_data_enumerator(keys); while((key = wi_enumerator_next_data(enumerator))) { value = wi_dictionary_data_for_key(instance, key); wi_xml_node_new_child(child_node, WI_STR("key"), key); if(!_wi_plist_write_instance_to_node(value, child_node)) return false; } } else if(id == wi_array_runtime_id()) { child_node = wi_xml_node_new_child(node, WI_STR("array"), NULL); xmlAddChild(node, child_node); enumerator = wi_array_data_enumerator(instance); while((value = wi_enumerator_next_data(enumerator))) { if(!_wi_plist_write_instance_to_node(value, child_node)) return false; } } else { wi_error_set_libwired_error_with_format(WI_ERROR_PLIST_WRITEFAILED, WI_STR("Unhandled class %@"), wi_runtime_class_name(instance)); return false; } return true; }
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; }