2 $Header: /cvs/src/tdl/io.c,v 1.5.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
25 #define MAGIC1 0x99bb0001L
27 static unsigned int count_length(struct links *x)/*{{{*/
31 for (y = x->next; &y->chain != x; y = y->chain.next) n++;
35 static unsigned int count_kids(struct node *x)/*{{{*/
37 return count_length(&x->kids);
40 static void write_int(unsigned int n, FILE *out)/*{{{*/
42 unsigned int a, b, c, d;
53 /*{{{ static unsigned int read_int(FILE *in)*/
54 static unsigned int read_int(FILE *in)
57 unsigned int a, b, c, d;
62 return ((a & 0xff) << 24) + ((b & 0xff) << 16) +
63 ((c & 0xff) << 8) + (d & 0xff);
67 static void write_node(struct node *x, FILE *out)/*{{{*/
72 len = strlen(x->text);
73 n_kids = count_kids(x);
75 write_int(x->arrived, out);
76 write_int(x->required_by, out);
77 write_int(x->done, out);
78 fputc(x->priority, out);
80 fwrite(x->text, 1, len, out);
82 write_int(n_kids, out);
83 for (kid = x->kids.next; kid != (struct node *) &x->kids; kid = kid->chain.next)
89 void write_database(FILE *out, struct links *from)/*{{{*/
94 n_kids = count_length(from);
96 write_int(MAGIC1, out);
97 write_int(n_kids, out);
98 for (kid = from->next; kid != (struct node *) from; kid = kid->chain.next)
100 write_node(kid, out);
104 /*{{{ static struct node *read_node(FILE *in)*/
105 static struct node *read_node(FILE *in)
107 struct node *r = new_node();
110 r->arrived = read_int(in);
111 r->required_by = read_int(in);
112 r->done = read_int(in);
113 r->priority = fgetc(in);
114 if ((r->priority < PRI_UNKNOWN) || (r->priority > PRI_URGENT)) {
115 r->priority = PRI_NORMAL;
118 r->text = new_array(char, len+1);
119 fread(r->text, 1, len, in);
122 n_kids = read_int(in);
124 for (i=0; i<n_kids; i++) {
127 prepend_child(nn, r);
134 /*{{{ void read_database(FILE *in)*/
135 int read_database(FILE *in, struct links *to)
140 /* FIXME : Ought to clear any existing database. For initial version this
141 * won't happen (only one load per invocation), and even later, we might just
142 * take the memory leak. But for safety, ... */
144 to->prev = (struct node *) to;
145 to->next = (struct node *) to;
148 fprintf(stderr, "Can't read anything from database\n");
152 magic = read_int(in);
153 if (magic != MAGIC1) {
154 fprintf(stderr, "Cannot parse database, wrong magic number\n");
157 n_kids = read_int(in);
158 for (i=0; i<n_kids; i++) {
161 prepend_node(nn, to);