Dawn/src/util/dictionary.c
2021-11-17 09:08:27 -08:00

51 lines
1.1 KiB
C

/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dictionary.h"
int32_t dictionaryGetIndex(char **keys, char *name, int32_t count) {
int32_t i;
for(i = 0; i < count; i++) {
if(keys[i] == NULL) continue;
if(strcmp(keys[i], name) == 0) return i;
}
return -1;
}
int32_t dictionaryGetOrAdd(
char **keys, char *name, int32_t *count, int32_t stride, char *buffer
) {
int32_t i;
int32_t firstFree = -1;
for(i = 0; i < *count; i++) {
if(keys[i] == NULL) {
if(firstFree == -1) firstFree = i;
continue;
}
if(strcmp(keys[i], name) == 0) return i;
}
if(firstFree == -1) {
firstFree = *count;
*count = *count + 1;
}
keys[i] = buffer + (sizeof(char) * i * stride);
memcpy(keys[i], name, strlen(name) + 1);
return *count;
}
void dictionaryRemoveByIndex(char **keys, int32_t i) {
keys[i] = NULL;
}
void dictionaryRemove(char **keys, char *name, int32_t count) {
int32_t i = dictionaryGetIndex(keys, name, count);
if(i == -1) return;
keys[i] = NULL;
}