1 /*
2  * file.c -- utility functions for import/export hnb
3  *
4  * Copyright (C) 2001-2003 �yvind Kol�s <pippin@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2, or (at your option) any later
9  * version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 59
18  * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 
30 #include "tree.h"
31 #include "file.h"
32 #include "prefs.h"
33 #include "ui_cli.h"
34 
init_import(import_state_t * is,Node * node)35 void init_import (import_state_t * is, Node *node)
36 {
37 	is->npos = node;
38 	is->startlevel = nodes_left (node);
39 }
40 
41 /*
42  *
43  * @return the node inserted
44  * */
import_node_text(import_state_t * is,int level,char * data)45 Node *import_node_text (import_state_t * is, int level, char *data)
46 {
47 	int node_level;
48 
49 	level += is->startlevel;
50 
51 	while ((node_level = nodes_left (is->npos)) > level)
52 		is->npos = node_left (is->npos);
53 	if (node_level == level)
54 		is->npos = node_insert_down (is->npos);
55 	if (node_level < level)
56 		is->npos = node_insert_right (is->npos);
57 	node_set (is->npos, TEXT, data);
58 	return is->npos;
59 }
60 
61 
62 
63 /*
64  *
65  * @return the node inserted, no need to free the node afterwards
66  * */
import_node(import_state_t * is,int level,Node * node)67 Node *import_node (import_state_t * is, int level, Node *node)
68 {
69 	int node_level;
70 
71 	level += is->startlevel;
72 
73 	while ((node_level = nodes_left (is->npos)) > level)
74 		is->npos = node_left (is->npos);
75 	if (node_level == level)
76 		is->npos = node_insert_down (is->npos);
77 	if (node_level < level)
78 		is->npos = node_insert_right (is->npos);
79 	node_swap (node, is->npos);
80 	node_free (is->npos);
81 	is->npos = node;
82 	return is->npos;
83 }
84 
85 
86 
87 /* returns 1 if the first couple of lines of file contains 'xml' */
xml_check(char * filename)88 int xml_check (char *filename)
89 {
90 	FILE *file;
91 	char buf[bufsize];
92 	int j;
93 
94 	file = fopen (filename, "r");
95 	if (file == NULL)
96 		return -1;
97 
98 	for (j = 0; j < 2; j++) {
99 		if (fgets (buf, bufsize, file) == NULL) {
100 			fclose (file);
101 			return 0;
102 		}
103 		if (strstr (buf, "xml") != 0) {
104 			fclose (file);
105 			return 1;
106 		}
107 	}
108 	fclose (file);
109 	return 0;
110 }
111 
112 /* returns the node number stored in the comment, if available  */
xml_getpos(char * filename)113 int xml_getpos (char *filename)
114 {
115 	FILE *file;
116 	char buf[bufsize];
117 	char *s;
118 	int j;
119 
120 	file = fopen (filename, "r");
121 	if (file == NULL)
122 		return -1;
123 
124 	for (j = 0; j < 2; j++) {
125 		if (fgets (buf, bufsize, file) == NULL) {
126 			fclose (file);
127 			return 0;
128 		}
129 		if ((s = strstr (buf, "<?pos=\""))) {
130 			fclose (file);
131 
132 			return atoi (&s[8]);
133 		}
134 	}
135 	fclose (file);
136 	return -1;
137 }
138 
139 
140 /*returns 1 if file exists*/
file_check(char * filename)141 int file_check (char *filename)
142 {
143 	FILE *file;
144 
145 	file = fopen (filename, "r");
146 	if (file == NULL)
147 		return 0;
148 	fclose (file);
149 	return 1;
150 }
151 
152 
cmd_save(int argc,char ** argv,void * data)153 static uint64_t cmd_save (int argc,char **argv, void *data)
154 {
155 	Node *pos = (Node *) data;
156 
157 	if (prefs.db_file[0] != (char) 255) { /* magic value of tutorial */
158 		{
159 			char buf[4096];
160 
161 			if (!strcmp(prefs.format,"hnb") || !strcmp(prefs.format,"opml")) {
162 				sprintf (buf, "export_%s %s %i", prefs.format, prefs.db_file,
163 						 node_no (pos) - 1);
164 			} else {
165 				sprintf (buf, "export_%s %s", prefs.format, prefs.db_file);
166 			}
167 			docmd (node_root (pos), buf);
168 		}
169 	}
170 	return PTR_TO_UINT64(pos);
171 }
172 
cmd_revert(int argc,char ** argv,void * data)173 static uint64_t cmd_revert (int argc,char **argv, void *data)
174 {
175 	Node *pos = (Node *) data;
176 
177 	if (prefs.db_file[0] != (char) 255) {
178 		{
179 			char buf[4096];
180 
181 			sprintf (buf, "import_%s %s", prefs.format, prefs.db_file);
182 			node_free(pos);
183 			pos=tree_new();
184 
185 			pos=docmd (pos, buf);
186 		}
187 	}
188 	return PTR_TO_UINT64(pos);
189 }
190 
191 
192 /*
193 !init_file();
194 */
init_file()195 void init_file ()
196 {
197 	cli_add_command ("save", cmd_save, "");
198 	cli_add_help ("save", "Saves the data");
199 
200 	cli_add_command ("revert", cmd_revert, "");
201 	cli_add_help ("revert", "Revert to last saved version");
202 }
203