1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#include "shipping.h"
#include "handler.h"
CURL *curl;
json_tokener *tokener;
struct shipping shipping;
static char *waybill = NULL;
extern inline void headers(const char *[], char *[]);
extern inline void handle(enum type, const char *, size_t, const char *[], const char *[], const char *[],
void *);
extern void anteraja_init(char *[]);
extern void anteraja_services(const char *, const char *, double, char **, char **);
extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **);
extern void anteraja_order(const char *, const char *, const char *, const char *, const char *,
const char *, const char *, const char *, const char *, const char *, int,
char **[], double, char **, char **);
extern size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **);
extern void anteraja_cleanup();
void pikul_init(enum pikul_company company, char *provisions[])
{
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
#ifdef DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
shipping.company = company;
switch (company) {
case PIKUL_ANTERAJA:
anteraja_init(provisions);
break;
default:
break;
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, shipping.headers);
tokener = json_tokener_new();
}
void recurse(struct json_object *outer, const char *trail[], struct json_object **last)
{
struct json_object *inner = NULL;
json_object_object_get_ex(outer, *trail, &inner);
if (*++trail)
recurse(inner, trail, last);
else
*last = inner;
}
struct pikul_services *pikul_services(const char *origin, const char *destination, double weight)
{
struct pikul_services *services = NULL;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &services);
char *url;
char *post = NULL;
size_t (*handler)(const char *, size_t, size_t, struct pikul_services **);
switch (shipping.company) {
case PIKUL_ANTERAJA:
anteraja_services(origin, destination, weight, &url, &post);
handler = anteraja_services_handle;
break;
default:
break;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handler);
curl_easy_perform(curl);
if (post)
free(post);
free(url);
return services;
}
static inline void free_service(struct pikul_service *service)
{
if (service->etd)
free(service->etd);
if (service->name)
free(service->name);
free(service->code);
free(service);
}
void pikul_free_services(struct pikul_services *services)
{
for (size_t i = 0; i < services->length; i++)
free_service(services->list[i]);
free(services);
}
static int servicecmp(const void *service1, const void *service2)
{
return strcmp((*(struct pikul_service * const *)service1)->code,
(*(struct pikul_service * const *)service2)->code);
}
double pikul_cost(const char *origin, const char *destination, double weight, const char *code)
{
struct pikul_services *services = pikul_services(origin, destination, weight);
if (!services || !services->length)
return .0;
qsort(services->list, services->length, sizeof(struct pikul_service *), servicecmp);
struct pikul_service *key_service = malloc(sizeof(struct pikul_service));
memset(key_service, '\0', sizeof(struct pikul_service));
key_service->code = malloc(strlen(code) + 1);
strcpy(key_service->code, code);
double cost = (*(struct pikul_service **)bsearch(&key_service, services->list,
services->length, sizeof(struct pikul_service *), servicecmp))->cost;
free_service(key_service);
pikul_free_services(services);
return cost;
}
char *pikul_order(const char *trx_id, const char *service, const char *sender_name,
const char *sender_phone, const char *origin, const char *sender_address,
const char *receiver_name, const char *receiver_phone, const char *destination,
const char *receiver_address, int nitems, char **items[], double subtotal)
{
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &waybill);
char *url;
char *post = NULL;
size_t (*handler)(const char *, size_t, size_t, char **);
switch (shipping.company) {
case PIKUL_ANTERAJA:
anteraja_order(trx_id, service, sender_name, sender_phone, origin, sender_address,
receiver_name, receiver_phone, destination, receiver_address,
nitems, items, subtotal, &url, &post);
handler = anteraja_order_handle;
break;
default:
break;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handler);
curl_easy_perform(curl);
if (post)
free(post);
free(url);
return waybill;
}
void pikul_cleanup()
{
switch (shipping.company) {
case PIKUL_ANTERAJA:
anteraja_cleanup();
break;
default:
break;
}
if (waybill)
free(waybill);
free(shipping.base);
json_tokener_free(tokener);
curl_slist_free_all(shipping.headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
}
|