Debian Codegraph @

libidu/idwrite.c

raw symbols SCIP
1 /* idwrite.c -- functions to write ID database files
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <obstack.h>
22 #include <xalloc.h>
23 #include <error.h>
24
25 #include "idfile.h"
26 #include "ignore-value.h"
27 #include "idu-hash.h"
28 #include "xnls.h"
29
30 static int file_link_qsort_compare (void const *x, void const *y);
31
32
33 /****************************************************************************/
34 /* Serialize and write a file_link hierarchy. */
35
36 void
37 serialize_file_links (struct idhead *idhp)
38 {
39 struct file_link **flinks_0;
40 struct file_link **flinks;
41 struct file_link **end;
42 struct file_link **parents_0;
43 struct file_link **parents;
44 unsigned long parent_index = 0;
45 int max_link = 0;
46
47 flinks_0 = (struct file_link **) hash_dump (&idhp->idh_file_link_table,
48 0, file_link_qsort_compare);
49 end = &flinks_0[idhp->idh_file_link_table.ht_fill];
50 parents = parents_0 = xmalloc (sizeof(struct file_link *) * idhp->idh_file_link_table.ht_fill);
51 for (flinks = flinks_0; flinks < end; flinks++)
52 {
53 struct file_link *flink = *flinks;
54 int name_length;
55
56 if (!(flink->fl_flags & FL_USED))
57 break;
58 name_length = strlen (flink->fl_name);
59 if (name_length > max_link)
60 max_link = name_length;
61 io_write (idhp->idh_FILE, flink->fl_name, 0, IO_TYPE_STR);
62 io_write (idhp->idh_FILE, &flink->fl_flags, sizeof (flink->fl_flags), IO_TYPE_INT);
63 io_write (idhp->idh_FILE, (IS_ROOT_FILE_LINK (flink)
64 ? &parent_index : &flink->fl_parent->fl_index),
65 FL_PARENT_INDEX_BYTES, IO_TYPE_INT);
66 *parents++ = flink->fl_parent; /* save parent link before clobbering */
67 flink->fl_index = parent_index++;
68 }
69 /* restore parent links */
70 for ((flinks = flinks_0), (parents = parents_0); flinks < end; flinks++)
71 {
72 struct file_link *flink = *flinks;
73 if (!(flink->fl_flags & FL_USED))
74 break;
75 flink->fl_parent = *parents++;
76 }
77 free (parents_0);
78 free (flinks_0);
79 idhp->idh_max_link = max_link + 1;
80 idhp->idh_file_links = parent_index;
81 idhp->idh_files = idhp->idh_member_file_table.ht_fill;
82 }
83
84 /* Collation sequence:
85 - Used before unused.
86 - Among used: breadth-first (dirs before files, parent dirs before children)
87 - Among files: collate by mf_index. */
88
89 static int
90 file_link_qsort_compare (void const *x, void const *y)
91 {
92 struct file_link const *flx = *(struct file_link const *const *) x;
93 struct file_link const *fly = *(struct file_link const *const *) y;
94 unsigned int x_flags = flx->fl_flags;
95 unsigned int y_flags = fly->fl_flags;
96 int result;
97
98 result = (y_flags & FL_USED) - (x_flags & FL_USED);
99 if (result)
100 return result;
101 if (!(x_flags & FL_USED)) /* If neither link is used, we don't care... */
102 return 0;
103 result = (y_flags & FL_TYPE_DIR) - (x_flags & FL_TYPE_DIR);
104 if (result)
105 return result;
106 result = (y_flags & FL_TYPE_MASK) - (x_flags & FL_TYPE_MASK);
107 if (result)
108 return result;
109 if (FL_IS_FILE (x_flags))
110 {
111 struct member_file *x_member = find_member_file (flx);
112 struct member_file *y_member = find_member_file (fly);
113 return x_member->mf_index - y_member->mf_index;
114 }
115 else
116 {
117 int x_depth = links_depth (flx);
118 int y_depth = links_depth (fly);
119 return (x_depth - y_depth);
120 }
121 }
122
123
124 /****************************************************************************/
125
126 int
127 write_idhead (struct idhead *idhp)
128 {
129 return io_idhead (idhp->idh_FILE, io_write, idhp);
130 }
131
132 int
133 io_write (FILE *output_FILE, void *addr, unsigned int size, int io_type)
134 {
135 if (io_type == IO_TYPE_INT || size == 1)
136 {
137 switch (size)
138 {
139 case 4:
140 putc (*(unsigned long *)addr, output_FILE);
141 putc (*(unsigned long *)addr >> 010, output_FILE);
142 putc (*(unsigned long *)addr >> 020, output_FILE);
143 putc (*(unsigned long *)addr >> 030, output_FILE);
144 break;
145 case 3:
146 putc (*(unsigned long *)addr, output_FILE);
147 putc (*(unsigned long *)addr >> 010, output_FILE);
148 putc (*(unsigned long *)addr >> 020, output_FILE);
149 break;
150 case 2:
151 putc (*(unsigned short *)addr, output_FILE);
152 putc (*(unsigned short *)addr >> 010, output_FILE);
153 break;
154 case 1:
155 putc (*(unsigned char *)addr, output_FILE);
156 break;
157 default:
158 error (EXIT_FAILURE, 0, _("unsupported size in io_write (): %u"), size);
159 }
160 }
161 else if (io_type == IO_TYPE_STR)
162 {
163 fputs (addr, output_FILE);
164 putc ('\0', output_FILE);
165 }
166 else if (io_type == IO_TYPE_FIX)
167 ignore_value (fwrite (addr, size, 1, output_FILE));
168 else
169 error (0, 0, _("unknown I/O type: %d"), io_type);
170 return size;
171 }