2 $Header: /cvs/src/tdl/impexp.c,v 1.6.2.1 2004/01/07 00:09:05 richard Exp $
4 tdl - A console program for managing to-do lists
5 Copyright (C) 2001-2004 Richard P. Curnow
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /* This code deals with the import and export commands. */
26 static struct node *clone_node(struct node *x, struct node *parent)/*{{{*/
31 r->text = new_string(x->text);
33 r->priority = x->priority;
34 r->arrived = x->arrived;
35 r->required_by = x->required_by;
38 for (k = x->kids.next; k != (struct node *) &x->kids; k = k->chain.next) {
39 struct node *nc = clone_node(k, r);
40 prepend_node(nc, &r->kids);
46 static void set_arrived_time(struct node *x, time_t now)/*{{{*/
50 for (k = x->kids.next; k != (struct node *) &x->kids; k = k->chain.next) {
51 set_arrived_time(k, now);
55 int process_export(char **x)/*{{{*/
65 fprintf(stderr, "Need a filename and at least one index to write\n");
71 data.prev = data.next = (struct node *) &data;
73 /* Build linked list of data to write */
74 for (i=1; i<argc; i++) {
76 n = lookup_node(x[i], 0, NULL);
78 nn = clone_node(n, NULL);
79 prepend_node(nn, &data);
82 out = fopen(filename, "wb");
84 fprintf(stderr, "Could not open file %s for writing\n", filename);
88 write_database(out, &data);
96 int process_import(char **x)/*{{{*/
105 argc = count_args(x);
107 fprintf(stderr, "Import requires a filename\n");
112 in = fopen(filename, "rb");
114 fprintf(stderr, "Could not open file %s for input\n", filename);
118 result = read_database(in, &data);
123 for (n = data.next; n != (struct node *) &data; n = nn) {
125 prepend_node(n, &top);
134 static int internal_copy_clone(struct links *l, char **x)/*{{{*/
141 argc = count_args(x);
143 fprintf(stderr, "Need at least one index to copy/clone\n");
147 for (i=0; i<argc; i++) {
149 n = lookup_node(x[i], 0, NULL);
151 nn = clone_node(n, NULL);
152 set_arrived_time(nn, now);
159 int process_clone(char **x)/*{{{*/
161 struct node *narrow_top;
162 narrow_top = get_narrow_top();
163 return internal_copy_clone((narrow_top ? &narrow_top->kids : &top), x);
166 int process_copyto(char **x)/*{{{*/
169 if (count_args(x) < 1) {
170 fprintf(stderr, "Need a parent index to copy into\n");
173 parent = lookup_node(x[0], 0, NULL);
177 return internal_copy_clone(&parent->kids, x + 1);