toolbox

Check-in [f2f1081b64]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:arte7dump: use a struct for json search patterns
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f2f1081b649493552348378d23d5db4bf3c4f5cf68f4324be41a52f342a56144
User & Date: jef 2019-08-04 15:19:40
Context
2019-08-06
07:33
arte7dump: improve example in man page check-in: 92ffe56f12 user: jef tags: trunk
2019-08-04
15:19
arte7dump: use a struct for json search patterns check-in: f2f1081b64 user: jef tags: trunk
14:42
radio: cosmetic changes check-in: cf6db36e7b user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/arte7dump/arte7dump.c.

14
15
16
17
18
19
20





21
22
23
24
25
26
27
...
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
typedef size_t (*curl_write_cb)(char *httpdata, size_t size, size_t nitems, void *userdata);

struct buffer {
	char *data;
	size_t cap;
	size_t size;
};






struct opts {
	char lang[3];
	int width;
	int dump;
} opts = { "fr", 1280, 0 };

................................................................................
	struct buffer buffer = { 0 };
	CURL *curl;
	char *pos, *json;
	int jsonlen;
	size_t njsons = 0;
	char buf[256] = { 0 };
	size_t i;
	char *patterns[2][2] = {
		{"var json_url = '", "%255[^']'"},
		{"json_url=", "%255[^\"]"}
	};

	curl = curl_easy_init();
	if (curl == NULL)
		die("curl error");
	curl_do(url, write_buffer_cb, &buffer);
	for (i = 0; i < ARRAY_SIZE(patterns); i++) {
		pos = buffer.data;
		while (njsons < jsons_size && (pos = strstr(pos, patterns[i][0])) != NULL) {
			pos += strlen(patterns[i][0]);
			if (sscanf(pos, patterns[i][1], &buf) == 1) {
				json = curl_easy_unescape(curl, buf, 0, &jsonlen);
				jsons[njsons++] = xstrdup(json);
			}
			pos++;
		}
	}
	curl_easy_cleanup(curl);







>
>
>
>
>







 







|








|

|
|
|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
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
typedef size_t (*curl_write_cb)(char *httpdata, size_t size, size_t nitems, void *userdata);

struct buffer {
	char *data;
	size_t cap;
	size_t size;
};

struct json_search {
	char *prefix;
	char *scan_pattern;
};

struct opts {
	char lang[3];
	int width;
	int dump;
} opts = { "fr", 1280, 0 };

................................................................................
	struct buffer buffer = { 0 };
	CURL *curl;
	char *pos, *json;
	int jsonlen;
	size_t njsons = 0;
	char buf[256] = { 0 };
	size_t i;
	struct json_search json_search[2] = {
		{"var json_url = '", "%255[^']'"},
		{"json_url=", "%255[^\"]"}
	};

	curl = curl_easy_init();
	if (curl == NULL)
		die("curl error");
	curl_do(url, write_buffer_cb, &buffer);
	for (i = 0; i < ARRAY_SIZE(json_search); i++) {
		pos = buffer.data;
		while (njsons < jsons_size && (pos = strstr(pos, json_search[i].prefix)) != NULL) {
			pos += strlen(json_search[i].prefix);
			if (sscanf(pos, json_search[i].scan_pattern, &buf) == 1) {
				json = curl_easy_unescape(curl, buf, 0, &jsonlen);
				jsons[njsons++] = xstrdup(json);
			}
			pos++;
		}
	}
	curl_easy_cleanup(curl);