File: tab_categories.c

package info (click to toggle)
gpr 0.12deb1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,568 kB
  • ctags: 250
  • sloc: sh: 6,138; ansic: 3,684; makefile: 86; sed: 16
file content (326 lines) | stat: -rw-r--r-- 8,293 bytes parent folder | download | duplicates (4)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libintl.h>
#define _(String) gettext (String)

#include <gnome-xml/parser.h>

#include "tab_categories.h"

/*-------------------------------------------------------------------
  Free all the elements of a tTab structure.
-------------------------------------------------------------------*/
void tab_free (ptrTab tab) {
  int idx;

  if (! tab) return;

  if (tab->title) free (tab->title);

  idx = 0;
  while (tab->patterns[idx])
    free (tab->patterns[idx++]);

  idx = 0;
  while (tab->regexp[idx])
    free (tab->regexp[idx++]);

  idx = 0;
  while (tab->strings[idx])
    free (tab->strings[idx++]);
}
    
/*-------------------------------------------------------------------
  Free a list of tTab structures, then free the list itself.
-------------------------------------------------------------------*/
void tab_free_all (ptrTab * tabs) {
  int idx;

  if (! tabs) return;

  idx = 0;
  while (tabs[idx]) {
    free (tabs[idx++]);
  }
  free (tabs);
}

/*-------------------------------------------------------------------
  Print out data from a tTab struct to stdout.
-------------------------------------------------------------------*/
void tab_print (ptrTab tab) {
  int idx;

  if (! tab) return;

  if (tab->title) printf (_("title = %s\n"), tab->title);

  idx = 0;
  while (tab->patterns[idx])
    printf (_("pattern = %s\n"), tab->patterns[idx++]);

  idx = 0;
  while (tab->regexp[idx]) {
    printf (_("regexp = %d\n"), (tab->regexp[idx])->re_nsub);
    idx++;
  }

  idx = 0;
  while (tab->strings[idx])
    printf (_("string = %s\n"), tab->strings[idx++]);
}
    
/*-------------------------------------------------------------------
  Build a tTab data structure from the parsed xml data.
-------------------------------------------------------------------*/
ptrTab tab_build_struct(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
  ptrTab ret = NULL;
  int pidx = 0;
  int sidx = 0;

  /* allocate the struct */
  ret = (ptrTab) malloc(sizeof(tTab));
  if (ret == NULL) {
    fprintf(stderr,_("out of memory\n"));
    return(NULL);
  }
  memset(ret, 0, sizeof(tTab));

  if ((!strcmp(cur->name, "Tab")) && (cur->ns == ns)) {

    ret->title = xmlGetProp(cur, "title");
    if (ret->title == NULL) {
      tab_free (ret);
      return (NULL);
    }

    /* We don't care what the top level element name is */
    cur = cur->childs;
    while (cur != NULL) {
      if ((!strcmp(cur->name, "PatternMatch")) && (cur->ns == ns)) {
	/* Allocate space for another pattern string */
	ret->patterns = (char **) realloc (ret->patterns, 
					   sizeof (char *) * (pidx + 1));
	if (ret->patterns)
	  ret->patterns[pidx] = xmlGetProp (cur, "text");
	else {
	  tab_free (ret);
	  return NULL;
	}

	/* Allocate space for another compiled regexp */
	ret->regexp = (regex_t **) realloc (ret->regexp,
					    sizeof (regex_t *) * (pidx + 1));
	if (ret->regexp) {
	  ret->regexp[pidx] = (regex_t *) malloc (sizeof (regex_t));
	  regcomp (ret->regexp[pidx], ret->patterns[pidx], REG_NOSUB);
	}

	pidx++;
      }
      if ((!strcmp(cur->name, "StringMatch")) && (cur->ns == ns)) {
	ret->strings = (char **) realloc (ret->strings,
					  sizeof (char *) * (sidx + 1));
	if (ret->strings)
	  ret->strings[sidx++] = xmlGetProp (cur, "text");
	else {
	  tab_free (ret);
	  return NULL;
	}
      }
      cur = cur->next;
    }
    ret->patterns = (char **) realloc (ret->patterns, 
				       sizeof (char *) * (pidx + 1));
    ret->patterns[pidx] = NULL;
      
    ret->regexp = (regex_t **) realloc (ret->regexp, 
					sizeof (regex_t *) * (pidx + 1));
    ret->regexp[pidx] = NULL;
      
    ret->strings = (char **) realloc (ret->strings, 
				      sizeof (char *) * (sidx + 1));
    ret->strings[sidx] = NULL;
      
  }

  return(ret);
}

