This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
114 lines (92 loc) · 2.02 KB
/
util.c
File metadata and controls
114 lines (92 loc) · 2.02 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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <inttypes.h>
#define grey "989f98"
#define green "a3be8c"
#define yellow "ebcb8b"
#define red "bf616a"
int8_t read_first_line(char *line, uint8_t length, const char *path)
{
FILE *file = fopen(path, "r");
if (file == NULL) {
return 1;
}
char ch;
uint8_t i = 0;
while ((ch = getc(file)) != '\n' && ch != EOF && i <= length) {
line[i] = ch;
i += 1;
} line[i] = '\0';
fclose(file);
return 0;
}
/*
* Reads the output configuration file. We stop when we reach a whitespace
* character or the end of the file. It also doubles as a ‘strcmp’
* implementation, checking for valid configuration.
*/
char read_config_output(FILE *file, char *line, uint8_t length)
{
char ch;
bool quote = false;
uint8_t i;
for (i = 0; i <= length; i += 1) {
ch = getc(file);
if (ch == '\n' || ch == EOF ||
(ch == ' ' || ch == '\t') && i != 0 && !quote)
{
break;
}
if (ch == '"')
quote = !quote;
line[i] = ch;
}
line[i] = '\0';
return ch;
}
char *read_file(const char *path)
{
FILE *file = fopen(path, "r");
if (file == NULL) {
return NULL;
}
char *line = NULL;
size_t len;
getline(&line, &len, file);
fclose(file);
return line;
}
/* char *formatstr(const char* format, ...) */
/* { */
/* va_list args; */
/* va_start(args, format); */
/* int length = vsnprintf(NULL, 0, format, args); */
/* va_end(args); */
/* va_start(args, format); */
/* char* str = malloc(length + 1); */
/* vsnprintf(str, length + 1, format, args); */
/* va_end(args); */
/* return str; */
/* } */
char *percentage_color(const unsigned int percentage)
{
if (percentage < 10)
return grey;
else if (percentage < 25)
return ""; // default colour
else if (percentage < 50)
return green;
else if (percentage < 75)
return yellow;
return red;
}
char *percentage_color_volume(const unsigned int percentage)
{
if (percentage == 0)
return grey; // grey
else if(percentage <= 100)
return ""; // default colour
return red; // red
}