File: node.c

package info (click to toggle)
diablo 1.16.test2-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 1,504 kB
  • ctags: 1,603
  • sloc: ansic: 17,654; perl: 2,054; sh: 260; csh: 118; makefile: 73
file content (146 lines) | stat: -rw-r--r-- 3,054 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

/*
 * LIB/NODE.C	- Simple string node routines
 *
 *
 * (c)Copyright 1997, Matthew Dillon, All Rights Reserved.  Refer to
 *    the COPYRIGHT file in the base directory of this distribution 
 *    for specific rights granted.
 *
 */

#include "defs.h"

Prototype void FreeNodeList(MemPool **pmp, Node **pnode);
Prototype void FreeNode(MemPool **pmp, Node *node);
Prototype Node *MakeNode(MemPool **pmp, const char *str, int val);
Prototype Node *MakeNode2(MemPool **pmp, const char *str1, const char *str2, int val);

#ifdef NOTDEF
const char *GetStrTab(const char *s);
void RelStrTab(const char *s);
int strhash(const char *s);
#endif

void
FreeNodeList(MemPool **pmp, Node **pnode)
{
    Node *no;

    while ((no = *pnode) != NULL) {
	*pnode = no->no_Next;
	FreeNode(pmp, no);
    }
}

void
FreeNode(MemPool **pmp, Node *node)
{
    if (node->no_Name2)
	zfree(pmp, node, sizeof(Node) + strlen(node->no_Name) + strlen(node->no_Name2) + 2);
    else
	zfree(pmp, node, sizeof(Node) + strlen(node->no_Name) + 1);
}

Node *
MakeNode(MemPool **pmp, const char *str, int val)
{
    Node *node = zalloc(pmp, sizeof(Node) + strlen(str) + 1);

    node->no_Next = NULL;
    node->no_Name = (char *)(node + 1);
    node->no_Name2 = NULL;
    node->no_Data = NULL;
    node->no_Value = val;
    strcpy((char *)node->no_Name, str);
    return(node);
}

Node *
MakeNode2(MemPool **pmp, const char *str1, const char *str2, int val)
{
    Node *node = zalloc(pmp, sizeof(Node) + strlen(str1) + strlen(str2) + 2);

    node->no_Next = NULL;
    node->no_Name = (char *)(node + 1);
    node->no_Name2 = node->no_Name + strlen(str1) + 1;
    node->no_Data = NULL;
    node->no_Value = val;
    strcpy((char *)node->no_Name, str1);
    strcpy((char *)node->no_Name2, str2);

    return(node);
}

#ifdef NOTDEF

/*
 *
 */

#define STHSIZE         128
#define STHMASK         (STHSIZE-1)

typedef struct STab {
    struct STab *st_Next;
    int         st_Refs;
    int         st_Hv;
} STab;

STab    *STHash[STHSIZE];

const char *
GetStrTab(const char *s)
{
    int hv = strhash(s);
    STab **pst = &STHash[hv & STHMASK];
    STab *st;

    while ((st = *pst) != NULL) {
        if (st->st_Hv == hv && strcmp((char *)(st + 1), s) == 0)
            break;
        pst = &st->st_Next;
    }
    if (st == NULL) {
        st = *pst = malloc(sizeof(STab) + strlen(s) + 1);
        st->st_Next = NULL;
        st->st_Hv = hv;
        st->st_Refs = 0;
        strcpy((char *)(st + 1), s);
    }
    ++st->st_Refs;
    return((char *)(st + 1));
}

void
RelStrTab(const char *s)
{
    int hv = strhash(s);
    STab **pst = &STHash[hv & STHMASK];
    STab *st;

    while ((st = *pst) != NULL) {
        if (st->st_Hv == hv && strcmp((char *)(st + 1), s) == 0)
            break;
        pst = &st->st_Next;
    }
    if (st && --st->st_Refs == 0) {
        *pst = st->st_Next;
        free(st);
    }
}

int
strhash(const char *s)
{
    int hv = 0xA45CD32F;

    while (*s) {
        hv = (hv << 5) ^ *s ^ (hv >> 23);
        ++s;
    }
    return(hv ^ (hv > 16));
}

#endif