1 /*
2  * list.c -- linear list management
3  *
4  * Copyright (C) 1991,1997,1999,2003 by Yoshifumi Mori
5  *
6  * tab:4
7  */
8 
9 #include "cdefs.h"
10 #include "extern.h"
11 
12 #define MAXLIST		6	/* �����ꥹ�ȿ� */
13 #define BUF_DEFAULT	512	/* ���ϥХåե��ǥե���ȥ����� */
14 
15 typedef struct list {
16 	struct list	*next;
17 	int			key1;
18 	int			key2;
19 	int			key3;
20 	char FAR	*msg;
21 } LIST;
22 
23 static LIST	*start[MAXLIST];
24 static char	*bufptr = NULL;
25 static int	buflen = 0;
26 #ifdef MEMCHK
27 static int	callcount;
28 #endif
29 
30 static void list_free2(int no);
31 
32 /*
33  * �����ꥹ�ȴ����ơ��֥����������
34  */
list_init(void)35 void list_init(void)
36 {
37 	list_free();
38 
39 	if (buflen == 0) {
40 		buflen = BUF_DEFAULT;
41 		bufptr = xmalloc(buflen);
42 	}
43 
44 #ifdef MEMCHK
45 	callcount = 0;
46 #endif
47 }
48 
49 /*
50  * �������ꥹ�Ȥ��������
51  */
list_free(void)52 void list_free(void)
53 {
54 	int	i;
55 
56 #ifdef MEMCHK
57 	errprint("list_free", ERR_DEBUG, "callcount = %d", callcount);
58 #endif
59 
60 	for (i = 0; i < MAXLIST; i++) {
61 		if (start[i] != NULL) {
62 			list_free2(i);
63 		}
64 	}
65 
66 	if (buflen != 0) {
67 		free(bufptr);
68 		bufptr = NULL;
69 		buflen = 0;
70 	}
71 }
72 
73 /*
74  * �����ꥹ�Ȥβ���
75  *
76  * ���ꤷ�������ꥹ�Ȥ��ΰ�����Ʋ�������
77  */
78 static
list_free2(int no)79 void list_free2(int no)
80 {
81 	LIST	*p, *q;
82 #ifdef MEMCHK
83 	int		listlen = 0;
84 #endif
85 
86 	p = start[no];
87 	while (p != NULL) {
88 		if (p->msg != NULL) {
89 			XFREE(p->msg);
90 		}
91 		q = p->next;
92 		free(p);
93 		p = q;
94 #ifdef MEMCHK
95 		listlen++;
96 #endif
97 	}
98 	start[no] = NULL;
99 #ifdef MEMCHK
100 	errprint("list_free", ERR_DEBUG, "No.%d listlen = %d", no, listlen);
101 #endif
102 }
103 
104 /*
105  * ���ꤷ�������ꥹ�ȤκǸ�˥ǡ���(msg)���ɲä���
106  */
list_add(int no,int key1,int key2,int key3,const char * msg)107 void list_add(int no, int key1, int key2, int key3, const char *msg)
108 {
109 	LIST	*p, *q;
110 
111 	if (no < 0 || no > (MAXLIST - 1)) {
112 		return ;
113 	}
114 
115 	p = (LIST *)xmalloc(sizeof(LIST));
116 	if (start[no] == NULL) {
117 		start[no] = p;
118 	} else {
119 		q = start[no];
120 		while (q->next != NULL) {
121 			q = q->next;
122 		}
123 		q->next = p;
124 	}
125 	p->key1 = key1;
126 	p->key2 = key2;
127 	p->key3 = key3;
128 	p->msg = XSTRDUP((const char FAR *)msg);
129 	p->next = NULL;
130 #ifdef MEMCHK
131 	callcount++;
132 #endif
133 }
134 
135 /*
136  * ���ꤷ�������ꥹ�Ȥ�����˥ǡ���(msg)����������
137  *
138  * key1, key2, key3 ����Ѥ������֤�õ��
139  * key1, key2, key3 �ξ���������¤�
140  */
list_insert(int no,int key1,int key2,int key3,const char * msg)141 void list_insert(int no, int key1, int key2, int key3, const char *msg)
142 {
143 	LIST	*p, *q;
144 
145 	if (no < 0 || no > (MAXLIST - 1)) {
146 		return ;
147 	}
148 
149 	if (start[no] == NULL) {
150 		list_add(no, key1, key2, key3, msg);
151 		return ;
152 	}
153 	p = start[no];
154 	while (key1 > p->key1 || (key1 == p->key1 && (key2 > p->key2 || (key2 == p->key2 && key3 >= p->key3)))) {
155 		p = p->next;
156 		if (p == NULL) {
157 			list_add(no, key1, key2, key3, msg);
158 			return ;
159 		}
160 	}
161 	q = (LIST *)xmalloc(sizeof(LIST));
162 	*q = *p;
163 	p->key1 = key1;
164 	p->key2 = key2;
165 	p->key3 = key3;
166 	p->msg = XSTRDUP((const char FAR *)msg);
167 	p->next = q;
168 #ifdef MEMCHK
169 	callcount++;
170 #endif
171 }
172 
173 /*
174  * ���ꤷ�������ꥹ�Ȥ�����(msg)����Ƭ������Ϥ���
175  *
176  * ������: (*outfunc)()
177  */
list_output(int no,void (* outfunc)(const char * fmt,...))178 void list_output(int no, void (*outfunc)(const char *fmt, ...))
179 {
180 	LIST	*p;
181 	int		msglen;
182 
183 	if (no < 0 || no > (MAXLIST - 1)) {
184 		return ;
185 	}
186 
187 	p = start[no];
188 	if (p == NULL) {
189 		return ;
190 	}
191 
192 	while (p != NULL) {
193 		msglen = FSTRLEN(p->msg) + 1;
194 		if (msglen > buflen) {
195 			free(bufptr);
196 			buflen = msglen;
197 			bufptr = xmalloc(buflen);
198 		}
199 		FSTRCPY((char FAR *)bufptr, p->msg);
200 #ifdef DEBUG
201 		(*outfunc)("p:%08x, msg:%08x, next:%08x, key1:%4d, key2:%4d, key3:%4d, msg:%s\n",
202 			p, p->msg, p->next, p->key1, p->key2, p->key3, bufptr);
203 #else
204 		(*outfunc)(bufptr);
205 #endif
206 		p = p->next;
207 	}
208 }
209