Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <sys/types.h>
  6. #include <string.h>
  7.  
  8. typedef struct{ //Structure pour la météo
  9.  
  10. float temp;
  11. float pressure;
  12. int humidity;
  13. char * main_description;
  14. char * description;
  15. char * icon;
  16. float wind_speed;
  17. float wind_deg;
  18. char * date;
  19. float rain;
  20. int cloud;
  21.  
  22.  
  23. }METEO;
  24.  
  25. int get_json();
  26.  
  27. char * cut_file(char * source, char * string_search, int size);
  28. char * recup_entreaccolade(char * string);
  29. char * recup_value(char * source, char * word_title);
  30. char * clean_string(char * string);
  31.  
  32. int main(int argc, char ** argv)
  33. {
  34. METEO * meteo = malloc(sizeof(METEO));
  35. meteo->description = malloc(50 * sizeof(char));
  36. meteo->date = malloc(50 * sizeof(char));
  37. meteo->icon = malloc(50 * sizeof(char));//Charge le contenu du fichier dans un bufferf(char));
  38. FILE * file;
  39. int size;
  40. char * buffer;
  41. char * buf2;
  42. char * buf3 = malloc(200 * sizeof(char));
  43. char * buf4;
  44. char * tab = malloc(200 * sizeof(char));
  45. char tab2[20];
  46. //Tableau qui contient tous les mots clés
  47. char word_list [][255] = {{"temp"},{"pressure"},{"humidity"},{"description"},{"speed"},{"deg"},{"dt_txt"},{"icon"},{"all"}};
  48. int i = 0;
  49. char * buf5 = malloc(100 * sizeof(char));
  50. char * string_clean = malloc(100 * sizeof(char));
  51. int value;
  52.  
  53. A remettre
  54. if(get_json() == 1) //Appel de la fonction pour récuperer le JSON
  55. {
  56. printf("erreur\n");
  57. return 0;
  58. }
  59.  
  60.  
  61. file = fopen("meteo.txt","r"); //On ouvre le fichier
  62.  
  63. //Récuperation de la taille du fichier
  64. fseek(file,0,SEEK_END);
  65. size = ftell(file);
  66. fseek(file,0,SEEK_SET);
  67.  
  68. buffer = malloc((size + 1) * sizeof(char)); //Allocation d'un buffer avec la taille récuperer juste avant
  69.  
  70. fread(buffer,size,1,file); //Charge le contenu du fichier dans un buffer
  71. buffer[size] = '\0'; //Fin de chaine
  72.  
  73. //S'il y a une erreur dans le fichier, on arrete le programme pour le pas rentrer de fausse info en bdd
  74. if(strstr(buffer,"error") != NULL || strstr(buffer,"invalid") != NULL)
  75. return 0;
  76.  
  77. while((buffer = strstr(buffer,"\"dt\"")) != NULL) //"dt" est le délimiteur de chaque infos différentes
  78. {
  79. buffer += 4;
  80. buf2 = cut_file(buffer,"\"dt\"",size); //Appel de la fonction cut_file qui permet de découper le JSON
  81. if(buf2 == NULL) //S'il n'y a plus de "dt" on s'arrête
  82. break;
  83.  
  84.  
  85. buf3 = strstr(buf2,"\"rain\":");
  86. if((tab = recup_entreaccolade(buf3)) != NULL )
  87. printf("%s\n\n",tab);
  88.  
  89. for(i = 0 ; i<9; i++) //Taille du tableau de mots clés
  90. {
  91. buf4 = recup_value(buf2, word_list[i]); //On récupère la chaine de caractère avec le mot clé et sa valeur
  92. buf5 = strchr(buf4,':'); //On récupère juste la valeur de la chaine d'au dessus qui est situé après le ":"
  93.  
  94. string_clean = clean_string(buf5); //Appel de la fontion qui permet de supprimer tous les caractères en trop
  95.  
  96. switch(i)
  97. {
  98. case 0: //Temperature
  99. meteo->temp = atof(string_clean); //Transforme la valeur en float
  100. meteo->temp -= 273.15; //Calcul pour passer du Kelvin en °C
  101. break;
  102. case 1: //Pression
  103. meteo->pressure = atof(string_clean);
  104. break;
  105. case 2: //Humidite
  106. meteo->humidity = atoi(string_clean); //Transforme la valeur en int
  107. break;
  108. case 3: //Description
  109. meteo->description = string_clean;
  110. break;
  111. case 4: //Vitesse du vent
  112. meteo->wind_speed = atof(string_clean);
  113. break;
  114. case 5: //Orientation du vent
  115. meteo->wind_deg = atof(string_clean);
  116. break;
  117. case 6: //Date et heure de l'info
  118. meteo->date = string_clean;
  119. break;
  120. case 7: //id de l'icone
  121. meteo->icon = string_clean;
  122. break;
  123. case 8: //Nuage
  124. meteo->cloud = atoi(string_clean);
  125. break;
  126.  
  127. }
  128.  
  129. }
  130.  
  131. printf("Temperature : %.2f degre\n",meteo->temp);
  132. printf("Pression : %.2f\n",meteo->pressure);
  133. printf("Humidite : %d\n",meteo->humidity);
  134. printf("Description : %s\n",meteo->description);
  135. printf("Vitesse vent : %.2f m/s\n",meteo->wind_speed);
  136. printf("Degre vent : %.2f\n",meteo->wind_deg);
  137. printf("Date : %s\n",meteo->date);
  138. printf("Icon : %s\n",meteo->icon);
  139. printf("Nuage : %d\n\n",meteo->cloud);
  140.  
  141. }
  142.  
  143. free(buffer);
  144. free(buf2);
  145. free(buf3);
  146. free(buf4);
  147. free(buf5);
  148. free(string_clean);
  149. fclose(file);
  150. return 0;
  151. }
  152.  
  153.  
  154. int get_json()
  155. {
  156. system("curl -o meteo.txt 'http://api.openweathermap.org/data/2.5/forecast/city?id=3019265&APPID=1c8dbc2a972b71d61606b6567b305b07&lang=fr'");
  157.  
  158. return 0;
  159.  
  160. }
  161.  
  162. char * cut_file(char * source, char * string_search, int size)
  163. {
  164. char * buf = malloc((size + 1) * sizeof(char));
  165. char * buf2;
  166. int size_info;
  167.  
  168. buf = strstr(source,string_search);
  169. if(buf == NULL)
  170. return NULL;
  171. size_info = strlen(source) - strlen(buf);
  172.  
  173.  
  174. buf2= malloc((size_info + 1) * sizeof(char));
  175.  
  176. strncpy(buf2,source,size_info);
  177. buf2[size_info] = '\0';
  178.  
  179. return buf2;
  180. }
  181.  
  182. char * recup_entreaccolade(char * string)
  183. {
  184. char * buf = malloc(400 * sizeof(char));
  185. char * buf2;
  186.  
  187. int size;
  188. if((buf = strchr(string,'{') ) == NULL)
  189. return NULL;
  190.  
  191. buf2 = strchr(string,'}');
  192. size = strlen(buf) - strlen(buf2);
  193.  
  194. buf2 = malloc((size+1) * sizeof(char));
  195.  
  196. strncpy(buf2,buf,size);
  197. buf2[size] = '\0';
  198. printf("SIZE : %d\n",size);
  199.  
  200. //free(buf);
  201. return buf2;
  202. }
  203.  
  204. char * recup_value(char * source, char * word_title)
  205. {
  206. char * buf = malloc(400 * sizeof(char));
  207. char * buf2 = malloc(100 * sizeof(char));
  208. int i = 0;
  209. int size = 0 ;
  210.  
  211. buf = strstr(source,word_title);
  212. /*
  213. buf2 = strchr(buf,',');
  214. size = strlen(buf) - strlen(buf2);
  215. strncpy(buf2,buf,size);
  216. buf2[size] = '\0';
  217. */
  218. //printf("buf : %s \n",buf);
  219. while(buf[i] != ',') //Ou ' \" '
  220. {
  221. *(buf2 + i) = *(buf + i);
  222. i++;
  223. }
  224. *(buf2 + i) = '\0';
  225.  
  226. //free(buf);
  227. return buf2;
  228. }
  229.  
  230. char * clean_string(char * string)
  231. {
  232. char * buf = malloc(100 * sizeof(char));
  233. int i = 0;
  234. int j = 0;
  235.  
  236. while(string[i] != '\0')
  237. {
  238. if((string[i] <= 'z' && string[i] >= 'a') || (string[i] <= '9' && string[i] >= '0') || string[i] == ' ' || string[i] == '.')
  239. {
  240. buf[j] = string[i];
  241. j++;
  242. }
  243. i++;
  244. }
  245. //printf("%s\n",buf);
  246. return buf;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement