-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
138 lines (110 loc) · 2.96 KB
/
utils.h
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
131
132
133
134
135
136
137
138
#pragma once
#ifndef STRING_IMPLEMENTATION
#define STRING_IMPLEMENTATION
#endif
#include "dynamic_array/string.h"
bool read_file(const char* file_path, string_builder* sb);
char* decimal_to_bin(int n, unsigned int bus_size);
char* bin_to_hex(const char* bin, unsigned int bus_size);
char* extract_file_name(const char* file, const char* extension);
#ifdef UTILS_IMPLEMENTATION
#include <stdio.h>
#include <libgen.h>
char *strndup(const char *str, unsigned long long chars)
{
char *buffer;
int n;
buffer = (char *) malloc(chars +1);
if (buffer)
{
for (n = 0; ((n < chars) && (str[n] != 0)) ; n++) buffer[n] = str[n];
buffer[n] = 0;
}
return buffer;
}
char* extract_file_name(const char* file, const char* extension)
{
char *bname;
char *path;
char* filename;
string_view file_ext;
if (!file) {
return NULL;
}
if (!(path = strndup(file, strlen(file)))) {
return NULL;
}
if (!(bname = basename(path))) {
free(path);
return NULL;
}
file_ext = sv_from_cstr(bname);
string_view file_token = sv_split_c(&file_ext, '.');
file_ext = sv_split_c(&file_ext, '.');
if (file_ext.count > 0 && sv_equal_cstr(file_ext, extension)) {
if (!(filename = (char *)malloc(sizeof(char) * file_token.count + 1))) {
return NULL;
}
if (!strncpy(filename, file_token.data, file_token.count)) {
fprintf(stderr, "filename invalid");
free(filename);
return NULL;
}
filename[file_token.count] = '\0';
free(path);
return filename;
}
fprintf(stderr, "File must have .%s extension\n", extension);
return NULL;
}
bool read_file(const char* file_path, string_builder* sb)
{
bool result = true;
FILE *file = fopen(file_path, "rb");
if (file == NULL) {
if (!result) fprintf(stderr, "Could not read file %s: %s", file_path, strerror(errno));
if (file) fclose(file);
return result;
}
int ch;
while ((ch = fgetc(file)) != EOF) {
sb_add_c(sb, ch);
}
if (ferror(file)) {
if (!result) fprintf(stderr, "Could not read file %s: %s", file_path, strerror(errno));
if (file) fclose(file);
return result;
}
fclose(file);
return result;
}
char* decimal_to_bin(int n, unsigned int bus_size)
{
static char bin[64];
int i = 0, b;
for (b = bus_size - 1; b >= 0; b--) {
int bit = n >> b;
if (bit & 1) {
bin[i] = '1';
}
else {
bin[i] = '0';
}
i++;
}
bin[bus_size] = '\0';
return bin;
}
char* bin_to_hex(const char* bin, unsigned int bus_size)
{
char* buffer = (char*)malloc(sizeof(char)*5);
int num = 0;
for (int i = 0; i < bus_size; i++) {
int b = bin[i] == '1' ? 1 : 0;
num = (num << 1) | b;
}
snprintf(buffer, 5, "%.4X", num);
buffer[4] = '\0';
return buffer;
}
#endif // UTILS_IMPLEMENTATION