Check-in [a8786cdfae]

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

Overview
Comment:Use utarray to manage the tags
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a8786cdfaece24e962272c9b556dad320be2065a
User & Date: bapt 2013-07-22 13:54:40
Context
2013-07-22
13:55
With the forgotten utarray.h :) check-in: aea3cb6063 user: bapt tags: trunk
13:54
Use utarray to manage the tags check-in: a8786cdfae user: bapt tags: trunk
13:35
Remove more dead code check-in: f89d9a28b1 user: bapt tags: trunk
Changes

Changes to cplanet.c.

22
23
24
25
26
27
28

29
30
31
32
33
34
35
..
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
...
342
343
344
345
346
347
348


349
350
351
352
353
354
355
...
364
365
366
367
368
369
370
371

372
373
374
375
376
377
378
379
380
381
382
383
384
385
...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

440
441
442
443
444
445
446
#include <inttypes.h>
#include <sqlite3.h>
#include <expat.h>
#include <curl/curl.h>
#include <stdbool.h>

#include "utstring.h"

#include "cplanet.h"

typedef enum {
	NONE,
	RSS,
	ATOM,
	UNKNOWN
................................................................................
	const unsigned char *name;
	UT_string *blog_title;
	UT_string *author;
	bool has_author;
	UT_string *data;
	sqlite3_stmt *stmt;
	sqlite3_stmt *tags;
	char **tag;
	int nbtags;
	struct buffer *xmlpath;
	feed_type type;
};

struct buffer {
	char *data;
	size_t size;
................................................................................
		warnx("Convert struct tm (from '%s') to time_t failed", s);
		return 0;
	}

	return t;
}

static void
add_tag(struct feed *feed, const char *data)
{
	feed->nbtags++;
	if (feed->tag == NULL) {
		feed->tag = malloc(feed->nbtags * sizeof(char *));
	} else {
		feed->tag = realloc(feed->tag, feed->nbtags * sizeof(char *));
	}
	feed->tag[feed->nbtags - 1] = strdup(data);
}

static void
parse_atom_el(struct feed *feed, const char *elt, const char **attr)
{
	int i;
	bool getlink = false;
	char *url = NULL;

................................................................................
		free(url);
	}

	if (!strcmp(feed->xmlpath->data, "/feed/entry/category")) {
		for (i = 0; attr[i] != NULL; i++) {
			if (!strcmp(attr[i], "term")) {
				i++;
				add_tag(feed, attr[i]);
			}
		}
	}
}

static void XMLCALL
xml_startel(void *userdata, const char *elt, const char **attr)
................................................................................
	}
}

static void XMLCALL
xml_endel(void *userdata, const char *elt)
{
	struct feed *feed = (struct feed *)userdata;
	int i;

	if (!strcmp(feed->xmlpath->data, "/feed/entry/id") ||
	    !strcmp(feed->xmlpath->data, "/rss/channel/item/guid")) {
		sqlite3_bind_text(feed->stmt, 1, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
		sqlite3_bind_text(feed->tags, 1, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
	}
	if (!strcmp(feed->xmlpath->data, "/feed/entry/title") ||
................................................................................
		sqlite3_bind_int64(feed->stmt, 9, iso8601_to_time_t(utstring_body(feed->data)));
	if (!strcmp(feed->xmlpath->data, "/feed/entry/updated"))
		sqlite3_bind_int64(feed->stmt, 10, iso8601_to_time_t(utstring_body(feed->data)));
	if (!strcmp(feed->xmlpath->data, "/rss/channel/item/pubDate")) {
		sqlite3_bind_int64(feed->stmt, 9, rfc822_to_time_t(utstring_body(feed->data)));
		sqlite3_bind_int64(feed->stmt, 10, rfc822_to_time_t(utstring_body(feed->data)));
	}


	if (!strcmp(feed->xmlpath->data, "/feed/entry/content") ||
	    !strcmp(feed->xmlpath->data, "/rss/channel/item/content:encoded")) {
		sqlite3_bind_text(feed->stmt, 7, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
	}

	if (!strcmp(feed->xmlpath->data, "/rss/channel/item/link")) {
		sqlite3_bind_text(feed->stmt, 6, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
................................................................................
		sqlite3_bind_text(feed->stmt, 3, utstring_body(feed->blog_title), -1, SQLITE_STATIC);
		if (!feed->has_author)
			sqlite3_bind_text(feed->stmt, 5, utstring_body(feed->author), -1, SQLITE_TRANSIENT);
		if (sqlite3_step(feed->stmt) != SQLITE_DONE)
			warnx("sqlite3: grr: %s", sqlite3_errmsg(db));
		sqlite3_reset(feed->stmt);

		for (i = 0; i < feed->nbtags; i++) {

			sqlite3_bind_text(feed->tags, 2, feed->tag[i], -1, SQLITE_STATIC);
			sqlite3_step(feed->tags);
			free(feed->tag[i]);
		}
		free(feed->tag);
		feed->tag = NULL;
		feed->nbtags = 0;
		feed->has_author = false;
	}
	feed->xmlpath->size -= strlen(elt);
	feed->xmlpath->data[feed->xmlpath->size] = '\0';
	if (feed->xmlpath->data[feed->xmlpath->size - 1] != '/')
		warnx("invalid xml");

................................................................................
	struct feed feed;

	utstring_new(rawfeed);

	feed.type = NONE;
	feed.name = name;
	feed.has_author = false;
	feed.tag = NULL;
	feed.nbtags = 0;
	utstring_new(feed.blog_title);
	utstring_new(feed.author);
	feed.xmlpath = malloc(sizeof(struct buffer));
	feed.xmlpath->size = 0;
	feed.xmlpath->cap = BUFSIZ;
	feed.xmlpath->data = malloc(BUFSIZ);
	feed.xmlpath->data[0] = '\0';

	utstring_new(feed.data);

	if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO posts "
	    "(uid, name, blog_title, title, author, link, content, description, "
	    "date, updated, tags) values ("
	    "?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11);", -1, &feed.stmt, NULL) != SQLITE_OK) {
		warnx("sqlite: %s", sqlite3_errmsg(db));







>







 







|
<







 







<
<
<
<
<
<
<
<
<
<
<
<







 







|







 







|







 







>
>







 







|
>
|

<

|
<
<







 







<
<







>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
43
44
45
46
47
48
49
50

51
52
53
54
55
56
57
...
235
236
237
238
239
240
241












242
243
244
245
246
247
248
...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
...
354
355
356
357
358
359
360
361
362
363
364

365
366


367
368
369
370
371
372
373
...
412
413
414
415
416
417
418


419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <inttypes.h>
#include <sqlite3.h>
#include <expat.h>
#include <curl/curl.h>
#include <stdbool.h>

#include "utstring.h"
#include "utarray.h"
#include "cplanet.h"

typedef enum {
	NONE,
	RSS,
	ATOM,
	UNKNOWN
................................................................................
	const unsigned char *name;
	UT_string *blog_title;
	UT_string *author;
	bool has_author;
	UT_string *data;
	sqlite3_stmt *stmt;
	sqlite3_stmt *tags;
	UT_array *tag;

	struct buffer *xmlpath;
	feed_type type;
};

struct buffer {
	char *data;
	size_t size;
................................................................................
		warnx("Convert struct tm (from '%s') to time_t failed", s);
		return 0;
	}

	return t;
}













static void
parse_atom_el(struct feed *feed, const char *elt, const char **attr)
{
	int i;
	bool getlink = false;
	char *url = NULL;

................................................................................
		free(url);
	}

	if (!strcmp(feed->xmlpath->data, "/feed/entry/category")) {
		for (i = 0; attr[i] != NULL; i++) {
			if (!strcmp(attr[i], "term")) {
				i++;
				utarray_push_back(feed->tag, &attr[i]);
			}
		}
	}
}

static void XMLCALL
xml_startel(void *userdata, const char *elt, const char **attr)
................................................................................
	}
}

static void XMLCALL
xml_endel(void *userdata, const char *elt)
{
	struct feed *feed = (struct feed *)userdata;
	char **p;

	if (!strcmp(feed->xmlpath->data, "/feed/entry/id") ||
	    !strcmp(feed->xmlpath->data, "/rss/channel/item/guid")) {
		sqlite3_bind_text(feed->stmt, 1, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
		sqlite3_bind_text(feed->tags, 1, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
	}
	if (!strcmp(feed->xmlpath->data, "/feed/entry/title") ||
................................................................................
		sqlite3_bind_int64(feed->stmt, 9, iso8601_to_time_t(utstring_body(feed->data)));
	if (!strcmp(feed->xmlpath->data, "/feed/entry/updated"))
		sqlite3_bind_int64(feed->stmt, 10, iso8601_to_time_t(utstring_body(feed->data)));
	if (!strcmp(feed->xmlpath->data, "/rss/channel/item/pubDate")) {
		sqlite3_bind_int64(feed->stmt, 9, rfc822_to_time_t(utstring_body(feed->data)));
		sqlite3_bind_int64(feed->stmt, 10, rfc822_to_time_t(utstring_body(feed->data)));
	}
	if (!strcmp(feed->xmlpath->data, "/rss/channel/item/category"))
		utarray_push_back(feed->tag, &utstring_body(feed->data));
	if (!strcmp(feed->xmlpath->data, "/feed/entry/content") ||
	    !strcmp(feed->xmlpath->data, "/rss/channel/item/content:encoded")) {
		sqlite3_bind_text(feed->stmt, 7, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
	}

	if (!strcmp(feed->xmlpath->data, "/rss/channel/item/link")) {
		sqlite3_bind_text(feed->stmt, 6, utstring_body(feed->data), -1, SQLITE_TRANSIENT);
................................................................................
		sqlite3_bind_text(feed->stmt, 3, utstring_body(feed->blog_title), -1, SQLITE_STATIC);
		if (!feed->has_author)
			sqlite3_bind_text(feed->stmt, 5, utstring_body(feed->author), -1, SQLITE_TRANSIENT);
		if (sqlite3_step(feed->stmt) != SQLITE_DONE)
			warnx("sqlite3: grr: %s", sqlite3_errmsg(db));
		sqlite3_reset(feed->stmt);

		p = NULL;
		while (( p = (char **)utarray_next(feed->tag, p)) != NULL) {
			sqlite3_bind_text(feed->tags, 2, *p, -1, SQLITE_STATIC);
			sqlite3_step(feed->tags);

		}
		utarray_clear(feed->tag);


		feed->has_author = false;
	}
	feed->xmlpath->size -= strlen(elt);
	feed->xmlpath->data[feed->xmlpath->size] = '\0';
	if (feed->xmlpath->data[feed->xmlpath->size - 1] != '/')
		warnx("invalid xml");

................................................................................
	struct feed feed;

	utstring_new(rawfeed);

	feed.type = NONE;
	feed.name = name;
	feed.has_author = false;


	utstring_new(feed.blog_title);
	utstring_new(feed.author);
	feed.xmlpath = malloc(sizeof(struct buffer));
	feed.xmlpath->size = 0;
	feed.xmlpath->cap = BUFSIZ;
	feed.xmlpath->data = malloc(BUFSIZ);
	feed.xmlpath->data[0] = '\0';
	utarray_new(feed.tag, &ut_str_icd);
	utstring_new(feed.data);

	if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO posts "
	    "(uid, name, blog_title, title, author, link, content, description, "
	    "date, updated, tags) values ("
	    "?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11);", -1, &feed.stmt, NULL) != SQLITE_OK) {
		warnx("sqlite: %s", sqlite3_errmsg(db));