summaryrefslogtreecommitdiff
path: root/data/reg2xml.c
blob: 5bd3b3186dd70b451ca134bb8b2c3347277cec41 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * reg2xml.c
 * Copyright (C) 2011-2012 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <akira@tagoh.org>
 * 
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <glib.h>
#include <libxml/tree.h>
#include "lt-utils.h"

/*< private >*/
static gchar *
_drop_crlf(gchar *string)
{
	gsize len, i;

	g_return_val_if_fail (string != NULL, NULL);
	g_return_val_if_fail (string[0] != 0, NULL);

	len = strlen(string);
	for (i = len - 1; i > 0; i--) {
		if (string[i] == '\r' ||
		    string[i] == '\n') {
			string[i] = 0;
		} else {
			break;
		}
	}

	return string;
}

static gboolean
_parse(const gchar *filename,
       xmlNodePtr   root)
{
	FILE *fp;
	gchar buffer[1024];
	gboolean in_entry = FALSE;
	xmlNodePtr ent = NULL;

	if ((fp = fopen(filename, "rb")) == NULL) {
		g_printerr("Unable to open %s\n", filename);
		return FALSE;
	}
	while (1) {
		fgets(buffer, 1024, fp);
		if (feof(fp))
			break;
		_drop_crlf(buffer);
		if (g_strcmp0(buffer, "%%") == 0) {
			if (in_entry) {
				if (ent) {
					xmlAddChild(root, ent);
				}
				ent = NULL;
			}
			in_entry = TRUE;
		} else {
			if (!in_entry) {
				/* ignore it */
				continue;
			}
			if (strncmp(buffer, "Type: ", 6) == 0) {
				if (ent) {
					g_warning("Duplicate entry type: line = '%s', current type = '%s'",
						  buffer, ent->name);
				} else {
					ent = xmlNewNode(NULL, (const xmlChar *)&buffer[6]);
				}
			} else {
				if (!ent) {
					g_warning("No entry type: line = '%s'",
						  buffer);
					in_entry = FALSE;
				} else {
					gchar **tokens, *tag;
					fpos_t pos;
					gsize len;

				  multiline:
					fgetpos(fp, &pos);
					len = strlen(buffer);
					fgets(&buffer[len], 1024 - len, fp);
					if (buffer[len] == ' ') {
						gsize l, i;

						_drop_crlf(&buffer[len]);
						l = strlen(&buffer[len]);
						for (i = 1; i < l; i++) {
							if (buffer[len + i] != ' ')
								break;
						}
						if (i > 1) {
							memmove(&buffer[len + 1], &buffer[len + i], l - i);
						}
						goto multiline;
					} else {
						buffer[len] = 0;
						fsetpos(fp, &pos);
					}
					tokens = g_strsplit(buffer, ": ", 2);
					tag = g_strdup(tokens[0]);
					xmlNewChild(ent, NULL,
						    (const xmlChar *)lt_strlower(tag),
						    (const xmlChar *)tokens[1]);
					g_strfreev(tokens);
					g_free(tag);
				}
			}
		}
	}

	return TRUE;
}

static gboolean
_output_xml(const gchar  *filename,
	    xmlChar     **output,
	    int          *size)
{
	xmlDocPtr doc;
	xmlNodePtr root;
	gboolean retval;

	doc = xmlNewDoc((const xmlChar *)"1.0");
	doc->encoding = xmlStrdup((const xmlChar *)"UTF-8");
	root = xmlNewDocNode(doc, NULL,
			     (const xmlChar *)"registry",
			     NULL);
	xmlDocSetRootElement(doc, root);

	if (!(retval = _parse(filename, root)))
		goto bail;

	xmlDocDumpFormatMemory(doc, output, size, 1);
  bail:
	xmlFreeDoc(doc);

	return retval;
}

/*< public >*/
int
main(int    argc,
     char **argv)
{
	xmlChar *xml;
	int size;

	if (argc < 2)
		return 1;

	if (!_output_xml(argv[1], &xml, &size))
		return 1;

	g_print("%s\n", xml);

	return 0;
}