aboutsummaryrefslogtreecommitdiff
path: root/dbfp.c
blob: d822f20d170cc0b31801813f70e69a126d7ed351 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2016 Robin Krahl <robin.krahl@ireas.org>
 *
 * dbfp.c
 */

#include "dbfp.h"

#include <curl/curl.h>
#include <errno.h>
#include <expat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DBFP_BASE_URL "http://open-api.bahn.de/bin/rest.exe"
#define DBFP_URL_FORMAT "%s/%s?authKey=%s&%s"
#define DBFP_URL_LEN 8000

struct parse_data {
	struct dbfp_status *status;
	void *data;
};

struct loc_data {
	size_t n;
	struct dbfp_location *locs;
};

static int dbfp_parse(void *contents, size_t len, size_t n, void *data);
static void dbfp_request(struct dbfp *dbfp, char *service, char *query,
		XML_Parser parser, struct dbfp_status *status);
static int dbfp_url(struct dbfp *dbfp, char *service, char *query, char **url);

static void handle_loc(struct loc_data *ld, const char **atts);
static void loc_ele_start(void *data, const char *name, const char **atts);
static void loc_ele_end(void *data, const char *name);

int dbfp_init(struct dbfp *dbfp, char *key)
{
	if (!dbfp || !key)
		return EINVAL;

	dbfp->key = strdup(key);
	if (!dbfp->key)
		return ENOMEM;

	curl_global_init(CURL_GLOBAL_ALL ^ CURL_GLOBAL_SSL);

	return 0;
}

void dbfp_close(struct dbfp *dbfp)
{
	if (!dbfp)
		return;

	curl_global_cleanup();

	free(dbfp->key);
}

struct dbfp_status dbfp_query_location_name(struct dbfp *dbfp, char *input,
		size_t *n, struct dbfp_location **out)
{
	struct dbfp_status status = { 0, 0, 0, 0, 0 };
	char *query = NULL;
	int len;
	struct parse_data pd;
	struct loc_data ld;
	XML_Parser parser = NULL;

	if (!dbfp || !input) {
		status.error = 1;
		status.run_error = EINVAL;
		goto cleanup;
	}

	/*
	 * TODO(robin.krahl):  improve query building and implement HTML
	 * encoding.
	 */
	query = malloc(DBFP_URL_LEN);
	if (!query) {
		status.error = 1;
		status.run_error = ENOMEM;
		goto cleanup;
	}
	len = snprintf(query, DBFP_URL_LEN, "input=%s", input);
	if (len < 0 || len >= DBFP_URL_LEN) {
		status.error = 1;
		status.run_error = -1;
		goto cleanup;
	}

	pd.status = &status;
	pd.data = &ld;
	ld.n = 0;
	ld.locs = NULL;

	parser = XML_ParserCreateNS(NULL, '\0');
	XML_SetUserData(parser, &pd);
	XML_SetElementHandler(parser, loc_ele_start, loc_ele_end);

	dbfp_request(dbfp, "location.name", query, parser, &status);

	if (!status.error) {
		if (n)
			*n = ld.n;
		if (out)
			*out = ld.locs;
		else
			free(ld.locs);
	} else {
		free(ld.locs);
	}

cleanup:
	XML_ParserFree(parser);
	free(query);

	return status;
}

static int dbfp_parse(void *contents, size_t len, size_t n, void *data)
{
	XML_Parser parser = (XML_Parser)data;
	struct parse_data *pd;
	size_t size = len * n;

	pd = (struct parse_data *)XML_GetUserData(parser);

	if (!pd->status->parse_error && !XML_Parse(parser, contents, size, 0)) {
		pd->status->error = 1;
		pd->status->parse_error = XML_GetErrorCode(parser);
	}

	return size;
}

static void dbfp_request(struct dbfp *dbfp, char *service, char *query,
		XML_Parser parser, struct dbfp_status *status)
{
	char *url;
	CURL *curl = NULL;

	status->run_error = dbfp_url(dbfp, service, query, &url);
	if (status->run_error)
		goto cleanup;

	curl = curl_easy_init();
	if (!curl) {
		status->error = 1;
		status->run_error = -1;
		goto cleanup;
	}

	curl_easy_setopt(curl, CURLOPT_URL, url);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dbfp_parse);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)parser);

	status->curl_error = curl_easy_perform(curl);

	if (status->parse_error)
		fprintf(stderr, "Parse error %d: %s\n", status->parse_error,
				XML_ErrorString(status->parse_error));

cleanup:
	if (curl)
		curl_easy_cleanup(curl);
	free(url);
}

static int dbfp_url(struct dbfp *dbfp, char *service, char *query, char **url)
{
	int len;

	*url = malloc(DBFP_URL_LEN);
	if (!*url)
		return ENOMEM;

	len = snprintf(*url, DBFP_URL_LEN, DBFP_URL_FORMAT, DBFP_BASE_URL,
			service, dbfp->key, query);
	if (len < 0 || len >= DBFP_URL_LEN) {
		free(*url);
		*url = NULL;
		return -1;
	}

	return 0;
}

static void handle_loc(struct loc_data *ld, const char **atts)
{
	char *name = NULL;
	char *id = NULL;
	size_t i;

	/*
	 * TODO(robin.krahl):  fix error handling for the cases that a)
	 * name or id is not set and b) realloc fails.
	 */

	for (i = 0; atts[i]; i += 2) {
		if (strcmp(atts[i], "name") == 0)
			name = strdup(atts[i + 1]);
		else if (strcmp(atts[i], "id") == 0)
			id = strdup(atts[i + 1]);
	}

	if (name && id) {
		ld->locs = realloc(ld->locs, (ld->n + 1) * sizeof(*ld->locs));
		if (ld->locs) {
			ld->n++;
			ld->locs[ld->n - 1].name = name;
			ld->locs[ld->n - 1].id = id;
		}
	} else {
		free(name);
		free(id);
	}
}

static void loc_ele_start(void *data, const char *name, const char **atts)
{
	struct parse_data *pd = (struct parse_data *)data;
	struct loc_data *ld = (struct loc_data *)pd->data;
	size_t i;

	if (strcmp(name, "Error") == 0) {
		for (i = 0; atts[i]; i += 2) {
			if (strcmp(atts[i], "code")) {
				fprintf(stderr, "err code %s\n", atts[i + 1]);
				break;
			}
		}
	} else if (strcmp(name, "StopLocation") == 0) {
		handle_loc(ld, atts);
	}
}

static void loc_ele_end(void *data, const char *name)
{
}