static int test_get_complex(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "; last modified 1 April 2001 by John Doe\n" "[owner]\n" "name = John Doe\n" "organization = Acme Widgets Inc.\n" "\n" "[database]\n" "; use IP address in case network name resolution is not working\n" "server = 192.0.2.62\n" "port = 143\n" "file = \"payroll.dat\"\n"; char value[32]; long long_value; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* owner -> name. */ BTASSERT(configfile_get(&configfile, "owner", "name", value, sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "John Doe") == 0); /* owner -> organization. */ BTASSERT(configfile_get(&configfile, "owner", "organization", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "Acme Widgets Inc.") == 0); /* database -> server. */ BTASSERT(configfile_get(&configfile, "database", "server", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "192.0.2.62") == 0); /* database -> port. */ BTASSERT(configfile_get_long(&configfile, "database", "port", &long_value) == 0); BTASSERT(long_value == 143); /* database -> file. */ BTASSERT(configfile_get(&configfile, "database", "file", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "\"payroll.dat\"") == 0); return (0); }
static int test_get_whitespace(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list] \n" "milk : 3 \t \r\n" "cheese\t: 1 cheddar \r\n"; char value[16]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Get the value of property 'milk' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "milk", value, sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "3") == 0); /* Get the value of property 'cheese' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "cheese", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "1 cheddar") == 0); return (0); }
static int test_get_comments(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list]\n" "#milk = 3\n\r" "cheese = 1 cheddar\r\n" ";ham = 1"; char value[16]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Get the value of property 'milk' in section 'shopping list'. Finds nothing since the milk line is a coment.*/ BTASSERT(configfile_get(&configfile, "shopping list", "milk", value, sizeof(value)) == NULL); /* Get the value of property 'cheese' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "cheese", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "1 cheddar") == 0); return (0); }
static int test_get_colon(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list]\r\n" "milk: 3\r\n" "cheese: 1 cheddar\r\n"; char value[16]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Get the value of property 'milk' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "milk", value, sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "3") == 0); /* Get the value of property 'cheese' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "cheese", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "1 cheddar") == 0); /* Get the value of a non-existing property in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "ham", &value[0], sizeof(value)) == NULL); /* Get the value of a non-existing property in a non-existing section. */ BTASSERT(configfile_get(&configfile, "clothes", "skirt", &value[0], sizeof(value)) == NULL); return (0); }
static int test_get_line_termination(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list]\n" "\r\n" "milk = 3\n\r" "cheese = 1 cheddar\r\n" "ham = 1"; char value[16]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Get the value of property 'milk' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "milk", value, sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "3") == 0); /* Get the value of property 'cheese' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "cheese", &value[0], sizeof(value)) == &value[0]); BTASSERT(strcmp(&value[0], "1 cheddar") == 0); /* Bad line termination NULL of the ham line. */ BTASSERT(configfile_get(&configfile, "shopping list", "ham", &value[0], sizeof(value)) == NULL); return (0); }
static int test_get_missing_section_termination(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list\r\n"; char value[16]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Get the value of property 'milk' in section 'shopping list'. */ BTASSERT(configfile_get(&configfile, "shopping list", "milk", value, sizeof(value)) == NULL); return (0); }
static int test_get_value_too_long(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list]\r\n" "milk: 12\r\n"; char value[2]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Value too long. */ BTASSERT(configfile_get(&configfile, "shopping list", "milk", &value[0], sizeof(value)) == NULL); return (0); }
static int test_get_bad_property_format(struct harness_t *harness_p) { struct configfile_t configfile; char buf[] = "[shopping list]\r\n" "milk ; 3\r\n"; char value[16]; BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0); /* Bad property format. */ BTASSERT(configfile_get(&configfile, "shopping list", "milk", &value[0], sizeof(value)) == NULL); return (0); }
int configfile_get_float(struct configfile_t *self_p, const char *section_p, const char *property_p, float *value_p) { char buf[32]; char *next_p; /* Get the property value as a string. */ if (configfile_get(self_p, section_p, property_p, buf, sizeof(buf)) == NULL) { return (-1); } /* Convert the property value string to a float. */ *value_p = strtod(buf, &next_p); if ((buf == next_p) || (*next_p != '\0')) { return (-1); } return (0); }
int configfile_get_long(struct configfile_t *self_p, const char *section_p, const char *property_p, long *value_p) { char buf[16]; const char *next_p; /* Get the property value as a string. */ if (configfile_get(self_p, section_p, property_p, buf, sizeof(buf)) == NULL) { return (-1); } /* Convert the property value string to a long. */ next_p = std_strtol(buf, value_p); if ((next_p == NULL) || (*next_p != '\0')) { return (-1); } return (0); }