Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../include/my.h"
- int core(t_line *file)
- {
- file->file_content = NULL;
- if ((file->file_content = read_my_file(file->file_name)) == NULL)// permet de stocker le json dans file content
- return (84);
- find_my_tmp(file);
- tmp_value_compare(file);
- return (0);
- }
- char *read_my_file(char *filename)
- {
- char *dest = NULL;
- int fd = -1;
- size_t len = 0;
- struct stat st;
- if ((fd = open(filename, O_RDONLY)) == -1) {
- print("Impossible de trouver le fichier.");
- return NULL;
- }
- if ((stat(filename, &st)) == -1) {
- print("Problème rencontré avec la lib systeme.");
- return NULL;
- }
- len = st.st_size; // entier le plus grand, len est plus grand qu'un int
- if ((dest = malloc(sizeof(char) * len + 1)) == NULL) { // malloc attribue memoire dyn, malloc a besoin d'une taille
- //sizeof. char vaut un octet. +1 pr caractere final
- print("Allocation de mémoire impossible.");
- return NULL;
- }
- if (read(fd, dest, len) == -1) {
- print("Impossible d'ouvrir le fichier");
- return NULL;
- }
- return (dest);
- }
- void find_my_tmp(t_line *file)
- {
- file->tmp_min = getMinTmp(file);
- file->tmp_max = getMaxTmp(file);
- file->tmp_value = getValueTmp(file);
- }
- int getMinTmp(t_line *file)
- {
- char *tmp = NULL;
- char *value_str = malloc(sizeof(char) * 20);
- int idx_vs;
- char *name = "tmp min";
- int idx = my_strlen(name) + 4;
- if ((tmp = strstr(file->file_content, name)) == NULL)
- exit(84);
- idx_vs = 0;
- if (tmp[idx] == '-') {
- idx += 1;
- value_str[idx_vs] = '-';
- idx_vs += 1;
- }
- for (; tmp[idx] && tmp[idx] != ','; idx += 1, idx_vs += 1) // stocker le nombre dans valuestr
- value_str[idx_vs] = tmp[idx];
- return (atoi(value_str)); // atoi transforme str en int
- }
- int getMaxTmp(t_line *file)
- {
- char *tmp = NULL;
- char *value_str = malloc(sizeof(char) * 20); // 20 taille du fichier json
- int idx_vs;
- char *name = "tmp max";
- int idx = my_strlen(name) + 4; // compteur, +4 signifie l'ecart " : "
- if ((tmp = strstr(file->file_content, name)) == NULL)
- exit(84); // fin programme
- idx_vs = 0; // int parcours la chaine de caractere
- if (tmp[idx] == '-') {
- idx += 1;
- value_str[idx_vs] = '-';
- idx_vs += 1;
- }
- for (; tmp[idx] && tmp[idx] != ','; idx += 1, idx_vs += 1)
- value_str[idx_vs] = tmp[idx];
- return (atoi(value_str));
- }
- int getValueTmp(t_line *file)
- {
- char *tmp = NULL;
- char *value_str = malloc(sizeof(char) * 20);
- int idx_vs;
- char *name = "tmp value";
- int idx = my_strlen(name) + 4;
- if ((tmp = strstr(file->file_content, name)) == NULL)
- exit(84);
- idx_vs = 0;
- if (tmp[idx] == '-') {
- idx += 1;
- value_str[idx_vs] = '-';
- idx_vs += 1;
- }
- for (; tmp[idx] && tmp[idx] != '\n'; idx += 1, idx_vs += 1)
- value_str[idx_vs] = tmp[idx];
- return (atoi(value_str));
- }
- void tmp_value_compare(t_line *file)
- {
- printf("Value tmp max = %d.\nValue tmp min = %d.\nValue tmp = %d.\n\n", file->tmp_max, file->tmp_min, file->tmp_value);
- if (file->tmp_value >= file->tmp_max)
- print("La température est élevée.\n");
- else if (file->tmp_value <= file->tmp_min)
- print("La température est basse.\n");
- else
- print("LA température est stable.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement