-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
130 lines (117 loc) · 3.03 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
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
#define _XOPEN_SOURCE
#define _BSD_SOURCE
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <ctype.h>
#include "utils.h"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_RESET "\x1b[0m"
// #define LOG 1
// #define LOG_ERR 1
// #define LOG_INFO 1
/* removes newline characters from the input string.
* Shifts characters over and shortens the length of
* the string by the number of newline characters.
*/
char* trim_newline(char *str) {
int length = strlen(str);
int current = 0;
for (int i = 0; i < length; ++i) {
if (!(str[i] == '\r' || str[i] == '\n')) {
str[current++] = str[i];
}
}
// Write new null terminator
str[current] = '\0';
return str;
}
/* removes space characters from the input string.
* Shifts characters over and shortens the length of
* the string by the number of space characters.
*/
char* trim_whitespace(char *str)
{
int length = strlen(str);
int current = 0;
for (int i = 0; i < length; ++i) {
if (!isspace(str[i])) {
str[current++] = str[i];
}
}
// Write new null terminator
str[current] = '\0';
return str;
}
/* removes parenthesis characters from the input string.
* Shifts characters over and shortens the length of
* the string by the number of parenthesis characters.
*/
char* trim_parenthesis(char *str) {
int length = strlen(str);
int current = 0;
for (int i = 0; i < length; ++i) {
if (!(str[i] == '(' || str[i] == ')')) {
str[current++] = str[i];
}
}
// Write new null terminator
str[current] = '\0';
return str;
}
char* trim_quotes(char *str) {
int length = strlen(str);
int current = 0;
for (int i = 0; i < length; ++i) {
if (str[i] != '\"') {
str[current++] = str[i];
}
}
// Write new null terminator
str[current] = '\0';
return str;
}
/* The following three functions will show output on the terminal
* based off whether the corresponding level is defined.
* To see log output, define LOG.
* To see error output, define LOG_ERR.
* To see info output, define LOG_INFO
*/
void cs165_log(FILE* out, const char *format, ...) {
#ifdef LOG
va_list v;
va_start(v, format);
vfprintf(out, format, v);
va_end(v);
#else
(void) out;
(void) format;
#endif
}
void log_err(const char *format, ...) {
#ifdef LOG_ERR
va_list v;
va_start(v, format);
fprintf(stderr, ANSI_COLOR_RED);
vfprintf(stderr, format, v);
fprintf(stderr, ANSI_COLOR_RESET);
va_end(v);
#else
(void) format;
#endif
}
void log_info(const char *format, ...) {
#ifdef LOG_INFO
va_list v;
va_start(v, format);
fprintf(stdout, ANSI_COLOR_GREEN);
vfprintf(stdout, format, v);
fprintf(stdout, ANSI_COLOR_RESET);
fflush(stdout);
va_end(v);
#else
(void) format;
#endif
}