File: ydpxml.c

package info (click to toggle)
libydpdict 1.0.4-3
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm, bullseye, buster, forky, sid, stretch, trixie
  • size: 200 kB
  • ctags: 73
  • sloc: ansic: 848; makefile: 30; python: 19; sh: 12
file content (96 lines) | stat: -rw-r--r-- 1,742 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#ifdef HAVE_LIBXML2
#include <libxml/parser.h>
#include <libxml/tree.h>
#endif

#include "ydpdict.h"

#define DICT_PATH "/usr/local/share/ydpdict"

int parse_xml(const char *xml, int validate)
{
#ifdef HAVE_LIBXML2
	xmlParserCtxtPtr ctxt;
	xmlDocPtr doc;

	ctxt = xmlNewParserCtxt();

	if (ctxt == NULL) {
		perror("xmlNewParserCtxt");
		exit(1);
	}

	doc = xmlCtxtReadMemory(ctxt, xml, strlen(xml), "", NULL, (validate) ? XML_PARSE_DTDVALID : 0);

	if (doc == NULL) {
		return 0;
	} else {
		if (validate && ctxt->valid == 0)
			return 0;

		xmlFreeDoc(doc);
	}

	xmlFreeParserCtxt(ctxt);
#endif

	return 1;
}

int main(int argc, char **argv)
{
	const char *cmd;
	ydpdict_t *dict;
	uint32_t i, j;
	int validate;

	if (argc > 1 && !strcmp(argv[1], "--valid"))
		validate = 1;
	else
		validate = 0;

	for (j = 0; j < 4; j++) {
		int count, dicts[4] = { 100, 101, 200, 201 };
		char dat[4096], idx[4096], prefix[128];

		snprintf(dat, sizeof(dat), DICT_PATH "/dict%d.dat", dicts[j]);
		snprintf(idx, sizeof(idx), DICT_PATH "/dict%d.idx", dicts[j]);

		if (!(dict = ydpdict_open(dat, idx, YDPDICT_ENCODING_UTF8))) {
			perror("ydpdict_open");
			return 1;
		}

		snprintf(prefix, sizeof(prefix), "%s", (strrchr(dat, '/')) ? (strrchr(dat, '/') + 1) : dat);

		count = ydpdict_get_count(dict);

		for (i = 0; i < count; i++) {
			unsigned char *tmp;
			FILE *f;
	
			printf("\r\033[K%s %d/%d %s", prefix, i, count, ydpdict_get_word(dict, i));
			fflush(stdout);

			tmp = ydpdict_read_xhtml(dict, i);

			if (!parse_xml(tmp, validate))
				printf(" ERROR\n");
			
			free(tmp);
		}

		printf("\r\033[K");
		fflush(stdout);
	
		ydpdict_close(dict);
	}
	
	return 0;
}