2025-10-16 12:49:38 +02:00

79 lines
1.4 KiB
C

#include "namesarchive.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define INDEX_NOT_FOUND -1
static char names[MAX_NAMES][MAX_NAME_LEN+1];
static unsigned int numberOfEntries = 0;
/*
static void cpy(char *dst, const char *src, unsigned int max)
{
int i;
for(i = 0; i < max && *src != '\0'; i++)
{
*dst = *src;
dst++;
src++;
}
if(i < max)
*dst = '\0';
}*/
int addName(const char *name)
{
if(numberOfEntries < MAX_NAMES)
{
strncpy(names[numberOfEntries], name, MAX_NAME_LEN+1);
names[numberOfEntries][MAX_NAME_LEN] = '\0';
numberOfEntries++;
return 1;
}
return 0;
}
void printNames()
{
for(int i = 0; i < numberOfEntries; i++)
{
printf("%s\n", names[i]);
}
}
static int getNameIdx(const char *name)
{
for(int i = 0; i < numberOfEntries; i++)
{
if(strcmp(name, names[i]) == 0)
return i;
}
return INDEX_NOT_FOUND;
}
static void removeAt(int idx)
{
for(int i = idx; i < numberOfEntries-1; i++)
{
strncpy(names[i], names[i+1], MAX_NAME_LEN+1);
names[i][MAX_NAME_LEN] = '\0';
}
numberOfEntries--;
}
int removeName(const char *name)
{
int idx = getNameIdx(name);
if(idx == INDEX_NOT_FOUND)
{
return 0;
}
else
{
removeAt(idx);
return 1;
}
}