Ejemplo n.º 1
0
wi_date_t * wi_date_init_with_string(wi_date_t *date, wi_string_t *string, wi_string_t *format) {
    wi_regexp_t         *regexp;
    wi_string_t         *substring;
    struct tm           tm;
    time_t              clock;
    wi_uinteger_t       count, offset, hours, minutes;

    memset(&tm, 0, sizeof(tm));

    if(!strptime(wi_string_utf8_string(string), wi_string_utf8_string(format), &tm)) {
        wi_release(date);
        
        return NULL;
    }

    offset = 0;

    if(wi_string_contains_string(format, WI_STR("%z"), WI_STRING_CASE_INSENSITIVE)) {
        regexp      = wi_regexp_with_pattern(WI_STR("((\\+|\\-)[0-9]{4})"), 0);
        substring   = wi_regexp_string_of_first_match_in_string(regexp, string);
        
        if(substring) {
            hours       = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(1, 2)));
            minutes     = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(3, 2)));
            offset      = (hours * 3600) + (minutes * 60);
            
            if(wi_string_has_prefix(substring, WI_STR("-")))
                offset = -offset;
        }
    }

    clock = wi_timegm(&tm) - offset;
    
    return wi_date_init_with_time(date, clock);
}
Ejemplo n.º 2
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, "");
}
Ejemplo n.º 3
0
static wi_boolean_t _wi_settings_set_port(wi_settings_t *settings, wi_uinteger_t index, wi_string_t *name, wi_string_t *value) {
	struct servent		*servent;
	wi_uinteger_t		port;
	
	port = wi_string_uinteger(value);
	
	if(port > 65535) {
		wi_error_set_libwired_error(WI_ERROR_SETTINGS_INVALIDPORT);
		
		return false;
	}
	
	if(port == 0) {
		servent = getservbyname(wi_string_cstring(value), "tcp");
		
		if(!servent) {
			wi_error_set_libwired_error(WI_ERROR_SETTINGS_NOSUCHSERVICE);

			return false;
		}
		
		port = servent->s_port;
	}
	
	*(wi_uinteger_t *) settings->spec[index].setting = port;
	
	return true;
}
Ejemplo n.º 4
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.º 5
0
static void wr_command_open(wi_array_t *arguments) {
	wi_mutable_array_t		*argv;
	wi_string_t				*login, *password, *host;
	wi_url_t				*url;
	const char				**xargv;
	wi_uinteger_t			port;
	int						ch;
	
	url			= NULL;
	login		= NULL;
	password	= NULL;
	port		= 0;

	argv = wi_autorelease(wi_mutable_copy(arguments));
	
	wi_mutable_array_insert_data_at_index(argv, WI_STR("open"), 0);
	
	xargv = wi_array_create_argv(argv);
	
	wi_getopt_reset();

	while((ch = getopt(wi_array_count(argv), (char **) xargv, "hl:P:p:")) != -1) {
		switch(ch) {
			case 'l':
				login = wi_string_with_cstring(optarg);
				break;

			case 'P':
				port = wi_string_uinteger(wi_string_with_cstring(optarg));
				break;

			case 'p':
				password = wi_string_with_cstring(optarg);
				break;

			case '?':
			case 'h':
			default:
				wr_commands_print_usage_for_command(WI_STR("open"));

				return;
				break;
		}
	}
	
	wi_array_destroy_argv(wi_array_count(argv), xargv);
	
	if((wi_uinteger_t) optind >= wi_array_count(argv)) {
		wr_commands_print_usage_for_command(WI_STR("open"));
		
		return;
	}
	
	url = wi_autorelease(wi_url_init_with_string(wi_url_alloc(), WI_ARRAY(argv, optind)));
	host = wi_url_host(url);
	
	if(port == 0)
		port = wi_url_port(url);
	
	if(wi_string_length(host) == 0) {
		wr_commands_print_usage_for_command(WI_STR("open"));
		
		return;
	}
	
	wr_client_connect(host, port, login, password);
}