wi_boolean_t wi_p7_message_set_enum_name_for_name(wi_p7_message_t *p7_message, wi_string_t *enum_name, wi_string_t *field_name) { wi_p7_spec_field_t *field; wi_dictionary_t *enums; wi_p7_enum_t enum_value; 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); if(wi_p7_message_debug) wi_log_debug(WI_STR("wi_p7_message_set_enum_name_for_name: %m")); return false; } enums = wi_p7_spec_field_enums_by_name(field); if(!wi_dictionary_contains_key(enums, enum_name)) { wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD, WI_STR("No value found for enum \"%@\""), enum_name); if(wi_p7_message_debug) wi_log_debug(WI_STR("wi_p7_message_set_enum_name_for_name: %m")); return false; } enum_value = (wi_p7_enum_t) (intptr_t) wi_dictionary_data_for_key(enums, enum_name); return wi_p7_message_set_enum_for_name(p7_message, enum_value, field_name); }
wi_string_t * wi_p7_message_enum_name_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) { wi_p7_spec_field_t *field; wi_dictionary_t *enums; wi_p7_enum_t enum_value; if(!wi_p7_message_get_enum_for_name(p7_message, &enum_value, field_name)) return NULL; field = wi_p7_spec_field_with_name(p7_message->spec, field_name); enums = wi_p7_spec_field_enums_by_value(field); if(!wi_dictionary_contains_key(enums, (void *) (intptr_t) enum_value)) { wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD, WI_STR("No name found for enum \"%u\""), enum_value); if(wi_p7_message_debug) wi_log_debug(WI_STR("wi_p7_message_enum_name_for_name: %m")); return NULL; } return wi_dictionary_data_for_key(enums, (void *) (intptr_t) enum_value); }
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; }
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; }