4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 static const char *levels[] = {
52 int level_get(const char *level)
56 if (g_ascii_strcasecmp(level, "ALL") == 0 || strcmp(level, "*") == 0)
59 if (g_ascii_strcasecmp(level, "NEVER") == 0)
60 return MSGLEVEL_NEVER;
63 if (len == 0) return 0;
65 /* partial match allowed, as long as it's the only one that matches */
67 for (n = 0; levels[n] != NULL; n++) {
68 if (g_ascii_strncasecmp(levels[n], level, len) == 0) {
69 if ((int)strlen(levels[n]) == len) {
74 /* ambiguous - abort */
84 int level2bits(const char *level, int *errorp)
86 char *orig, *str, *ptr;
87 int ret, singlelevel, negative;
92 g_return_val_if_fail(level != NULL, 0);
97 orig = str = g_strdup(level);
100 for (ptr = str; ; str++) {
103 else if (*str != '\0')
106 negative = *ptr == '-';
107 if (*ptr == '-' || *ptr == '+') ptr++;
109 singlelevel = level_get(ptr);
110 if (singlelevel != 0) {
111 ret = !negative ? (ret | singlelevel) :
112 (ret & ~singlelevel);
113 } else if (errorp != NULL)
116 while (*str == ' ') str++;
117 if (*str == '\0') break;
126 char *bits2level(int bits)
135 if (bits == MSGLEVEL_ALL)
136 return g_strdup("ALL");
138 str = g_string_new(NULL);
139 if (bits & MSGLEVEL_NEVER)
140 g_string_append(str, "NEVER ");
142 for (n = 0; levels[n] != NULL; n++) {
143 if (bits & (1L << n))
144 g_string_append_printf(str, "%s ", levels[n]);
147 g_string_truncate(str, str->len-1);
150 g_string_free(str, FALSE);
155 int combine_level(int dest, const char *src)
157 char **list, **item, *itemname;
160 g_return_val_if_fail(src != NULL, dest);
162 list = g_strsplit(src, " ", -1);
163 for (item = list; *item != NULL; item++) {
164 itemname = *item + (**item == '+' || **item == '-' ? 1 : 0);
165 itemlevel = level_get(itemname);
167 if (strcmp(itemname, "NONE") == 0)
169 else if (**item == '-')
170 dest &= ~(itemlevel);