mirror of
https://github.com/gnif/LookingGlass.git
synced 2024-11-25 06:47:19 +00:00
[common] stringlist: use vector for storage
This commit is contained in:
parent
e6df0acad9
commit
e5f2b3079e
@ -19,62 +19,61 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "common/stringlist.h"
|
#include "common/stringlist.h"
|
||||||
|
#include "common/vector.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct StringList
|
struct StringList
|
||||||
{
|
{
|
||||||
bool owns_strings;
|
bool owns_strings;
|
||||||
unsigned int size;
|
Vector vector;
|
||||||
unsigned int count;
|
|
||||||
char ** list;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
StringList stringlist_new(bool owns_strings)
|
StringList stringlist_new(bool owns_strings)
|
||||||
{
|
{
|
||||||
StringList sl = malloc(sizeof(*sl));
|
StringList sl = malloc(sizeof(*sl));
|
||||||
|
|
||||||
sl->owns_strings = owns_strings;
|
sl->owns_strings = owns_strings;
|
||||||
sl->size = 32;
|
|
||||||
sl->count = 0;
|
|
||||||
sl->list = malloc(sizeof(char *) * sl->size);
|
|
||||||
|
|
||||||
|
if (!vector_create(&sl->vector, sizeof(char *), 32))
|
||||||
|
{
|
||||||
|
free(sl);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return sl;
|
return sl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stringlist_free(StringList * sl)
|
void stringlist_free(StringList * sl)
|
||||||
{
|
{
|
||||||
if ((*sl)->owns_strings)
|
if ((*sl)->owns_strings)
|
||||||
for(unsigned int i = 0; i < (*sl)->count; ++i)
|
{
|
||||||
free((*sl)->list[i]);
|
char * ptr;
|
||||||
|
vector_forEach(ptr, &(*sl)->vector)
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
free((*sl)->list);
|
vector_destroy(&(*sl)->vector);
|
||||||
free((*sl));
|
free((*sl));
|
||||||
*sl = NULL;
|
*sl = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int stringlist_push (StringList sl, char * str)
|
int stringlist_push(StringList sl, char * str)
|
||||||
{
|
{
|
||||||
if (sl->count == sl->size)
|
int index = vector_size(&sl->vector);
|
||||||
{
|
vector_push(&sl->vector, &str);
|
||||||
sl->size += 32;
|
|
||||||
sl->list = realloc(sl->list, sizeof(*sl->list) * sl->size);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int index = sl->count;
|
|
||||||
sl->list[sl->count++] = str;
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int stringlist_count(StringList sl)
|
unsigned int stringlist_count(StringList sl)
|
||||||
{
|
{
|
||||||
return sl->count;
|
return vector_size(&sl->vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
char * stringlist_at(StringList sl, unsigned int index)
|
char * stringlist_at(StringList sl, unsigned int index)
|
||||||
{
|
{
|
||||||
if (index >= sl->count)
|
if (index >= vector_size(&sl->vector))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return sl->list[index];
|
char * ptr;
|
||||||
|
vector_at(&sl->vector, index, &ptr);
|
||||||
|
return ptr;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user