forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogmask.cpp
More file actions
148 lines (132 loc) · 2.82 KB
/
logmask.cpp
File metadata and controls
148 lines (132 loc) · 2.82 KB
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
/**
* @file logmask.cpp
*
* Functions to convert between a string and a severity mask.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "logmask.h"
#include <cstdio> /* snprintf() */
#include <cstdlib> /* strtoul() */
#include "unc_ctype.h"
/**
* Convert a logmask into a string
*
* @param mask the mask to convert
* @param buf the buffer to hold the string
* @param size the size of the buffer
* @return buf (pass through)
*/
char *logmask_to_str(const log_mask_t& mask, char *buf, int size)
{
int last_sev = -1;
bool is_range = false;
int sev;
int len = 0;
if ((buf == NULL) || (size <= 0))
{
return(buf);
}
for (sev = 0; sev < 256; sev++)
{
if (logmask_test(mask, sev))
{
if (last_sev == -1)
{
len += snprintf(&buf[len], size - len, "%d,", sev);
}
else
{
is_range = true;
}
last_sev = sev;
}
else
{
if (is_range)
{
buf[len - 1] = '-'; /* change last comma to a dash */
len += snprintf(&buf[len], size - len, "%d,", last_sev);
is_range = false;
}
last_sev = -1;
}
}
/* handle a range that ends on the last bit */
if (is_range && (last_sev != -1))
{
buf[len - 1] = '-'; /* change last comma to a dash */
len += snprintf(&buf[len], size - len, "%d", last_sev);
}
else
{
/* Eat the last comma */
if (len > 0)
{
len--;
}
}
buf[len] = 0;
return(buf);
}
/**
* Parses a string into a log severity
*
* @param str The string to parse
* @param mask The mask to populate
*/
void logmask_from_string(const char *str, log_mask_t& mask)
{
char *ptmp;
bool was_dash = false;
int last_level = -1;
int level;
int idx;
if (str == NULL)
{
return;
}
/* Start with a clean mask */
logmask_set_all(mask, false);
/* If the first character is 'A', set all sevs */
if (unc_toupper(*str) == 'A')
{
logmask_set_all(mask, true);
str++;
}
while (*str != 0)
{
if (unc_isspace(*str))
{
str++;
continue;
}
if (unc_isdigit(*str))
{
level = strtoul(str, &ptmp, 10);
str = ptmp;
logmask_set_sev(mask, level, true);
if (was_dash)
{
for (idx = last_level + 1; idx < level; idx++)
{
logmask_set_sev(mask, idx, true);
}
was_dash = false;
}
last_level = level;
}
else if (*str == '-')
{
was_dash = true;
str++;
}
else /* probably a comma */
{
last_level = -1;
was_dash = false;
str++;
}
}
}