File: file_opml.c

package info (click to toggle)
hnb 1.9.18%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,680 kB
  • sloc: ansic: 8,117; makefile: 114
file content (224 lines) | stat: -rw-r--r-- 5,308 bytes parent folder | download | duplicates (4)
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
/*
 * file_opml.c -- generic xml import/export filters for hnb
 *
 * Copyright (C) 2001-2003 yvind Kols <pippin@users.sourceforge.net>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * This program 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, write to the Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "xml_tok.h"

#include "cli.h"
#include "tree.h"

#include "file.h"
#include "prefs.h"
#include "query.h"
#include "util_string.h"

#define indent(count,char)	{int j;for(j=0;j<count;j++)fprintf(file,char);}

/* *INDENT-OFF* */

static char *xmlquote[]={
	"<","&lt;",
	">","&gt;",
	"&","&amp;",
	"\"","&quot;",
	"'","&apos;",
	NULL
};

static char *xmlunquote[]={
	"&lt;","<",
	"&gt;",">",
	"&amp;","&",
	"&quot;","\"",
	"&apos;","'",
	NULL
};

/* *INDENT-ON* */

static void opml_export_nodes (FILE * file, Node *node, int level)
{
	while (node) {
		fprintf (file, "\n");
		indent (level, "\t");
		fprintf (file, "<outline");

		{Node_AttItem *att=node->attrib;
		 while(att){
		 	char *quoted=string_replace(att->data,xmlquote);
			fprintf (file, " %s=\"%s\"", att->name, quoted);
			free(quoted);
			att=att->next;
		 }		 
		}

		if (node_right (node)) {
			fprintf (file, ">");

			opml_export_nodes (file, node_right (node), level + 1);
			fprintf (file, "\n");
			indent (level, "\t");
			fprintf (file, "</outline>");
		} else {
			fprintf (file, " />");
		}

		node = node_down (node);
	}
}

static void* export_opml (int argc, char **argv, void *data)
{
	Node *node = (Node *) data;
	char *filename = argc>=2?argv[1]:"";
	FILE *file;

	if (!strcmp (filename, "-"))
		file = stdout;
	else
		file = fopen (filename, "w");

	if (!file) {
		cli_outfunf ("opml export, unable to open \"%s\"", filename);
		return node;
	}

	fprintf (file,
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
		"<?pos=\"%s\"?>\n"
		" <!-- generated by hnb %s (http://hnb.sourceforge.net/) -->\n",
	 	argc == 3 ? argv[2] : "1", VERSION);
	fprintf (file, "<opml version=\"1.0\">\n\
	<head>\n\
		<title>outline exported from hnb</title>\n\
		<dateCreated></dateCreated>\n\
		<dateModified></dateModified>\n\
		<ownerName></ownerName>\n\
		<ownerEmail></ownerEmail>\n\
		<expansionState></expansionState>\n\
		<vertScrollState></vertScrollState>\n\
		<windowTop>20</windowTop>\n\
		<windowLeft>20</windowLeft>\n\
		<windowBottom>200</windowBottom>\n\
		<windowRight>200</windowRight>\n\
		</head>\n\
	<body>\n");

	opml_export_nodes (file, node, 0);

	fprintf (file, "\n</body>\n</opml>\n");
	if (file != stdout)
		fclose (file);

	cli_outfunf ("opml export, wrote data to \"%s\"", filename);

	return node;
}

static void* import_opml (int argc, char **argv, void *data)
{
	Node *node = (Node *) data;
	char *filename = argc==2?argv[1]:"";
	char *rdata;
	int type;
	int in_body = 0;
	int in_outlineelement = 0;
	int level = -1;
	xml_tok_state *s;
	import_state_t ist;

	Node *tempnode=NULL;
	FILE *file;

	file = fopen (filename, "r");
	if (!file) {
		cli_outfunf ("opml import, unable to open \"%s\"", filename);
		return node;
	}
	s = xml_tok_init (file);
	init_import (&ist, node);

	while (((type = xml_tok_get (s, &rdata)) != t_eof) && (type != t_error)) {
		if (type == t_error) {
			cli_outfunf ("opml import error, parsing og '%s', line:%i %s", filename,
						 s->line_no, rdata);
			fclose (file);
			return node;
		}
		if (in_body) {
			if (type == t_tag && !strcmp (rdata, "outline")) {
				level++;
				in_outlineelement = 1;
				tempnode=node_new();
				continue;
			}
			if (in_outlineelement && type == t_att){
				char *att_name=strdup(rdata);
				char *unquoted;
				if(xml_tok_get(s,&rdata)!=t_val){
					cli_outfun("import_opml: hmm I don't think this is according to OPML,..");
				};
				unquoted=string_replace(rdata,xmlunquote);

				node_set(tempnode,att_name,unquoted);
				free(unquoted);
				free(att_name);
				continue;
			}
			if ((type == t_endtag || type == t_closeemptytag)
				&& !strcmp (rdata, "outline")) {
				in_outlineelement = 0;
				import_node(&ist, level, tempnode);	/* will free tempnode */
				tempnode=NULL;
			}
			if ((type == t_closetag || type == t_closeemptytag)
				&& !strcmp (rdata, "outline")) {
				level--;
				continue;
			}
		} else {
			if (type == t_tag && !strcmp (rdata, "body"))
				in_body = 1;
		}
	}

	if (node_getflag (node, F_temp))
		node = node_remove (node);	/* remove temporary node, if tree was empty */

	cli_outfunf ("opml import - imported \"%s\" %i lines", filename, s->line_no);
	xml_tok_cleanup (s);
	return node;
}


/*
!init_file_opml();
*/
void init_file_opml ()
{
	cli_add_command ("export_opml", export_opml, "<filename>");
	cli_add_command ("import_opml", import_opml, "<filename>");
}