File: map.c

package info (click to toggle)
inn2 2.2.2.2000.01.31-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,424 kB
  • ctags: 5,722
  • sloc: ansic: 61,219; perl: 9,939; sh: 5,644; makefile: 1,695; awk: 1,567; yacc: 1,548; lex: 249; tcl: 3
file content (100 lines) | stat: -rw-r--r-- 1,744 bytes parent folder | download | duplicates (2)
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
100
/*  $Revision: 1.1.1.1 $
**
*/
#include <stdio.h>
#include <sys/types.h>
#include "configdata.h"
#include "clibrary.h"
#include <errno.h>
#include "paths.h"
#include "libinn.h"
#include "macros.h"


typedef struct _PAIR {
    char	First;
    char	*Key;
    char	*Value;
} PAIR;

STATIC PAIR	*MAPdata;
STATIC PAIR	*MAPend;


/*
**  Free the map.
*/
void
MAPfree()
{
    register PAIR	*mp;

    for (mp = MAPdata; mp < MAPend; mp++) {
	DISPOSE(mp->Key);
	DISPOSE(mp->Value);
    }
    DISPOSE(MAPdata);
    MAPdata = NULL;
}


/*
**  Read the map file.
*/
void
MAPread(name)
    char		*name;
{
    register FILE	*F;
    register int	i;
    register PAIR	*mp;
    register char	*p;
    char		buff[BUFSIZ];

    if (MAPdata != NULL)
	MAPfree();

    /* Open file, count lines. */
    if ((F = fopen(name, "r")) == NULL) {
	(void)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 = NEW(PAIR, i + 1);

    /* Read each line; ignore blank and comment lines. */
    (void)fseek(F, (OFFSET_T)0, SEEK_SET);
    while (fgets(buff, sizeof buff, F) != NULL) {
	if ((p = strchr(buff, '\n')) != NULL)
	    *p = '\0';
	if (buff[0] == '\0'
	 || buff[0] == COMMENT_CHAR
	 || (p = strchr(buff, ':')) == NULL)
	    continue;
	*p++ = '\0';
	mp->First = buff[0];
	mp->Key = COPY(buff);
	mp->Value = COPY(p);
	mp++;
    }
    (void)fclose(F);
    MAPend = mp;
}


/*
**  Look up a name in the map, return original value if not found.
*/
char *
MAPname(p)
    register char	*p;
{
    register PAIR	*mp;
    register char	c;

    for (c = *p, mp = MAPdata; mp < MAPend; mp++)
	if (c == mp->First && EQ(p, mp->Key))
	    return mp->Value;
    return p;
}