mirror of
https://github.com/pret/pokeplatinum.git
synced 2026-05-12 05:55:46 -05:00
107 lines
2.3 KiB
C
107 lines
2.3 KiB
C
#include "string_list.h"
|
|
|
|
#include <nitro.h>
|
|
#include <string.h>
|
|
|
|
#include "heap.h"
|
|
#include "message.h"
|
|
#include "string_gf.h"
|
|
|
|
static StringList *FindFirstEmptyEntry(StringList *list, u32 *outHeapID);
|
|
static void FreeEntries(StringList *list);
|
|
|
|
StringList *StringList_New(u32 capacity, u32 heapID)
|
|
{
|
|
StringList *list = Heap_Alloc(heapID, sizeof(StringList) * (capacity + 1));
|
|
|
|
if (list) {
|
|
u32 i;
|
|
for (i = 0; i < capacity; i++) {
|
|
list[i].entry = NULL;
|
|
list[i].index = 0;
|
|
}
|
|
|
|
// This entry is special; it should always have an index value equal
|
|
// to the heap on which this list was allocated.
|
|
list[i].entry = STRING_LIST_TERMINATOR;
|
|
list[i].index = heapID;
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
void StringList_Free(StringList *list)
|
|
{
|
|
FreeEntries(list);
|
|
Heap_Free(list);
|
|
}
|
|
|
|
void StringList_AddFromMessageBank(StringList *list, const MessageLoader *loader, u32 bankEntry, u32 index)
|
|
{
|
|
u32 tmp;
|
|
list = FindFirstEmptyEntry(list, &tmp);
|
|
|
|
if (list) {
|
|
list->entry = MessageLoader_GetNewString(loader, bankEntry);
|
|
list->index = index;
|
|
}
|
|
}
|
|
|
|
void StringList_AddFromString(StringList *list, const String *string, u32 index)
|
|
{
|
|
u32 heapID;
|
|
list = FindFirstEmptyEntry(list, &heapID);
|
|
|
|
if (list) {
|
|
list->entry = String_Clone(string, heapID);
|
|
list->index = index;
|
|
}
|
|
}
|
|
|
|
void StringList_AddFromEntry(StringList *list, const StringList *entry)
|
|
{
|
|
u32 tmp;
|
|
list = FindFirstEmptyEntry(list, &tmp);
|
|
|
|
if (list) {
|
|
list->entry = entry->entry;
|
|
list->index = entry->index;
|
|
}
|
|
}
|
|
|
|
static StringList *FindFirstEmptyEntry(StringList *list, u32 *outHeapID)
|
|
{
|
|
while (list->entry != NULL) {
|
|
if (list->entry == STRING_LIST_TERMINATOR) {
|
|
GF_ASSERT(FALSE);
|
|
return NULL;
|
|
}
|
|
|
|
list++;
|
|
}
|
|
|
|
StringList *empty = list;
|
|
|
|
// Keep going, so we can also get the heap ID
|
|
while (list->entry != STRING_LIST_TERMINATOR) {
|
|
list++;
|
|
}
|
|
|
|
*outHeapID = list->index;
|
|
return empty;
|
|
}
|
|
|
|
static void FreeEntries(StringList *list)
|
|
{
|
|
StringList *tmp = list;
|
|
while (tmp->entry != STRING_LIST_TERMINATOR) {
|
|
if (tmp->entry == NULL) {
|
|
break;
|
|
}
|
|
|
|
String_Free(tmp->entry);
|
|
tmp->entry = NULL;
|
|
tmp++;
|
|
}
|
|
}
|