/*-------------------------------------------------------------------
  Return 1 if file exists (as regular file), or 0 otherwise.
-------------------------------------------------------------------*/
static int file_exists (char *filename) {
  struct stat s;

  if (! filename) return 0;

  if (stat(filename, &s) != 0)
    return 0;
  else {
    if (S_ISREG (s.st_mode))
      return 1;
    else
      return 0;
  }
}

/*-------------------------------------------------------------------
  Parse the xml file containing tab information.  Use this to build 
  a list of tTab structs that we'll use for matching later on.
-------------------------------------------------------------------*/
ptrTab * tab_parse_xml_file(char *filename) {
    xmlDocPtr doc;
    xmlNsPtr ns;
    xmlNodePtr cur;
    ptrTab tab;
    ptrTab *tabs = NULL;
    int tidx;

    /* Check for existence of file. */
    if (! file_exists (filename)) return NULL;

    /* Build an XML tree from a the file */
    doc = xmlParseFile(filename);
    if (doc == NULL) return(NULL);

    /* Check the document is of the right kind */
    cur = doc->root;
    if (cur == NULL) {
        fprintf(stderr,_("empty document\n"));
	xmlFreeDoc(doc);
	return NULL;
    }

    ns = xmlSearchNsByHref(doc, cur, "http://sourceforge.net/projects/gnulpr");
    if ((ns == NULL) || (strcmp(cur->name, "Helping"))) {
        fprintf(stderr,
	        _("Document of the wrong type\n"));
	xmlFreeDoc (doc);
	return NULL;
    }


    /* Now, walk the tree. */

    /* First level we expect just Tabs */
    cur = cur->childs;
    if ((strcmp(cur->name, "Tabs")) || (cur->ns != ns)) {
        fprintf(stderr,
		_("document of the wrong type, Tabs expected, found (%s)\n"),
		cur->name);
	xmlFreeDoc (doc);
	return NULL;
    }

    /* Second level is a list of Tab */
    cur = cur->childs;
    tidx = 0;
    while (cur != NULL) {
        if ((!strcmp(cur->name, "Tab")) && (cur->ns == ns)) {
	    tab = tab_build_struct(doc, ns, cur);

	    tabs = (ptrTab *) realloc (tabs, sizeof (ptrTab) * (tidx+1));
	    if (tabs) 
	      tabs[tidx++] = tab;
	    else {
	      /* free stuff here... */
	      return NULL;
	    }
	}
	cur = cur->next;
    }
    tabs = (ptrTab *) realloc (tabs, sizeof (ptrTab) * (tidx+1));
    tabs[tidx] = NULL;

    xmlFreeDoc (doc);
    return tabs;
}

/*-------------------------------------------------------------------
  Match the given description string to the list of strings and
  regexp patterns.  If there's a match, return the tab's title
  string.
-------------------------------------------------------------------*/
char * tab_match (char *desc, ptrTab *tablist) {
  int idx;
  int tidx = 0;
  char *retstr = NULL;
  int failed = REG_NOMATCH;
  ptrTab tab = NULL;

  if (! tablist) return NULL;

  tab = tablist[tidx++];
  while (tab) {
    /* First check simple string compares */
    idx = 0;
    while (tab->strings[idx]) {
      if (! strcmp (tab->strings[idx], desc)) {
	retstr = strdup (tab->title);
	return retstr;
      }
      idx++;
    }

    /* Last, check expensive regular expression matches */
    idx = 0;
    while (tab->regexp[idx]) {
      failed = regexec (tab->regexp[idx], desc, 0, NULL, 0) ;
      if (! failed) {
	retstr = strdup (tab->title);
	return retstr;
      }
      idx++;
    }

    tab = tablist[tidx++];
  }
  return NULL;
}

/*-------------------------------------------------------------------
  Used for testing only.
-------------------------------------------------------------------*/
int testmain(int argc, char **argv) {
  int i;
  ptrTab *cur;
  int cidx;
  char teststr[100];

  for (i = 1; i < argc ; i++) {
    cur = tab_parse_xml_file (argv[i]);

    if (cur) {
      printf ("\n\n%02d **************************\n", i);
      cidx = 0;
      while (cur[cidx]) {
	printf ("=============================\n");
	tab_print (cur[cidx++]);
      }
    }
    /* Some test matches */

    strcpy (teststr, "Watermark Stuff");
    printf (_("[%s] matches [%s]\n"), teststr, tab_match (teststr, cur));

    strcpy (teststr, "Foobar");
    printf (_("[%s] matches [%s]\n"), teststr, tab_match (teststr, cur));

    strcpy (teststr, "Water Mark String");
    printf (_("[%s] matches [%s]\n"), teststr, tab_match (teststr, cur));

    strcpy (teststr, "WaterMark");
    printf (_("[%s] matches [%s]\n"), teststr, tab_match (teststr, cur));


    tab_free_all (cur);
  }

    
  return(0);
}