Commit db0a5a58 authored by Titouan Soulard's avatar Titouan Soulard

libpotoml: add parsing of long

parent 1a31dde3
......@@ -23,7 +23,8 @@ enum PotomlState {
PotomlAfterPropertyName = 0x31,
PotomlBeforePropertyValue = 0x32,
PotomlPropertyValueString = 0x40,
PotomlPropertyValueDouble = 0x41,
PotomlPropertyValueLong = 0x41,
PotomlPropertyValueDouble = 0x42,
};
struct PotomlTomlSubset {
......@@ -37,6 +38,7 @@ char potoml_toml_parse_file(struct CommonHashtableTable *hashtable, char *toml_f
struct PotomlTomlSubset *potoml_toml_create_subset(struct CommonHashtableTable *hashtable, const char *section_name);
char *potoml_toml_get_string(struct PotomlTomlSubset *subset, const char *property);
double potoml_toml_get_double(struct PotomlTomlSubset *subset, const char *property);
long potoml_toml_get_long(struct PotomlTomlSubset *subset, const char *property);
void potoml_toml_subset_free(struct PotomlTomlSubset *subset);
void potoml_toml_free(struct CommonHashtableTable *hashtable);
......@@ -6,10 +6,8 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
size_t section_length = 0;
size_t property_length = 0;
size_t value_length = 0;
double *double_value;
size_t ci;
char c;
char s = PotomlLineBeginning;
......@@ -17,6 +15,8 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
char *property = malloc(POTOML_MAX_PROPERTY_LENGTH * sizeof(char));
char *value = malloc(POTOML_MAX_PROPERTY_LENGTH * sizeof(char));
void *casted_value;
for(ci = 0; ci < toml_length; ci++) {
c = toml[ci];
......@@ -85,7 +85,7 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
if(c == '"') s = PotomlPropertyValueString;
else if(c >= 48 && c <= 57) {
ci--;
s = PotomlPropertyValueDouble;
s = PotomlPropertyValueLong;
} else if(c != ' ' && c != '\t') return InvalidCharacter;
break;
......@@ -103,19 +103,28 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
value_length++;
break;
case PotomlPropertyValueLong:
case PotomlPropertyValueDouble:
if(c == '\n' || c == ' ' || c == '\t') {
double_value = malloc(sizeof(double));
*double_value = strtod(value, NULL);
if(s == PotomlPropertyValueLong) {
casted_value = malloc(sizeof(long));
*((long *) casted_value) = strtol(value, NULL, 10);
} else /* if(s == PotomlPropertyValueDouble) */ {
casted_value = malloc(sizeof(double));
*((double *) casted_value) = strtod(value, NULL);
}
if(!common_hashtable_insert(hashtable, property, double_value)) {
if(!common_hashtable_insert(hashtable, property, casted_value)) {
return HashtableInsertError;
}
if(c == '\n') ci--;
s = PotomlLineEnding;
break;
} else if((c < 48 || c > 57) && c != '.') return InvalidCharacter;
} else if(c == '.') {
if(s == PotomlPropertyValueLong) s = PotomlPropertyValueDouble;
else return InvalidCharacter;
} else if(c < 48 || c > 57) return InvalidCharacter;
value[value_length] = c;
value_length++;
......@@ -167,10 +176,10 @@ struct PotomlTomlSubset *potoml_toml_create_subset(struct CommonHashtableTable *
char *owned_section_name = malloc((section_name_length + 2) * sizeof(char));
if(!owned_section_name) return NULL;
// Append dot to section name
strcpy(owned_section_name, section_name);
strcat(owned_section_name, ".");
// Append dot to section name if non-empty
if(section_name_length != 0) strcat(owned_section_name, ".");
subset = (struct PotomlTomlSubset *) malloc(sizeof(struct PotomlTomlSubset));
......@@ -228,6 +237,30 @@ double potoml_toml_get_double(struct PotomlTomlSubset *subset, const char *prope
return value;
}
long potoml_toml_get_long(struct PotomlTomlSubset *subset, const char *property) {
struct CommonHashtableElement *el;
size_t property_length;
long value;
char *full_property;
value = 0;
property_length = strlen(property);
full_property = (char *) malloc(property_length + subset->section_name_length + 1);
strcpy(full_property, subset->section_name);
strcat(full_property, property);
el = common_hashtable_find(subset->hashtable, full_property);
if(el) {
// Clone instead of giving reference for long
value = *((long *) el->content);
}
free(full_property);
return value;
}
void potoml_toml_subset_free(struct PotomlTomlSubset *subset) {
free((void *) subset->section_name);
free((void *) subset);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment