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