Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a remove_entry function #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ static ini_entry_s* _ini_entry_get(ini_table_s* table, const char* section_name,
return entry;
}

static void _ini_section_remove_reorder(ini_section_s* section, int remove)
{
free(section->entry[remove].key);
free(section->entry[remove].value);

memmove(&section->entry[remove], &section->entry[remove+1], (section->size - remove) * sizeof(ini_entry_s));
section->size -= 1;
}

static bool _ini_section_remove_entry(ini_section_s* section, ini_entry_s* entry)
{
for (int i = 0; i < section->size; i++) {
if (entry == &section->entry[i]) {
_ini_section_remove_reorder(section, i);
return true;
}
}

return false;
}

ini_table_s* ini_table_create()
{
ini_table_s* table = malloc(sizeof(ini_table_s));
Expand Down Expand Up @@ -111,7 +132,7 @@ bool ini_table_read_from_file(ini_table_s* table, const char* file)
int position = 0;
int spaces = 0;
int line = 0;
int buffer_size = 128 * sizeof(char);
int buffer_size = 1024 * sizeof(char);
char* buf = malloc(buffer_size);
char* value = NULL;

Expand All @@ -134,6 +155,15 @@ bool ini_table_read_from_file(ini_table_s* table, const char* file)
default: if (buf[0] != '\0') spaces++; break;
}
break;
case '#':
if (state == Value) {
buf[position++] = c;
break;
}

line++;
state=Key;
break;
case ';':
if (state == Value) {
buf[position++] = c;
Expand Down Expand Up @@ -281,3 +311,51 @@ bool ini_table_get_entry_as_bool(ini_table_s* table, const char* section_name,
return true;
}

bool ini_table_remove_entry(ini_table_s* table, const char* section_name,
const char* key)
{

ini_section_s* section = _ini_section_find(table, section_name);
if (section == NULL) {
return false;
}

ini_entry_s* entry = _ini_entry_find(section, key);
if (entry == NULL) {
return false;
}

return _ini_section_remove_entry(section, entry);
}

int ini_table_get_section_count(ini_table_s* table)
{
return table->size;
}

char* ini_table_get_section_name(ini_table_s* table, int section)
{
return table->section[section].name;
}

int ini_table_get_entry_count(ini_table_s* table, const char* section_name)
{
ini_section_s* section = _ini_section_find(table, section_name);

if (section == NULL){
return -1;
}

return section->size;
}

char* ini_table_get_entry_from_index(ini_table_s* table, const char* section_name, int entry)
{
ini_section_s* section = _ini_section_find(table, section_name);

if (section == NULL){
return NULL;
}

return section->entry[entry].key;
}
20 changes: 20 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ bool ini_table_write_to_file(ini_table_s* table, const char* file);
void ini_table_create_entry(ini_table_s* table, const char* section_name,
const char* key, const char* value);

/**
* @brief Removes an entry in the `table' containing the `key' and `value'
* provided it exists.
* Returns true if the entry was removed, otherwise false.
* @param table
* @param section_name
* @param key
*/
bool ini_table_remove_entry(ini_table_s* table, const char* section_name,
const char* key);

/**
* @brief Checks for the existance of an entry in the specified `table'. Returns
* false if the entry does not exist, otherwise true.
Expand Down Expand Up @@ -108,4 +119,13 @@ bool ini_table_get_entry_as_int(ini_table_s* table, const char* section_name,
bool ini_table_get_entry_as_bool(ini_table_s* table, const char* section_name,
const char* key, bool* value);


int ini_table_get_section_count(ini_table_s* table);

char* ini_table_get_section_name(ini_table_s* table, int section);

int ini_table_get_entry_count(ini_table_s* table, const char* section_name);

char* ini_table_get_entry_from_index(ini_table_s* table, const char* section_name, int entry);

#endif//_TCONFIG_H_