1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
/* $Id: map.c 6135 2003-01-19 01:15:40Z rra $
**
*/
#include "config.h"
#include "clibrary.h"
#include <errno.h>
#include "libinn.h"
#include "paths.h"
#include "map.h"
typedef struct _PAIR {
char First;
char *Key;
char *Value;
} PAIR;
static PAIR *MAPdata;
static PAIR *MAPend;
/*
** Free the map.
*/
void
MAPfree(void)
{
PAIR *mp;
for (mp = MAPdata; mp < MAPend; mp++) {
free(mp->Key);
free(mp->Value);
}
free(MAPdata);
MAPdata = NULL;
}
/*
** Read the map file.
*/
void
MAPread(const char *name)
{
FILE *F;
int i;
PAIR *mp;
char *p;
char buff[BUFSIZ];
if (MAPdata != NULL)
MAPfree();
/* Open file, count lines. */
if ((F = fopen(name, "r")) == NULL) {
fprintf(stderr, "Can't open %s, %s\n", name, strerror(errno));
exit(1);
}
for (i = 0; fgets(buff, sizeof buff, F) != NULL; i++)
continue;
mp = MAPdata = xmalloc((i + 1) * sizeof(PAIR));
/* Read each line; ignore blank and comment lines. */
fseeko(F, 0, SEEK_SET);
while (fgets(buff, sizeof buff, F) != NULL) {
if ((p = strchr(buff, '\n')) != NULL)
*p = '\0';
if (buff[0] == '\0'
|| buff[0] == '#'
|| (p = strchr(buff, ':')) == NULL)
continue;
*p++ = '\0';
mp->First = buff[0];
mp->Key = xstrdup(buff);
mp->Value = xstrdup(p);
mp++;
}
fclose(F);
MAPend = mp;
}
/*
** Look up a name in the map, return original value if not found.
*/
char *
MAPname(char *p)
{
PAIR *mp;
char c;
for (c = *p, mp = MAPdata; mp < MAPend; mp++)
if (c == mp->First && strcmp(p, mp->Key) == 0)
return mp->Value;
return p;
}
|