From 6f5be3c839bf7af8ad41774ca2efc7be0c12197b Mon Sep 17 00:00:00 2001
From: Rick <rick@gnous.eu>
Date: Mon, 4 Jan 2021 15:16:30 +0100
Subject: [PATCH] Ajout fonctions pour jour9
---
lib/utils.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/utils.h | 2 ++
2 files changed, 57 insertions(+)
diff --git a/lib/utils.c b/lib/utils.c
index 916c55f..1141bdd 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -69,3 +69,58 @@ int check_void_line(FILE *ptr)
return ret;
}
+
+/**
+ * sum_ptr(): make the sum of int in pointer
+ * @ptr: the pointer with int
+ * @size: the size of pointer
+ *
+ *
+ *
+ * Return: the sum
+ */
+int sum_ptr(int *ptr, int size)
+{
+ int ret = 0;
+ for (int i = 0; i < size; i++)
+ {
+ ret += *(ptr + i);
+ }
+ return ret;
+}
+
+/**
+ * go_line(): positionne le curseur à la ligne donnée
+ * @ptr: le pointeur vers le fichier
+ * @line: le numéro de la ligne où mettre le curseur
+ *
+ * met le curseur à la ligne passée en paramètre
+ *
+ * Return: -1 quand pointeur null ou line négative
+ * 1 quand le curseur est à la fin du fichier
+ * 0 quand tout est ok
+ */
+int go_line(FILE *ptr, int line)
+{
+ int ret = -1;
+
+ if (ptr != NULL && line >= 0)
+ {
+ fseek(ptr, 0, SEEK_SET);
+ char *tmp = NULL;
+ size_t t = 0;
+ int i = 0;
+ while (i < line - 1 && !feof(ptr))
+ {
+ getline(&tmp, &t, ptr);
+ i++;
+ }
+
+ if (feof(ptr))
+ ret = 1;
+
+ free(tmp);
+ }
+
+ return ret;
+}
diff --git a/lib/utils.h b/lib/utils.h
index 09a407a..40b1629 100644
--- a/lib/utils.h
+++ b/lib/utils.h
@@ -1,3 +1,5 @@
int sub_digit(char *str);
int check_ecl(char *str);
int check_void_line(FILE *ptr);
+int sum_ptr(int *ptr, int size);
+int go_line(FILE *ptr, int line);