summaryrefslogtreecommitdiff
path: root/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'input.c')
-rw-r--r--input.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/input.c b/input.c
new file mode 100644
index 0000000..5d184bb
--- /dev/null
+++ b/input.c
@@ -0,0 +1,55 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <err.h>
+
+/*public*/size_t count_lines(FILE* file) {
+ size_t lineCount = 0;
+ char currentChar;
+ // on part du debut
+ // TODO verifier le code de retour
+ fseek(file, 0, SEEK_SET);
+ while((currentChar=fgetc(file)) != EOF) {
+ if(currentChar == '\n')
+ lineCount++;
+ }
+ return lineCount;
+}
+
+/*public*/int read_lines_as_int(FILE* file, int* result, int resultSize) {
+ // retour au debut du fichier et lecture des lignes
+ // TODO verifier le code de retour
+ fseek(file, 0, SEEK_SET);
+ int current;
+ for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) {
+ if(fscanf(file, "%d", &current) != 1) {
+ result[lineIndex] = 0;
+ printf("Erreur de lecture ligne %d\n", lineIndex);
+ continue;
+ }
+ result[lineIndex] = current;
+ }
+
+ return 0;
+}
+
+/*public*/int read_lines_as_string(FILE* file, char** result, int resultSize) {
+ // retour au debut du fichier et lecture des lignes
+ // TODO verifier le code de retour
+ fseek(file, 0, SEEK_SET);
+ size_t lineSize = 0;
+ for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) {
+ char** dst = &result[lineIndex];
+ if(getline(dst, &lineSize, file) < 0)
+ err(1, "Erreur de lecture ligne %d\n", lineIndex);
+ }
+
+ return 0;
+}
+
+/*public*/void free_strings(char** strings, size_t stringCount) {
+ for(size_t i = 0; i < stringCount; i++) {
+ free(strings[i]);
+ }
+ free(strings);
+}