Commit 18460064 authored by Titouan Soulard's avatar Titouan Soulard

libpotoml: allow getting parameters from subsets

parent eb08a9d1
......@@ -9,7 +9,7 @@
// After these operations, a simple character folding algorithm is
// applied, based on multiplying by a prime number, choosen here for
// operations on chars.
unsigned char common_hashtable_compute_hash(char *str, unsigned char modulus) {
unsigned char common_hashtable_compute_hash(const char *str, unsigned char modulus) {
unsigned char result = 0;
unsigned char extended_char;
......@@ -62,7 +62,7 @@ void common_hashtable_free(struct CommonHashtableTable *hashtable) {
// Elements are stored in a plane of the hashtable size, and collisions are
// solved by finding the first empty place after the theoretical (hash-given)
// element's position.
struct CommonHashtableElement *common_hashtable_find(struct CommonHashtableTable *hashtable, char *name) {
struct CommonHashtableElement *common_hashtable_find(struct CommonHashtableTable *hashtable, const char *name) {
struct CommonHashtableElement *el;
unsigned char initial_pos;
unsigned char pos;
......@@ -80,7 +80,7 @@ struct CommonHashtableElement *common_hashtable_find(struct CommonHashtableTable
return NULL;
}
bool common_hashtable_insert(struct CommonHashtableTable *hashtable, char *name, void *content) {
bool common_hashtable_insert(struct CommonHashtableTable *hashtable, const char *name, void *content) {
struct CommonHashtableElement *el;
unsigned char initial_pos;
unsigned char pos;
......
......@@ -17,9 +17,9 @@ struct CommonHashtableElement {
void *content;
};
unsigned char common_hashtable_compute_hash(char *str, unsigned char modulus);
unsigned char common_hashtable_compute_hash(const char *str, unsigned char modulus);
bool common_hashtable_initialize(struct CommonHashtableTable *hashtable, unsigned char hashtable_size);
void common_hashtable_free(struct CommonHashtableTable *hashtable);
struct CommonHashtableElement *common_hashtable_find(struct CommonHashtableTable *hashtable, char *name);
bool common_hashtable_insert(struct CommonHashtableTable *hashtable, char *name, void *content);
struct CommonHashtableElement *common_hashtable_find(struct CommonHashtableTable *hashtable, const char *name);
bool common_hashtable_insert(struct CommonHashtableTable *hashtable, const char *name, void *content);
......@@ -26,7 +26,17 @@ enum PotomlState {
PotomlPropertyValueDouble = 0x41,
};
struct PotomlTomlSubset {
struct CommonHashtableTable *hashtable;
char *section_name;
size_t section_name_length;
};
char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml);
char potoml_toml_parse_file(struct CommonHashtableTable *hashtable, char *toml_file_path);
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);
void potoml_toml_subset_free(struct PotomlTomlSubset *subset);
void potoml_toml_free(struct CommonHashtableTable *hashtable);
......@@ -160,6 +160,79 @@ char potoml_toml_parse_file(struct CommonHashtableTable *hashtable, char *toml_f
return out;
}
struct PotomlTomlSubset *potoml_toml_create_subset(struct CommonHashtableTable *hashtable, const char *section_name) {
struct PotomlTomlSubset *subset;
size_t section_name_length = strlen(section_name);
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, ".");
subset = (struct PotomlTomlSubset *) malloc(sizeof(struct PotomlTomlSubset));
if(!subset) return NULL;
subset->hashtable = hashtable;
subset->section_name = owned_section_name;
subset->section_name_length = section_name_length + 1;
return subset;
}
char *potoml_toml_get_string(struct PotomlTomlSubset *subset, const char *property) {
struct CommonHashtableElement *el;
size_t property_length;
char *full_property;
property_length = strlen(property);
full_property = (char *) malloc((property_length + subset->section_name_length + 1) * sizeof(char));
strcpy(full_property, subset->section_name);
strcat(full_property, property);
el = common_hashtable_find(subset->hashtable, full_property);
if(!el) {
return NULL;
}
free(full_property);
return (char *) el->content;
}
double potoml_toml_get_double(struct PotomlTomlSubset *subset, const char *property) {
struct CommonHashtableElement *el;
size_t property_length;
double value;
char *full_property;
value = 0.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 double
value = *((double *) el->content);
}
free(full_property);
return value;
}
void potoml_toml_subset_free(struct PotomlTomlSubset *subset) {
free((void *) subset->section_name);
free((void *) subset);
}
void potoml_toml_free(struct CommonHashtableTable *hashtable) {
unsigned char i;
struct CommonHashtableElement *el;
......
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