-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathutil.c
More file actions
69 lines (53 loc) · 1.84 KB
/
util.c
File metadata and controls
69 lines (53 loc) · 1.84 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
// Copyright (c) 2018 Electric Power Research Institute, Inc.
// author: Mark Slicker <mark.slicker@gmail.com>
/** @defgroup util Utility
Provides base layer idiomatic C macros and functions.
@{
*/
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#define min(x, y) (((x) < (y))? x : y)
#define max(x, y) (((x) < (y))? y : x)
#define ok(x) if (!(x)) return NULL
#define ok_v(x, value) if (!(x)) return value
#define streq(a,b) (strcmp (a, b) == 0)
#define in_range(x, a, b) ((x) >= a && (x) <= b)
#define alpha(c) (in_range (c, 'a', 'z') || in_range (c, 'A', 'Z'))
#define digit(c) in_range (c, '0', '9')
#define ws(c) (c == '\n' || c == ' ' || c == '\t' || c == '\r')
#define only(x) (*trim (x) == '\0')
#define struct_field(type, any, name) ((type *)any)->name
#define type_alloc(type) calloc (1, sizeof (type))
#define number_q(x, data) (digit (*(data))? number (x, data) : data)
int bit_count (uint32_t x);
int string_index (const char *s, const char * const *ss, int n);
char *trim (char *data);
char *to_lower (char *s);
char *number (int *x, char *data);
char *number64 (uint64_t *x, char *data);
/** @} */
#ifndef HEADER_ONLY
int bit_count (uint32_t x) { int n = 0;
while (x) x >>= 1, n++; return n;
}
int string_index (const char *s, const char * const *ss, int n) { int i;
for (i = 0; i < n; i++) if (streq (s, ss[i])) break; return i;
}
char *trim (char *data) { while (ws (*data)) data++; return data; }
char *to_lower (char *s) { char *t = s;
while (*t) *t = tolower (*t), t++; return s;
}
char *number (int *x, char *data) {
int y = 0, n = 0;
while (digit (*data))
y = y * 10 + (*data++ - '0'), n++;
return n > 0? *x = y, data : NULL;
}
char *number64 (uint64_t *x, char *data) {
uint64_t y = 0; int n = 0;
while (digit (*data))
y = y * 10 + (*data++ - '0'), n++;
return n > 0? *x = y, data : NULL;
}
#endif