Skip to content

Commit ddc659b

Browse files
committed
fix: reuse functions for c
1 parent 5f0217a commit ddc659b

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

c/day03.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "helpers.h"
12
#include <ctype.h>
23
#include <stdbool.h>
34
#include <stdio.h>
@@ -156,24 +157,6 @@ static int parse_with_rules(const char *message) {
156157
return count;
157158
}
158159

159-
static char *read_file(const char *file_path) {
160-
FILE *file = fopen(file_path, "r");
161-
if (!file) {
162-
perror("Unable to open file");
163-
exit(EXIT_FAILURE);
164-
}
165-
166-
fseek(file, 0, SEEK_END);
167-
long file_size = ftell(file);
168-
rewind(file);
169-
170-
char *content = malloc(file_size + 1);
171-
fread(content, 1, file_size, file);
172-
content[file_size] = '\0';
173-
fclose(file);
174-
return content;
175-
}
176-
177160
int main() {
178161
char *message = read_file("crates/day03/input.txt");
179162

c/day05.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "helpers.h"
12
#include <stdbool.h>
23
#include <stdio.h>
34
#include <stdlib.h>
@@ -209,31 +210,6 @@ static int check_and_sort_page_order(const struct PrintQueueChecker *checker) {
209210
return sum;
210211
}
211212

212-
static char *read_file(const char *filename) {
213-
FILE *file = fopen(filename, "r");
214-
if (file == NULL) {
215-
fprintf(stderr, "Unable to open file: %s\n", filename);
216-
exit(1);
217-
}
218-
219-
fseek(file, 0, SEEK_END);
220-
long file_size = ftell(file);
221-
rewind(file);
222-
223-
char *buffer = (char *)malloc(file_size + 1);
224-
if (buffer == NULL) {
225-
fclose(file);
226-
fprintf(stderr, "Memory allocation failed\n");
227-
exit(1);
228-
}
229-
230-
size_t read_size = fread(buffer, 1, file_size, file);
231-
buffer[read_size] = '\0';
232-
233-
fclose(file);
234-
return buffer;
235-
}
236-
237213
static void free_print_queue_checker(struct PrintQueueChecker *checker) {
238214
for (int i = 0; i < checker->rule_count; i++) {
239215
free(checker->rules[i].values);

c/helpers.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdbool.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
static char *read_file(const char *filename) {
7+
FILE *file = fopen(filename, "r");
8+
if (file == NULL) {
9+
fprintf(stderr, "Unable to open file: %s\n", filename);
10+
exit(1);
11+
}
12+
13+
fseek(file, 0, SEEK_END);
14+
long file_size = ftell(file);
15+
rewind(file);
16+
17+
char *buffer = (char *)malloc(file_size + 1);
18+
if (buffer == NULL) {
19+
fclose(file);
20+
fprintf(stderr, "Memory allocation failed\n");
21+
exit(1);
22+
}
23+
24+
size_t read_size = fread(buffer, 1, file_size, file);
25+
buffer[read_size] = '\0';
26+
27+
fclose(file);
28+
return buffer;
29+
}

0 commit comments

Comments
 (0)