diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-25 11:48:32 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-25 11:48:32 +0800 |
commit | 017f4c4312444c1ba386d6e0f6192fdc40f03c01 (patch) | |
tree | 3cf9ff0db673e3990363a3f154bf3df02d02fbc3 /pikul.c | |
parent | 6e7d2c8ef7394e31f18b7193fb1957d3242c72aa (diff) |
Origin & destination info retrieval implementations
Diffstat (limited to 'pikul.c')
-rw-r--r-- | pikul.c | 78 |
1 files changed, 78 insertions, 0 deletions
@@ -24,6 +24,8 @@ void anteraja_order(const char *, const char *, const char *, const char *, cons void anteraja_cleanup(); const char **sicepat_init(char *[]); +const char **sicepat_origins(); +const char **sicepat_destinations(); const char **sicepat_services(const char *, const char *, double); static void recurse(struct json_object *outer, const char *trail[], struct json_object **last) @@ -62,6 +64,46 @@ static size_t handle(char *contents, size_t size, size_t nmemb, struct shipping return realsize; } switch (shipping->mode) { + case PLACES: + ; + struct json_object *place_array = NULL; + recurse(response, shipping->trail, &place_array); + size_t places_length = json_object_array_length(place_array); + struct pikul_place **places = malloc(sizeof(struct pikul_place *) + * (places_length + 1)); + const char **place_attrs = (const char **)shipping->data; + enum { PLACE_CODE, DISTRICT, CITY, PROVINCE }; + for (size_t i = 0; i < places_length; i++) { + places[i] = malloc(sizeof(struct pikul_place)); + struct pikul_place *place = places[i]; + json_object *object = json_object_array_get_idx(place_array, i); + struct json_object_iterator iterator = json_object_iter_begin(object); + struct json_object_iterator iterator_end = json_object_iter_end(object); + while (!json_object_iter_equal(&iterator, &iterator_end)) { + const char *name = json_object_iter_peek_name(&iterator); + json_object *value = json_object_iter_peek_value(&iterator); + int len = json_object_get_string_len(value); + if (len) { + char *string = malloc(len + 1); + strcpy(string, json_object_get_string(value)); + if (!strcmp(name, place_attrs[PLACE_CODE])) + place->code = string; + else if (place_attrs[DISTRICT] + && !strcmp(name, place_attrs[DISTRICT])) + place->district = string; + else if (place_attrs[CITY] + && !strcmp(name, place_attrs[CITY])) + place->city = string; + else if (place_attrs[PROVINCE] + && !strcmp(name, place_attrs[PROVINCE])) + place->province = string; + } + json_object_iter_next(&iterator); + } + } + places[places_length] = NULL; + shipping->data = places; + break; case SERVICES: ; struct json_object *array = NULL; @@ -147,6 +189,42 @@ void pikul_init(enum pikul_company company, char *provisions[]) shipping->tokener = json_tokener_new(); } +struct pikul_place **pikul_origins(enum pikul_company company) +{ + struct shipping *shipping = shipping_list[company]; + switch (company) { + case PIKUL_SICEPAT: + shipping->data = sicepat_origins(); + break; + default: + break; + } + curl_easy_setopt(shipping->handle, CURLOPT_URL, shipping->url); + curl_easy_setopt(shipping->handle, CURLOPT_HTTPGET, 1L); + shipping->mode = PLACES; + curl_easy_perform(shipping->handle); + free(shipping->url); + return (struct pikul_place **)shipping->data; +} + +struct pikul_place **pikul_destinations(enum pikul_company company) +{ + struct shipping *shipping = shipping_list[company]; + switch (company) { + case PIKUL_SICEPAT: + shipping->data = sicepat_destinations(); + break; + default: + break; + } + curl_easy_setopt(shipping->handle, CURLOPT_URL, shipping->url); + curl_easy_setopt(shipping->handle, CURLOPT_HTTPGET, 1L); + shipping->mode = PLACES; + curl_easy_perform(shipping->handle); + free(shipping->url); + return (struct pikul_place **)shipping->data; +} + struct pikul_service **pikul_services(enum pikul_company company, const char *origin, const char *destination, double weight) { |