Update log2lines to ver. 2.2. Jan Roeloffzen, bug #4342.
[reactos.git] / reactos / tools / log2lines / list.c
1 /*
2 * ReactOS log2lines
3 * Written by Jan Roeloffzen
4 *
5 * - List handling
6 */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #include "config.h"
13 #include "compat.h"
14 #include "list.h"
15 #include "util.h"
16 #include "options.h"
17
18 PLIST_MEMBER
19 entry_lookup(PLIST list, char *name)
20 {
21 PLIST_MEMBER pprev = NULL;
22 PLIST_MEMBER pnext;
23
24 if (!name || !name[0])
25 return NULL;
26
27 pnext = list->phead;
28 while (pnext != NULL)
29 {
30 if (PATHCMP(name, pnext->name) == 0)
31 {
32 if (pprev)
33 { // move to head for faster lookup next time
34 pprev->pnext = pnext->pnext;
35 pnext->pnext = list->phead;
36 list->phead = pnext;
37 }
38 return pnext;
39 }
40 pprev = pnext;
41 pnext = pnext->pnext;
42 }
43 return NULL;
44 }
45
46 PLIST_MEMBER
47 entry_delete(PLIST_MEMBER pentry)
48 {
49 if (!pentry)
50 return NULL;
51 if (pentry->buf)
52 free(pentry->buf);
53 free(pentry);
54 return NULL;
55 }
56
57 PLIST_MEMBER
58 entry_insert(PLIST list, PLIST_MEMBER pentry)
59 {
60 if (!pentry)
61 return NULL;
62
63 pentry->pnext = list->phead;
64 list->phead = pentry;
65 if (!list->ptail)
66 list->ptail = pentry;
67 return pentry;
68 }
69
70 void list_clear(PLIST list)
71 {
72 PLIST_MEMBER pentry = list->phead;
73 PLIST_MEMBER pnext;
74 while (pentry)
75 {
76 pnext = pentry->pnext;
77 entry_delete(pentry);
78 pentry = pnext;
79 }
80 list->phead = list->ptail = NULL;
81 }
82
83 #if 0
84 LIST_MEMBER *
85 entry_remove(LIST *list, LIST_MEMBER *pentry)
86 {
87 LIST_MEMBER *pprev = NULL, *p = NULL;
88
89 if (!pentry)
90 return NULL;
91
92 if (pentry == list->phead)
93 {
94 list->phead = pentry->pnext;
95 p = pentry;
96 }
97 else
98 {
99 pprev = list->phead;
100 while (pprev->pnext)
101 {
102 if (pprev->pnext == pentry)
103 {
104 pprev->pnext = pentry->pnext;
105 p = pentry;
106 break;
107 }
108 pprev = pprev->pnext;
109 }
110 }
111 if (pentry == list->ptail)
112 list->ptail = pprev;
113
114 return p;
115 }
116 #endif
117
118 PLIST_MEMBER
119 cache_entry_create(char *Line)
120 {
121 PLIST_MEMBER pentry;
122 char *s = NULL;
123 int l;
124
125 if (!Line)
126 return NULL;
127
128 pentry = malloc(sizeof(LIST_MEMBER));
129 if (!pentry)
130 return NULL;
131
132 l = strlen(Line);
133 pentry->buf = s = malloc(l + 1);
134 if (!s)
135 {
136 l2l_dbg(1, "Alloc entry failed\n");
137 return entry_delete(pentry);
138 }
139
140 strcpy(s, Line);
141 if (s[l] == '\n')
142 s[l] = '\0';
143
144 pentry->name = s;
145 s = strchr(s, '|');
146 if (!s)
147 {
148 l2l_dbg(1, "Name field missing\n");
149 return entry_delete(pentry);
150 }
151 *s++ = '\0';
152
153 pentry->path = s;
154 s = strchr(s, '|');
155 if (!s)
156 {
157 l2l_dbg(1, "Path field missing\n");
158 return entry_delete(pentry);
159 }
160 *s++ = '\0';
161 if (1 != sscanf(s, "%x", (unsigned int *)(&pentry->ImageBase)))
162 {
163 l2l_dbg(1, "ImageBase field missing\n");
164 return entry_delete(pentry);
165 }
166 pentry->RelBase = INVALID_BASE;
167 pentry->Size = 0;
168 return pentry;
169 }
170
171
172 PLIST_MEMBER
173 sources_entry_create(PLIST list, char *path, char *prefix)
174 {
175 PLIST_MEMBER pentry;
176 char *s = NULL;
177 int l;
178
179 if (!path)
180 return NULL;
181 if (!prefix)
182 prefix = "";
183
184 pentry = malloc(sizeof(LIST_MEMBER));
185 if (!pentry)
186 return NULL;
187
188 l = strlen(path) + strlen(prefix);
189 pentry->buf = s = malloc(l + 1);
190 if (!s)
191 {
192 l2l_dbg(1, "Alloc entry failed\n");
193 return entry_delete(pentry);
194 }
195
196 strcpy(s, prefix);
197 strcat(s, path);
198 if (s[l] == '\n')
199 s[l] = '\0';
200
201 pentry->name = s;
202 if (list)
203 {
204 if (entry_lookup(list, pentry->name))
205 {
206 l2l_dbg(1, "Entry %s exists\n", pentry->name);
207 pentry = entry_delete(pentry);
208 }
209 else
210 {
211 l2l_dbg(1, "Inserting entry %s\n", pentry->name);
212 entry_insert(list, pentry);
213 }
214 }
215
216 return pentry;
217 }
218
219 /* EOF */