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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 static const char *levels[] = {
52 int level_get(const char *level)
56 if (g_strcasecmp(level, "ALL") == 0 || strcmp(level, "*") == 0)
59 if (g_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_strncasecmp(levels[n], level, len) == 0) {
69 if ((int)strlen(levels[n]) == len) {
74 /* ambiguous - abort */
84 int level2bits(const char *level)
86 char *orig, *str, *ptr;
87 int ret, singlelevel, negative;
89 g_return_val_if_fail(level != NULL, 0);
94 orig = str = g_strdup(level);
97 for (ptr = str; ; str++) {
100 else if (*str != '\0')
103 negative = *ptr == '-';
104 if (*ptr == '-' || *ptr == '+') ptr++;
106 singlelevel = level_get(ptr);
107 if (singlelevel != 0) {
108 ret = !negative ? (ret | singlelevel) :
109 (ret & ~singlelevel);
112 while (*str == ' ') str++;
113 if (*str == '\0') break;
122 char *bits2level(int bits)
131 if (bits == MSGLEVEL_ALL)
132 return g_strdup("ALL");
134 str = g_string_new(NULL);
135 if (bits & MSGLEVEL_NEVER)
136 g_string_append(str, "NEVER ");
138 for (n = 0; levels[n] != NULL; n++) {
139 if (bits & (1L << n))
140 g_string_sprintfa(str, "%s ", levels[n]);
143 g_string_truncate(str, str->len-1);
146 g_string_free(str, FALSE);
151 int combine_level(int dest, const char *src)
153 char **list, **item, *itemname;
156 g_return_val_if_fail(src != NULL, dest);
158 list = g_strsplit(src, " ", -1);
159 for (item = list; *item != NULL; item++) {
160 itemname = *item + (**item == '+' || **item == '-' ? 1 : 0);
162 itemlevel = level_get(itemname);
164 if (strcmp(itemname, "NONE") == 0)
166 else if (**item == '-')
167 dest &= ~(itemlevel);