-
Notifications
You must be signed in to change notification settings - Fork 0
/
impexp.c
179 lines (151 loc) · 3.98 KB
/
impexp.c
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
/*
$Header: /cvs/src/tdl/impexp.c,v 1.6.2.1 2004/01/07 00:09:05 richard Exp $
tdl - A console program for managing to-do lists
Copyright (C) 2001-2004 Richard P. Curnow
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 of the License, 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
*/
/* This code deals with the import and export commands. */
#include "tdl.h"
static struct node *clone_node(struct node *x, struct node *parent)/*{{{*/
{
struct node *r;
struct node *k;
r = new_node();
r->text = new_string(x->text);
r->parent = parent;
r->priority = x->priority;
r->arrived = x->arrived;
r->required_by = x->required_by;
r->done = x->done;
for (k = x->kids.next; k != (struct node *) &x->kids; k = k->chain.next) {
struct node *nc = clone_node(k, r);
prepend_node(nc, &r->kids);
}
return r;
}
/*}}}*/
static void set_arrived_time(struct node *x, time_t now)/*{{{*/
{
struct node *k;
x->arrived = now;
for (k = x->kids.next; k != (struct node *) &x->kids; k = k->chain.next) {
set_arrived_time(k, now);
}
}
/*}}}*/
int process_export(char **x)/*{{{*/
{
FILE *out;
char *filename;
int argc, i;
struct links data;
argc = count_args(x);
if (argc < 2) {
fprintf(stderr, "Need a filename and at least one index to write\n");
return -2;
}
filename = x[0];
data.prev = data.next = (struct node *) &data;
/* Build linked list of data to write */
for (i=1; i<argc; i++) {
struct node *n, *nn;
n = lookup_node(x[i], 0, NULL);
if (!n) return -1;
nn = clone_node(n, NULL);
prepend_node(nn, &data);
}
out = fopen(filename, "wb");
if (!out) {
fprintf(stderr, "Could not open file %s for writing\n", filename);
return -2;
}
write_database(out, &data);
fclose(out);
free_database(&data);
return 0;
}
/*}}}*/
int process_import(char **x)/*{{{*/
{
char *filename;
FILE *in;
struct links data;
struct node *n, *nn;
int argc;
int result;
argc = count_args(x);
if (argc < 1) {
fprintf(stderr, "Import requires a filename\n");
return -2;
}
filename = x[0];
in = fopen(filename, "rb");
if (!in) {
fprintf(stderr, "Could not open file %s for input\n", filename);
return -2;
}
result = read_database(in, &data);
fclose(in);
if (!result) {
/* read was OK */
for (n = data.next; n != (struct node *) &data; n = nn) {
nn = n->chain.next;
prepend_node(n, &top);
n->parent = NULL;
}
}
return result;
}
/*}}}*/
static int internal_copy_clone(struct links *l, char **x)/*{{{*/
{
int argc, i;
time_t now;
now = time(NULL);
argc = count_args(x);
if (argc < 1) {
fprintf(stderr, "Need at least one index to copy/clone\n");
return -2;
}
for (i=0; i<argc; i++) {
struct node *n, *nn;
n = lookup_node(x[i], 0, NULL);
if (!n) return -1;
nn = clone_node(n, NULL);
set_arrived_time(nn, now);
prepend_node(nn, l);
}
return 0;
}
/*}}}*/
int process_clone(char **x)/*{{{*/
{
struct node *narrow_top;
narrow_top = get_narrow_top();
return internal_copy_clone((narrow_top ? &narrow_top->kids : &top), x);
}
/*}}}*/
int process_copyto(char **x)/*{{{*/
{
struct node *parent;
if (count_args(x) < 1) {
fprintf(stderr, "Need a parent index to copy into\n");
return -2;
}
parent = lookup_node(x[0], 0, NULL);
if (!parent) {
return -1;
}
return internal_copy_clone(&parent->kids, x + 1);
}
/*}}}*/