| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <errno.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <obstack.h> |
| 25 |
#include <xalloc.h> |
| 26 |
#include <error.h> |
| 27 |
|
| 28 |
#include "idu-hash.h" |
| 29 |
#include "idfile.h" |
| 30 |
#include "iduglobal.h" |
| 31 |
#include "ignore-value.h" |
| 32 |
#include "xnls.h" |
| 33 |
|
| 34 |
static int fgets0 (char *buf0, int size, FILE *in_FILE); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
struct file_link ** |
| 44 |
read_id_file (char const *id_file_name, struct idhead *idhp) |
| 45 |
{ |
| 46 |
struct file_link **flinkv = maybe_read_id_file (id_file_name, idhp); |
| 47 |
if (flinkv) |
| 48 |
return flinkv; |
| 49 |
error (EXIT_FAILURE, errno, _("can't open `%s'"), id_file_name); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct file_link ** |
| 59 |
maybe_read_id_file (char const *id_file_name, struct idhead *idhp) |
| 60 |
{ |
| 61 |
obstack_init (&idhp->idh_file_link_obstack); |
| 62 |
idhp->idh_FILE = fopen (id_file_name, "rb"); |
| 63 |
if (idhp->idh_FILE == 0) |
| 64 |
return 0; |
| 65 |
|
| 66 |
read_idhead (idhp); |
| 67 |
if (idhp->idh_magic[0] != IDH_MAGIC_0 || idhp->idh_magic[1] != IDH_MAGIC_1) |
| 68 |
error (EXIT_FAILURE, 0, _("`%s' is not an ID file! (bad magic #)"), id_file_name); |
| 69 |
if (idhp->idh_version != IDH_VERSION) |
| 70 |
error (EXIT_FAILURE, 0, _("`%s' is version %d, but I only grok version %d"), |
| 71 |
id_file_name, idhp->idh_version, IDH_VERSION); |
| 72 |
|
| 73 |
fseek (idhp->idh_FILE, idhp->idh_flinks_offset, 0); |
| 74 |
return deserialize_file_links (idhp); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
struct file_link ** |
| 83 |
deserialize_file_links (struct idhead *idhp) |
| 84 |
{ |
| 85 |
struct file_link **flinks_0 = xmalloc (sizeof(struct file_link *) * idhp->idh_file_links); |
| 86 |
struct file_link **flinks = flinks_0; |
| 87 |
struct file_link **members_0 = xmalloc (sizeof(struct file_link *) * idhp->idh_file_links + 1); |
| 88 |
struct file_link **members = members_0; |
| 89 |
struct file_link *flink; |
| 90 |
struct file_link **slot; |
| 91 |
int i; |
| 92 |
|
| 93 |
for (i = 0; i < idhp->idh_file_links; i++) |
| 94 |
{ |
| 95 |
unsigned long parent_index; |
| 96 |
int c; |
| 97 |
|
| 98 |
obstack_blank (&idhp->idh_file_link_obstack, offsetof (struct file_link, fl_name)); |
| 99 |
if (obstack_room (&idhp->idh_file_link_obstack) >= idhp->idh_max_link) |
| 100 |
do |
| 101 |
{ |
| 102 |
c = getc (idhp->idh_FILE); |
| 103 |
obstack_1grow_fast (&idhp->idh_file_link_obstack, c); |
| 104 |
} |
| 105 |
while (c); |
| 106 |
else |
| 107 |
do |
| 108 |
{ |
| 109 |
c = getc (idhp->idh_FILE); |
| 110 |
obstack_1grow (&idhp->idh_file_link_obstack, c); |
| 111 |
} |
| 112 |
while (c); |
| 113 |
flink = (struct file_link *) obstack_finish (&idhp->idh_file_link_obstack); |
| 114 |
*flinks = flink; |
| 115 |
io_read (idhp->idh_FILE, &flink->fl_flags, sizeof (flink->fl_flags), IO_TYPE_INT); |
| 116 |
io_read (idhp->idh_FILE, &parent_index, FL_PARENT_INDEX_BYTES, IO_TYPE_INT); |
| 117 |
flink->fl_parent = flinks_0[parent_index]; |
| 118 |
slot = (struct file_link **) hash_find_slot (&idhp->idh_file_link_table, flink); |
| 119 |
if (HASH_VACANT (*slot)) |
| 120 |
hash_insert_at (&idhp->idh_file_link_table, flink, slot); |
| 121 |
else |
| 122 |
{ |
| 123 |
obstack_free (&idhp->idh_file_link_obstack, flink); |
| 124 |
(*slot)->fl_flags = flink->fl_flags; |
| 125 |
flink = *flinks = *slot; |
| 126 |
} |
| 127 |
flinks++; |
| 128 |
if (flink->fl_flags & FL_MEMBER) |
| 129 |
*members++ = flink; |
| 130 |
} |
| 131 |
free (flinks_0); |
| 132 |
*members = 0; |
| 133 |
return members_0; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
int |
| 140 |
read_idhead (struct idhead *idhp) |
| 141 |
{ |
| 142 |
return io_idhead (idhp->idh_FILE, io_read, idhp); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static int |
| 150 |
fgets0 (char *buf0, int size, FILE * in_FILE) |
| 151 |
{ |
| 152 |
char *buf; |
| 153 |
int c; |
| 154 |
char *end; |
| 155 |
|
| 156 |
buf = buf0; |
| 157 |
end = &buf[size]; |
| 158 |
while ((c = getc (in_FILE)) > 0 && buf < end) |
| 159 |
*buf++ = c; |
| 160 |
*buf = '\0'; |
| 161 |
return (buf - buf0); |
| 162 |
} |
| 163 |
|
| 164 |
int |
| 165 |
io_read (FILE *input_FILE, void *addr, unsigned int size, int io_type) |
| 166 |
{ |
| 167 |
if (io_type == IO_TYPE_INT || size == 1) |
| 168 |
{ |
| 169 |
switch (size) |
| 170 |
{ |
| 171 |
case 4: |
| 172 |
*(unsigned long *)addr = getc (input_FILE); |
| 173 |
*(unsigned long *)addr += getc (input_FILE) << 010; |
| 174 |
*(unsigned long *)addr += getc (input_FILE) << 020; |
| 175 |
*(unsigned long *)addr += getc (input_FILE) << 030; |
| 176 |
break; |
| 177 |
case 3: |
| 178 |
*(unsigned long *)addr = getc (input_FILE); |
| 179 |
*(unsigned long *)addr += getc (input_FILE) << 010; |
| 180 |
*(unsigned long *)addr += getc (input_FILE) << 020; |
| 181 |
break; |
| 182 |
case 2: |
| 183 |
*(unsigned short *)addr = getc (input_FILE); |
| 184 |
*(unsigned short *)addr += getc (input_FILE) << 010; |
| 185 |
break; |
| 186 |
case 1: |
| 187 |
*(unsigned char *)addr = getc (input_FILE); |
| 188 |
break; |
| 189 |
default: |
| 190 |
error (EXIT_FAILURE, 0, _("unsupported size in io_read (): %u"), size); |
| 191 |
} |
| 192 |
} |
| 193 |
else if (io_type == IO_TYPE_STR) |
| 194 |
fgets0 (addr, size, input_FILE); |
| 195 |
else if (io_type == IO_TYPE_FIX) |
| 196 |
{ |
| 197 |
ignore_value (fread (addr, size, 1, input_FILE)); |
| 198 |
} |
| 199 |
else |
| 200 |
error (0, 0, _("unknown I/O type: %d"), io_type); |
| 201 |
return size; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
unsigned int |
| 208 |
token_flags (char const *buf) |
| 209 |
{ |
| 210 |
return *(unsigned char const *)&buf[strlen (buf) + 1]; |
| 211 |
} |
| 212 |
|
| 213 |
unsigned short |
| 214 |
token_count (char const *buf) |
| 215 |
{ |
| 216 |
unsigned char const *flags = (unsigned char const *)&buf[strlen (buf) + 1]; |
| 217 |
unsigned char const *addr = flags + 1; |
| 218 |
unsigned short count = *addr; |
| 219 |
if (*flags & TOK_SHORT_COUNT) |
| 220 |
count += (*++addr << 8); |
| 221 |
return count; |
| 222 |
} |
| 223 |
|
| 224 |
unsigned char const * |
| 225 |
token_hits_addr (char const *buf) |
| 226 |
{ |
| 227 |
unsigned char const *flags = (unsigned char const *)&buf[strlen (buf) + 1]; |
| 228 |
unsigned char const *addr = flags + 2; |
| 229 |
if (*flags & TOK_SHORT_COUNT) |
| 230 |
addr++; |
| 231 |
return addr; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
int |
| 239 |
tree8_count_levels (unsigned int cardinality) |
| 240 |
{ |
| 241 |
int levels = 1; |
| 242 |
cardinality--; |
| 243 |
while (cardinality >>= 3) |
| 244 |
++levels; |
| 245 |
return levels; |
| 246 |
} |
| 247 |
|
| 248 |
int |
| 249 |
gets_past_00 (char *tok, FILE *input_FILE) |
| 250 |
{ |
| 251 |
int got = 0; |
| 252 |
int c; |
| 253 |
do |
| 254 |
{ |
| 255 |
do |
| 256 |
{ |
| 257 |
got++; |
| 258 |
c = getc (input_FILE); |
| 259 |
*tok++ = c; |
| 260 |
} |
| 261 |
while (c > 0); |
| 262 |
got++; |
| 263 |
c = getc (input_FILE); |
| 264 |
*tok++ = c; |
| 265 |
} |
| 266 |
while (c > 0); |
| 267 |
return got - 2; |
| 268 |
} |
| 269 |
|
| 270 |
int |
| 271 |
skip_past_00 (FILE *input_FILE) |
| 272 |
{ |
| 273 |
int skipped = 0; |
| 274 |
do |
| 275 |
{ |
| 276 |
do |
| 277 |
skipped++; |
| 278 |
while (getc (input_FILE) > 0); |
| 279 |
skipped++; |
| 280 |
} |
| 281 |
while (getc (input_FILE) > 0); |
| 282 |
return skipped; |
| 283 |
} |