-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
98 lines (73 loc) · 1.92 KB
/
util.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
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
/*
* path: /home/klassiker/.local/share/repos/cinfo/util.c
* author: klassiker [mrdotx]
* github: https://github.com/mrdotx/cinfo
* date: 2022-08-27T18:11:48+0200
*/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "util.h"
void split_string(char* input, char** output, char* delimiter) {
char *temp;
temp = strtok(input, delimiter);
for (int i = 0; temp != NULL; i++) {
output[i] = temp;
temp = strtok(NULL, " ");
}
}
const char *remove_char(char *string, const char *remove) {
int i = 0, j;
while (i < strlen(string)) {
if (string[i] == *remove) {
for (j = i; j < strlen(string); j++)
string[j] = string[j + 1];
}
else i++;
}
return string;
}
const int update_header_len(const char *line, int header_len) {
if (line == NULL) {
header_len = 0;
} else {
header_len += strlen(line);
}
return header_len;
}
const int update_line_len(const char *line, int line_len) {
if (line == NULL) {
line_len = 0;
} else if (line_len < strlen(line)) {
line_len = strlen(line);
}
return line_len;
}
const char *set_spacer(const char *character, int length) {
int i;
static char spacer[256];
spacer[0] = '\0';
for (i = 0; i < length; i++) {
strcat(spacer, character);
}
return spacer;
}
const double get_execution_time(void *print()) {
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
print();
clock_gettime(CLOCK_REALTIME, &end);
double time_spend = (end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec) / 1000000000.0;
return time_spend;
}
const char *remove_file(const char *file) {
static char string[2];
if (0 == remove(file)) {
sprintf(string, "x");
} else {
sprintf(string, "?");
}
return string;
}