Sam-Sampaio

cep1

Sep 29th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <json.h>
  5. #include <curl/curl.h>
  6.  
  7. struct memory
  8. {
  9.    char *response;
  10.    size_t size;
  11. };
  12.  
  13. static size_t func(void *data, size_t size, size_t nmemb, void *userp)
  14. {
  15.    size_t realsize = size * nmemb;
  16.    struct memory *mem = (struct memory *)userp;
  17.  
  18.    char *ptr = realloc(mem->response, mem->size + realsize + 1);
  19.   if(!ptr)
  20.   {
  21.      fputs("Failed Memory\n", stdout);
  22.      return EXIT_FAILURE;
  23.   }
  24.  
  25.    mem->response = ptr;
  26.    memcpy(&(mem->response[mem->size]), data, realsize);
  27.    mem->size += realsize;
  28.    mem->response[mem->size] = 0;
  29.  
  30.    return realsize;
  31. }
  32.  
  33. void parse(char *buffer)
  34. {
  35.      struct json_object *jobj;
  36.      struct json_object *j[10];
  37.      jobj = json_tokener_parse(buffer);
  38.  
  39.      size_t i;
  40.      static char *keys[] = {"cep", "logradouro", "complemento",
  41.                        "bairro", "localidade", "uf",
  42.                        "ibge", "gia", "ddd", "siafi"};
  43.  
  44.  
  45.      for(i = 0; i < 10; i++)
  46.         json_object_object_get_ex(jobj, keys[i], &j[i]);
  47.  
  48.  
  49.       const char *name = json_object_get_string(j[4]);
  50.       printf("%s\n", name);
  51. }
  52.  
  53. int main(void)
  54. {
  55.     struct memory s;
  56.     s.response = malloc(1000);
  57.    
  58.     if(!s.response)
  59.     {
  60.       fputs("Failed Memory\n", stdout);
  61.       return EXIT_FAILURE;
  62.     }
  63.     s.size = 0;    
  64.  
  65.     fputs("Input your CEP: ", stdout);
  66.  
  67.     char cep[9];
  68.     scanf("%9[^\n]s", cep);
  69.  
  70.     size_t i, j = 0;
  71.  
  72.     for(i = 0; cep[i]; i++)
  73.       if(cep[i] >= '0' && cep[i] <= '9') j++;
  74.  
  75.     if(j == 8)
  76.     {
  77.        
  78.        char url[50];
  79.  
  80.        snprintf(url, sizeof url, "http://viacep.com.br/ws/%s/json/", cep);    
  81.    
  82.        CURL *curl = curl_easy_init();
  83.  
  84.        if(curl)
  85.        {
  86.            curl_easy_setopt(curl, CURLOPT_URL, url);
  87.            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, func);
  88.            curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&s);
  89.            curl_easy_perform(curl);
  90.            parse(s.response);
  91.            curl_easy_cleanup(curl);
  92.         } else {
  93.            fputs("Request Failed\n", stdout);
  94.            return EXIT_FAILURE;    
  95.         }
  96.      }  else  {
  97.         fputs("Invalid Number\n", stdout);
  98.     }
  99.      free(s.response);
  100.  
  101.     return 0;
  102. }
  103.  
Add Comment
Please, Sign In to add comment