]>
Commit | Line | Data |
---|---|---|
7024e37b JW |
1 | /* |
2 | $Header: /cvs/src/tdl/io.c,v 1.5.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 | #include <string.h> | |
23 | #include "tdl.h" | |
24 | ||
25 | #define MAGIC1 0x99bb0001L | |
26 | ||
27 | static unsigned int count_length(struct links *x)/*{{{*/ | |
28 | { | |
29 | int n = 0; | |
30 | struct node *y; | |
31 | for (y = x->next; &y->chain != x; y = y->chain.next) n++; | |
32 | return n; | |
33 | } | |
34 | /*}}}*/ | |
35 | static unsigned int count_kids(struct node *x)/*{{{*/ | |
36 | { | |
37 | return count_length(&x->kids); | |
38 | } | |
39 | /*}}}*/ | |
40 | static void write_int(unsigned int n, FILE *out)/*{{{*/ | |
41 | { | |
42 | unsigned int a, b, c, d; | |
43 | a = (n >> 24) & 255; | |
44 | b = (n >> 16) & 255; | |
45 | c = (n >> 8) & 255; | |
46 | d = (n ) & 255; | |
47 | fputc(a, out); | |
48 | fputc(b, out); | |
49 | fputc(c, out); | |
50 | fputc(d, out); | |
51 | } | |
52 | /*}}}*/ | |
53 | /*{{{ static unsigned int read_int(FILE *in)*/ | |
54 | static unsigned int read_int(FILE *in) | |
55 | { | |
56 | ||
57 | unsigned int a, b, c, d; | |
58 | a = fgetc(in); | |
59 | b = fgetc(in); | |
60 | c = fgetc(in); | |
61 | d = fgetc(in); | |
62 | return ((a & 0xff) << 24) + ((b & 0xff) << 16) + | |
63 | ((c & 0xff) << 8) + (d & 0xff); | |
64 | } | |
65 | ||
66 | /*}}}*/ | |
67 | static void write_node(struct node *x, FILE *out)/*{{{*/ | |
68 | { | |
69 | int len, n_kids; | |
70 | struct node *kid; | |
71 | ||
72 | len = strlen(x->text); | |
73 | n_kids = count_kids(x); | |
74 | ||
75 | write_int(x->arrived, out); | |
76 | write_int(x->required_by, out); | |
77 | write_int(x->done, out); | |
78 | fputc(x->priority, out); | |
79 | write_int(len, out); | |
80 | fwrite(x->text, 1, len, out); | |
81 | ||
82 | write_int(n_kids, out); | |
83 | for (kid = x->kids.next; kid != (struct node *) &x->kids; kid = kid->chain.next) | |
84 | { | |
85 | write_node(kid, out); | |
86 | } | |
87 | } | |
88 | /*}}}*/ | |
89 | void write_database(FILE *out, struct links *from)/*{{{*/ | |
90 | { | |
91 | int n_kids; | |
92 | struct node *kid; | |
93 | ||
94 | n_kids = count_length(from); | |
95 | ||
96 | write_int(MAGIC1, out); | |
97 | write_int(n_kids, out); | |
98 | for (kid = from->next; kid != (struct node *) from; kid = kid->chain.next) | |
99 | { | |
100 | write_node(kid, out); | |
101 | } | |
102 | } | |
103 | /*}}}*/ | |
104 | /*{{{ static struct node *read_node(FILE *in)*/ | |
105 | static struct node *read_node(FILE *in) | |
106 | { | |
107 | struct node *r = new_node(); | |
108 | int len, n_kids, i; | |
109 | ||
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; | |
116 | } | |
117 | len = read_int(in); | |
118 | r->text = new_array(char, len+1); | |
119 | fread(r->text, 1, len, in); | |
120 | r->text[len] = 0; | |
121 | ||
122 | n_kids = read_int(in); | |
123 | ||
124 | for (i=0; i<n_kids; i++) { | |
125 | struct node *nn; | |
126 | nn = read_node(in); | |
127 | prepend_child(nn, r); | |
128 | } | |
129 | ||
130 | return r; | |
131 | } | |
132 | ||
133 | /*}}}*/ | |
134 | /*{{{ void read_database(FILE *in)*/ | |
135 | int read_database(FILE *in, struct links *to) | |
136 | { | |
137 | int n_kids, i; | |
138 | unsigned int magic; | |
139 | ||
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, ... */ | |
143 | ||
144 | to->prev = (struct node *) to; | |
145 | to->next = (struct node *) to; | |
146 | ||
147 | if (feof(in)) { | |
148 | fprintf(stderr, "Can't read anything from database\n"); | |
149 | return -1; | |
150 | } | |
151 | ||
152 | magic = read_int(in); | |
153 | if (magic != MAGIC1) { | |
154 | fprintf(stderr, "Cannot parse database, wrong magic number\n"); | |
155 | return -1; | |
156 | } | |
157 | n_kids = read_int(in); | |
158 | for (i=0; i<n_kids; i++) { | |
159 | struct node *nn; | |
160 | nn = read_node(in); | |
161 | prepend_node(nn, to); | |
162 | } | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | /*}}}*/ |