add: endpoint /tags

fix: leaks from config (now using static length for config strings)
This commit is contained in:
Tobias Reisinger 2020-06-13 19:21:32 +02:00
parent 0f1cd9c02c
commit a78815cb32
10 changed files with 147 additions and 16 deletions
models

View file

@ -77,6 +77,50 @@ tag_get_tag(int id)
return result;
}
char**
tag_get_all()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT tag FROM tags;", -1, &stmt, NULL);
char **all_tags = malloc(sizeof(char*));
int row = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
const char *new_tag = (const char *)sqlite3_column_text(stmt, 0);
int new_tag_len = strlen(new_tag);
row++;
all_tags = (char**)realloc(all_tags, sizeof(char*) * (row + 1));
all_tags[row - 1] = malloc(sizeof(char) * (new_tag_len + 1));
strcpy(all_tags[row - 1], new_tag);
}
else
{
if(s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR("error selecting tags from database: %s\n", sqlite3_errstr(s));
break;
}
}
}
sqlite3_finalize(stmt);
all_tags[row] = NULL;
return all_tags;
}
int
tag_get_id(const char *tag)
{