-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24306fc
commit d33761b
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "../../utils/c/input.h" | ||
|
||
int main(int argc, char **argv) { | ||
input_t input = read_input(); | ||
if (input == NULL) { | ||
return EXIT_FAILURE; | ||
} | ||
|
||
printf("%s\n", input); | ||
free(input); | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <libgen.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "input.h" | ||
|
||
input_t read_input_impl(char *main_file) { | ||
char *directory = dirname(main_file); | ||
|
||
size_t directory_length = strlen(directory); | ||
|
||
// Room for the directory, a slash, input.txt and a null byte | ||
size_t path_length = path_length + 1 + 9 + 1; | ||
char *input_path = (char *)malloc(path_length); | ||
if (input_path == NULL) { | ||
return NULL; | ||
} | ||
strncpy(input_path, directory, path_length); | ||
strncpy(input_path + directory_length, "/input.txt", path_length); | ||
input_path[path_length - 1] = 0; | ||
|
||
FILE *file = fopen(input_path, "r"); | ||
fseek(file, 0, SEEK_END); | ||
size_t file_size = ftell(file); | ||
fseek(file, 0, SEEK_SET); | ||
|
||
char *input = malloc(file_size + 1); | ||
fread(input, sizeof(char), file_size, file); | ||
fclose(file); | ||
input[file_size] = 0; | ||
|
||
return (input_t)input; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef __INPUT_H__ | ||
#define __INPUT_H__ | ||
|
||
#include <stdlib.h> | ||
|
||
typedef char* input_t; | ||
|
||
#define read_input() read_input_impl(__FILE__) | ||
|
||
input_t read_input_impl(char *main_file); | ||
|
||
#endif |