Advertisement
Guest User

test

a guest
Apr 7th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include "../include/my.h"
  2.  
  3. int core(t_line *file)
  4. {
  5.     file->file_content = NULL;
  6.     if ((file->file_content = read_my_file(file->file_name)) == NULL)// permet de stocker le json dans file content
  7.         return (84);
  8.     find_my_tmp(file);
  9.     tmp_value_compare(file);
  10.     return (0);
  11. }
  12.  
  13. char *read_my_file(char *filename)
  14. {
  15.     char *dest = NULL;
  16.     int fd = -1;
  17.     size_t len = 0;
  18.     struct stat st;
  19.  
  20.     if ((fd = open(filename, O_RDONLY)) == -1) {
  21.         print("Impossible de trouver le fichier.");
  22.         return NULL;
  23.     }
  24.     if ((stat(filename, &st)) == -1) {
  25.         print("Problème rencontré avec la lib systeme.");
  26.         return NULL;
  27.     }
  28.     len = st.st_size; // entier le plus grand, len est plus grand qu'un int
  29.  
  30.     if ((dest = malloc(sizeof(char) * len + 1)) == NULL) {  // malloc attribue memoire dyn, malloc a besoin d'une taille
  31.                                                             //sizeof. char vaut un octet. +1 pr caractere final
  32.         print("Allocation de mémoire impossible.");
  33.         return NULL;
  34.     }
  35.     if (read(fd, dest, len) == -1) {
  36.         print("Impossible d'ouvrir le fichier");
  37.         return NULL;
  38.     }
  39.     return (dest);
  40.  
  41. }
  42.  
  43. void find_my_tmp(t_line *file)
  44. {
  45.  
  46.     file->tmp_min = getMinTmp(file);
  47.     file->tmp_max = getMaxTmp(file);
  48.     file->tmp_value = getValueTmp(file);
  49. }
  50.  
  51. int getMinTmp(t_line *file)
  52. {
  53.     char *tmp = NULL;
  54.     char *value_str = malloc(sizeof(char) * 20);
  55.     int idx_vs;
  56.     char *name = "tmp min";
  57.     int idx = my_strlen(name) + 4;
  58.  
  59.     if ((tmp = strstr(file->file_content, name)) == NULL)
  60.         exit(84);
  61.     idx_vs = 0;
  62.     if (tmp[idx] == '-') {
  63.         idx += 1;
  64.         value_str[idx_vs] = '-';
  65.         idx_vs += 1;
  66.     }
  67.  
  68.     for (; tmp[idx] && tmp[idx] != ','; idx += 1, idx_vs += 1) // stocker le nombre dans valuestr
  69.         value_str[idx_vs] = tmp[idx];
  70.  
  71.     return (atoi(value_str)); // atoi transforme str en int
  72. }
  73.  
  74. int getMaxTmp(t_line *file)
  75. {
  76.     char *tmp = NULL;
  77.     char *value_str = malloc(sizeof(char) * 20); // 20 taille du fichier json
  78.     int idx_vs;
  79.     char *name = "tmp max";
  80.     int idx = my_strlen(name) + 4; // compteur, +4 signifie l'ecart " : "
  81.  
  82.     if ((tmp = strstr(file->file_content, name)) == NULL)
  83.         exit(84); // fin programme
  84.     idx_vs = 0; // int parcours la chaine de caractere
  85.     if (tmp[idx] == '-') {
  86.         idx += 1;
  87.         value_str[idx_vs] = '-';
  88.         idx_vs += 1;
  89.     }
  90.  
  91.     for (; tmp[idx] && tmp[idx] != ','; idx += 1, idx_vs += 1)
  92.         value_str[idx_vs] = tmp[idx];
  93.  
  94.     return (atoi(value_str));
  95. }
  96.  
  97. int getValueTmp(t_line *file)
  98. {
  99.     char *tmp = NULL;
  100.     char *value_str = malloc(sizeof(char) * 20);
  101.     int idx_vs;
  102.     char *name = "tmp value";
  103.     int idx = my_strlen(name) + 4;
  104.  
  105.     if ((tmp = strstr(file->file_content, name)) == NULL)
  106.         exit(84);
  107.     idx_vs = 0;
  108.     if (tmp[idx] == '-') {
  109.         idx += 1;
  110.         value_str[idx_vs] = '-';
  111.         idx_vs += 1;
  112.     }
  113.    
  114.  
  115.     for (; tmp[idx] && tmp[idx] != '\n'; idx += 1, idx_vs += 1)
  116.         value_str[idx_vs] = tmp[idx];
  117.    
  118.  
  119.     return (atoi(value_str));
  120. }
  121.  
  122. void tmp_value_compare(t_line *file)
  123. {
  124.     printf("Value tmp max = %d.\nValue tmp min = %d.\nValue tmp = %d.\n\n", file->tmp_max, file->tmp_min, file->tmp_value);
  125.     if (file->tmp_value >= file->tmp_max)
  126.         print("La température est élevée.\n");
  127.     else if (file->tmp_value <= file->tmp_min)
  128.         print("La température est basse.\n");
  129.     else
  130.         print("LA température est stable.\n");
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement