-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.c
70 lines (57 loc) · 2.02 KB
/
utils.c
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
#include "utils.h"
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
void time_diff(struct timespec t1, struct timespec t2, struct timespec *diff) {
if (t2.tv_nsec < t1.tv_nsec) {
/* If nanoseconds in t1 are larger than nanoseconds in t2, it
means that something like the following happened:
t1.tv_sec = 1000 t1.tv_nsec = 100000
t2.tv_sec = 1001 t2.tv_nsec = 10
In this case, less than a second has passed but subtracting
the tv_sec parts will indicate that 1 second has passed. To
fix this problem, we subtract 1 second from the elapsed
tv_sec and add one second to the elapsed tv_nsec. See
below:
*/
diff->tv_sec += t2.tv_sec - t1.tv_sec - 1;
diff->tv_nsec += t2.tv_nsec - t1.tv_nsec + 1000000000;
} else {
diff->tv_sec += t2.tv_sec - t1.tv_sec;
diff->tv_nsec += t2.tv_nsec - t1.tv_nsec;
}
}
char *time_snprintf(char *buf, size_t n, struct timespec t1) {
double pct = (t1.tv_sec * 1000000000.0) + t1.tv_nsec / 1000000000.0;
snprintf(buf, n, "%f", pct);
return buf;
}
int match_regex(char *regmatch, char *matches[], int n_matches,
const char *to_match) {
/* "M" contains the matches found. */
regmatch_t m[n_matches];
regex_t regex;
if (regcomp(®ex, regmatch, REG_EXTENDED)) {
fprintf(stderr, "Could not compile regex: %s\n", regmatch);
return -1;
}
int nomatch = regexec(®ex, to_match, n_matches, m, 0);
if (nomatch == REG_NOMATCH) {
return 0;
}
int match_count = 0;
for (int i = 0; i < n_matches; i++) {
if (m[i].rm_so == -1) {
continue;
}
match_count++;
int match_len = m[i].rm_eo - m[i].rm_so;
matches[i] = malloc(match_len + 1); // make room for '\0'
int k = 0;
for (int j = m[i].rm_so; j < m[i].rm_eo; j++) {
matches[i][k++] = to_match[j];
}
matches[i][k] = '\0';
}
return match_count;
}