]> Joshua Wise's Git repositories - tdl.git/blob - impexp.c
Sort list by a priority function in 'tdll'.
[tdl.git] / impexp.c
1 /*
2    $Header: /cvs/src/tdl/impexp.c,v 1.6.2.1 2004/01/07 00:09:05 richard Exp $
3   
4    tdl - A console program for managing to-do lists
5    Copyright (C) 2001-2004  Richard P. Curnow
6
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.
11
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.
16
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
20    */
21
22 /* This code deals with the import and export commands. */
23
24 #include "tdl.h"
25
26 static struct node *clone_node(struct node *x, struct node *parent)/*{{{*/
27 {
28   struct node *r;
29   struct node *k;
30   r = new_node();
31   r->text = new_string(x->text);
32   r->parent = parent;
33   r->priority = x->priority;
34   r->arrived = x->arrived;
35   r->required_by = x->required_by;
36   r->done = x->done;
37
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);
41   }
42
43   return r;
44 }
45 /*}}}*/
46 static void set_arrived_time(struct node *x, time_t now)/*{{{*/
47 {
48   struct node *k;
49   x->arrived = now;
50   for (k = x->kids.next; k != (struct node *) &x->kids; k = k->chain.next) {
51     set_arrived_time(k, now);
52   }
53 }
54 /*}}}*/
55 int process_export(char **x)/*{{{*/
56 {
57   FILE *out;
58   char *filename;
59   int argc, i;
60   struct links data;
61
62   argc = count_args(x);
63
64   if (argc < 2) {
65     fprintf(stderr, "Need a filename and at least one index to write\n");
66     return -2;
67   }
68   
69   filename = x[0];
70
71   data.prev = data.next = (struct node *) &data;
72
73   /* Build linked list of data to write */
74   for (i=1; i<argc; i++) {
75     struct node *n, *nn;
76     n = lookup_node(x[i], 0, NULL);
77     if (!n) return -1;
78     nn = clone_node(n, NULL);
79     prepend_node(nn, &data);
80   }
81   
82   out = fopen(filename, "wb");
83   if (!out) {
84     fprintf(stderr, "Could not open file %s for writing\n", filename);
85     return -2;
86   }
87
88   write_database(out, &data);
89     
90   fclose(out);
91   free_database(&data);
92   return 0;
93
94 }
95 /*}}}*/
96 int process_import(char **x)/*{{{*/
97 {
98   char *filename;
99   FILE *in;
100   struct links data;
101   struct node *n, *nn;
102   int argc;
103   int result;
104
105   argc = count_args(x);
106   if (argc < 1) {
107     fprintf(stderr, "Import requires a filename\n");
108     return -2;
109   }
110
111   filename = x[0];
112   in = fopen(filename, "rb");
113   if (!in) {
114     fprintf(stderr, "Could not open file %s for input\n", filename);
115     return -2;
116   }
117
118   result = read_database(in, &data);
119   fclose(in);
120
121   if (!result) {
122     /* read was OK */
123     for (n = data.next; n != (struct node *) &data; n = nn) {
124       nn = n->chain.next;
125       prepend_node(n, &top);
126       n->parent = NULL;
127     }
128   }
129
130   return result;
131
132 }
133 /*}}}*/
134 static int internal_copy_clone(struct links *l, char **x)/*{{{*/
135 {
136   int argc, i;
137   time_t now;
138
139   now = time(NULL);
140
141   argc = count_args(x);
142   if (argc < 1) {
143     fprintf(stderr, "Need at least one index to copy/clone\n");
144     return -2;
145   }
146
147   for (i=0; i<argc; i++) {
148     struct node *n, *nn;
149     n = lookup_node(x[i], 0, NULL);
150     if (!n) return -1;
151     nn = clone_node(n, NULL);
152     set_arrived_time(nn, now);
153     prepend_node(nn, l);
154   }
155
156   return 0;
157 }
158 /*}}}*/
159 int process_clone(char **x)/*{{{*/
160 {
161   struct node *narrow_top;
162   narrow_top = get_narrow_top();
163   return internal_copy_clone((narrow_top ? &narrow_top->kids : &top), x);
164 }
165 /*}}}*/
166 int process_copyto(char **x)/*{{{*/
167 {
168   struct node *parent;
169   if (count_args(x) < 1) {
170     fprintf(stderr, "Need a parent index to copy into\n");
171     return -2;
172   }
173   parent = lookup_node(x[0], 0, NULL);
174   if (!parent) {
175     return -1;
176   }
177   return internal_copy_clone(&parent->kids, x + 1);
178 }
179 /*}}}*/
This page took 0.032968 seconds and 4 git commands to generate.