Diffstat (limited to 'gammu/emb/gammu/depend/nokia/dct3trac/wmx-list.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/gammu/depend/nokia/dct3trac/wmx-list.c | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/gammu/emb/gammu/depend/nokia/dct3trac/wmx-list.c b/gammu/emb/gammu/depend/nokia/dct3trac/wmx-list.c new file mode 100644 index 0000000..9af4b48 --- a/dev/null +++ b/gammu/emb/gammu/depend/nokia/dct3trac/wmx-list.c | |||
@@ -0,0 +1,137 @@ | |||
1 | /** | ||
2 | * Decode trace type using text file | ||
3 | * wumpus 2003 -- www.blacksphere.tk | ||
4 | */ | ||
5 | |||
6 | #include <stdio.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <string.h> | ||
9 | #include <signal.h> | ||
10 | |||
11 | #include "wmx-util.h" | ||
12 | #include "wmx-list.h" | ||
13 | |||
14 | static void wmx_tracelist_init(struct wmx_tracelist *tl) | ||
15 | { | ||
16 | tl->entries = 0; | ||
17 | tl->max = 10; | ||
18 | tl->records = malloc(tl->max * sizeof(struct wmx_tracetype)); | ||
19 | } | ||
20 | |||
21 | static void wmx_tracelist_add(struct wmx_tracelist *tl, int type, char *desc) | ||
22 | { | ||
23 | /* Possibly expand list */ | ||
24 | if(tl->entries == tl->max) { | ||
25 | tl->max *= 2; | ||
26 | tl->records = realloc(tl->records, tl->max * sizeof(struct wmx_tracetype)); | ||
27 | } | ||
28 | /* Add record */ | ||
29 | tl->records[tl->entries].type = type; | ||
30 | tl->records[tl->entries].desc = strdup(desc); | ||
31 | /* Increase number of entries */ | ||
32 | tl->entries++; | ||
33 | //printf("%04x %04x\n", tl->entries, tl->max); | ||
34 | } | ||
35 | |||
36 | static void wmx_tracelist_free(struct wmx_tracelist *tl) | ||
37 | { | ||
38 | int x; | ||
39 | |||
40 | for(x=0; x<tl->entries; x++) free(tl->records[x].desc); | ||
41 | free(tl->records); | ||
42 | tl->entries = tl->max = 0; | ||
43 | tl->records = NULL; | ||
44 | } | ||
45 | |||
46 | /** Comparision function for bsearch */ | ||
47 | static int compar(const void *a, const void *b) | ||
48 | { | ||
49 | int typea = ((struct wmx_tracetype*)a)->type; | ||
50 | int typeb = ((struct wmx_tracetype*)b)->type; | ||
51 | if(typea < typeb) return -1; | ||
52 | if(typea > typeb) return 1; | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Look up debug trace type | ||
58 | */ | ||
59 | static struct wmx_tracetype *wmx_tracelist_search(struct wmx_tracelist *tl, int type) | ||
60 | { | ||
61 | /* | ||
62 | int x; | ||
63 | for(x=0; x<tl->entries; x++) | ||
64 | if(tl->records[x].type == type) | ||
65 | return &tl->records[x]; | ||
66 | return NULL; | ||
67 | */ | ||
68 | |||
69 | struct wmx_tracetype key; | ||
70 | |||
71 | key.type = type; | ||
72 | return bsearch(&key, tl->records, tl->entries, sizeof(struct wmx_tracetype), &compar); | ||
73 | } | ||
74 | |||
75 | struct wmx_tracestruct *wmx_tracestruct_load(char *listfile) | ||
76 | { | ||
77 | char data[256]; | ||
78 | FILE *f; | ||
79 | struct wmx_tracestruct *tl; | ||
80 | int tpe; | ||
81 | |||
82 | f = fopen(listfile, "r"); | ||
83 | |||
84 | if(f == NULL) return NULL; | ||
85 | printf("Loading\n"); | ||
86 | |||
87 | tl = malloc(sizeof(struct wmx_tracestruct)); | ||
88 | wmx_tracelist_init(&tl->minors); | ||
89 | wmx_tracelist_init(&tl->majors); | ||
90 | |||
91 | while(fgets(data, sizeof(data), f)) { | ||
92 | int l = strlen(data); | ||
93 | if(l > 0 && data[l-1]=='\n') l--; | ||
94 | data[l] = 0; | ||
95 | if(l < 6) | ||
96 | /* Empty/invalid line */ | ||
97 | continue; | ||
98 | if(data[2] == 'X' && data[3] == 'X') { | ||
99 | sscanf(data, "%02X", &tpe); | ||
100 | wmx_tracelist_add(&tl->majors, tpe, &data[5]); | ||
101 | } else { | ||
102 | sscanf(data, "%04X", &tpe); | ||
103 | wmx_tracelist_add(&tl->minors, tpe, &data[5]); | ||
104 | } | ||
105 | } | ||
106 | fclose(f); | ||
107 | return tl; | ||
108 | } | ||
109 | |||
110 | |||
111 | struct wmx_tracetype *wmx_tracestruct_querymajor(struct wmx_tracestruct * ts, int type) { | ||
112 | return wmx_tracelist_search(&ts->majors, type); | ||
113 | } | ||
114 | |||
115 | struct wmx_tracetype *wmx_tracestruct_queryminor(struct wmx_tracestruct * ts, int type) { | ||
116 | return wmx_tracelist_search(&ts->minors, type); | ||
117 | } | ||
118 | |||
119 | void wmx_tracestruct_free(struct wmx_tracestruct * ts) { | ||
120 | wmx_tracelist_free(&ts->minors); | ||
121 | wmx_tracelist_free(&ts->majors); | ||
122 | free(ts); | ||
123 | } | ||
124 | |||
125 | /* | ||
126 | main() { | ||
127 | int x; | ||
128 | struct wmx_tracestruct * tstruct= wmx_tracestruct_load( | ||
129 | "/home/orion/projects/blacksphere/tables/nhm5_587.txt"); | ||
130 | |||
131 | printf("%s\n", wmx_tracestruct_queryminor(tstruct, 0x1802)->desc); | ||
132 | } | ||
133 | */ | ||
134 | |||
135 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
136 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
137 | */ | ||