Strip off version numbers from dir name
[dockapps.git] / wmbiff / wmbiff / regulo.c
blob507cd192733394b92963fb2bb5a5e810fd4bb855
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <sys/types.h>
9 #include <regex.h>
10 #include <ctype.h>
12 #include "regulo.h"
13 #include "charutil.h"
15 #define min(a,b) ((a)<(b) ? (a) : (b))
17 /* callbacks specified by the calling function to extract
18 substrings to values. */
19 void regulo_atoi(void *dest_int, const char *source)
21 /* skip any leading non-digit */
22 while (*source != '\0' && !isdigit(*source))
23 source++;
25 *(int *) dest_int = atoi(source);
28 void regulo_strcpy(void *dest, const char *source)
30 strcpy((char *) dest, source);
33 void regulo_strcpy_tolower(void *dest, const char *source)
35 unsigned int i;
36 for (i = 0; i < strlen(source); i++) {
37 ((char *) dest)[i] = tolower(source[i]);
39 ((char *) dest)[i] = '\0';
42 void regulo_strcpy_skip1(void *dest, const char *source)
44 strcpy((char *) dest, source + 1);
47 #ifdef USE_GNU_REGEX
48 /* deprecated as unportable */
50 int
51 regulo_match(const char *regex,
52 const char *string, const struct regulo *instructions)
54 struct re_registers regs;
55 int ret;
56 int matchedchars;
57 int i;
58 memset(&regs, 0, sizeof(struct re_registers));
59 matchedchars = compile_and_match_regex(regex, string, &regs);
60 if (matchedchars <= 0)
61 return 0;
62 if (instructions == NULL)
63 return 1;
64 for (i = 0; instructions[i].match_handler != NULL; i++) {
65 char buf[255];
66 int j = instructions[i].match_index;
67 int len = min(254, regs.end[j] - regs.start[j]);
68 if (regs.start[j] >= 0) {
69 strncpy(buf, string + regs.start[j], len);
70 buf[len] = '\0';
71 instructions[i].match_handler(instructions[i].destination,
72 buf);
75 ret = regs.end[0];
76 free(regs.end); // added 3 jul 02, appeasing valgrind
77 free(regs.start); // added 3 jul 02, appeasing valgrind
78 return ret;
81 #else
82 /* favored */
84 int compile_and_match_regex_posix(const char *regex, const char *str, /*@out@ */
85 regmatch_t * regs, size_t regs_len)
87 regex_t reg;
88 int errcode;
89 if ((errcode = regcomp(&reg, regex, REG_EXTENDED)) != 0) {
90 char errbuf[256];
91 regerror(errcode, &reg, errbuf, 256);
92 fprintf(stderr, "error in compiling regular expression: %s\n",
93 errbuf);
94 return -1;
97 errcode = regexec(&reg, str, regs_len, regs, 0);
98 regfree(&reg);
99 if (errcode == 0)
100 return 1;
101 else
102 return 0;
107 regulo_match(const char *regex,
108 const char *string, const struct regulo *instructions)
110 regmatch_t regs[20];
111 int ret;
112 int matchedchars;
113 int i;
114 matchedchars = compile_and_match_regex_posix(regex, string, regs, 20);
115 if (matchedchars <= 0)
116 return 0;
117 if (instructions == NULL)
118 return 1;
119 for (i = 0; instructions[i].match_handler != NULL; i++) {
120 char buf[255];
121 int j = instructions[i].match_index;
122 int len = min(254, regs[j].rm_eo - regs[j].rm_so);
123 if (regs[j].rm_so >= 0) {
124 strncpy(buf, string + regs[j].rm_so, len);
125 buf[len] = '\0';
126 instructions[i].match_handler(instructions[i].destination,
127 buf);
130 ret = regs[0].rm_eo;
131 return ret;
134 #endif