70 lines
1.2 KiB
C
70 lines
1.2 KiB
C
#include "namesarchive.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define INDEX_NOT_FOUND -1
|
|
|
|
static char *names[MAX_NAMES];
|
|
static unsigned int numberOfEntries = 0;
|
|
|
|
int addName(const char *name)
|
|
{
|
|
if(numberOfEntries < MAX_NAMES)
|
|
{
|
|
names[numberOfEntries] = (char *)malloc(strlen(name)+1);
|
|
|
|
if(names[numberOfEntries] != NULL)
|
|
{
|
|
strcpy(names[numberOfEntries], name);
|
|
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++)
|
|
{
|
|
free(names[i]);
|
|
names[i] = names[i+1];
|
|
}
|
|
numberOfEntries--;
|
|
}
|
|
|
|
int removeName(const char *name)
|
|
{
|
|
int idx = getNameIdx(name);
|
|
|
|
if(idx == INDEX_NOT_FOUND)
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
removeAt(idx);
|
|
return 1;
|
|
}
|
|
} |