Subversion Repositories tendra.SVN

Rev

Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
/*** library.c --- TDF library ADT.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 *** Commentary:
66
 *
67
 * This file implements the TDF library routines used by the TDF linker.
68
 *
69
 *** Change Log:
70
 * $Log: library.c,v $
71
 * Revision 1.1.1.1  1998/01/17  15:57:18  release
72
 * First version to be checked into rolling release.
73
 *
74
 * Revision 1.5  1995/09/22  08:39:17  smf
75
 * Fixed problems with incomplete structures (to shut "tcc" up).
76
 * Fixed some problems in "name-key.c" (no real problems, but rewritten to
77
 * reduce the warnings that were output by "tcc" and "gcc").
78
 * Fixed bug CR95_354.tld-common-id-problem (library capsules could be loaded
79
 * more than once).
80
 *
81
 * Revision 1.4  1995/07/07  15:32:25  smf
82
 * Updated to support TDF specification 4.0.
83
 *
84
 * Revision 1.3  1994/12/12  11:46:24  smf
85
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
86
 * OSSG C Coding Standards.
87
 *
88
 * Revision 1.2  1994/08/23  09:40:08  smf
89
 * Fixed bug DR114:ids-too-long.
90
 *
91
 * Revision 1.1.1.1  1994/07/25  16:03:31  smf
92
 * Initial import of TDF linker 3.5 non shared files.
93
 *
94
**/
95
 
96
/****************************************************************************/
97
 
98
#include "library.h"
99
#include "capsule.h"
100
#include "debug.h"
101
#include "exception.h"
102
#include "file-name.h"
103
#include "gen-errors.h"
104
#include "tdf.h"
105
#include "tdf-write.h"
106
 
107
#include "solve-cycles.h"
108
 
109
/*--------------------------------------------------------------------------*/
110
 
7 7u83 111
static ExceptionP XX_library_error   = EXCEPTION("error in TDF library");
2 7u83 112
 
113
/*--------------------------------------------------------------------------*/
114
 
115
static TDFReaderP
7 7u83 116
library_reader(LibraryP library)
2 7u83 117
{
7 7u83 118
    ASSERT(library->type == LT_INPUT);
119
    return(& (library->u.reader));
2 7u83 120
}
121
 
122
static TDFWriterP
7 7u83 123
library_writer(LibraryP library)
2 7u83 124
{
7 7u83 125
    ASSERT(library->type == LT_OUTPUT);
126
    return(& (library->u.writer));
2 7u83 127
}
128
 
129
/*--------------------------------------------------------------------------*/
130
 
131
static void
7 7u83 132
library_check_index_entry(LibraryP library, ShapeEntryP entry, BoolT need_dec,
133
			  BoolT no_mult, NStringP shape_key, NameKeyP key,
134
			  unsigned use, LibCapsuleP lib_capsule,
135
			  NameTableP table)
2 7u83 136
{
7 7u83 137
    NameEntryP  name_entry  = name_table_add(table, key, entry);
138
    unsigned    name_use    = name_entry_get_use(name_entry);
2 7u83 139
 
140
    if (use & ~(U_USED | U_DECD | U_DEFD | U_MULT)) {
7 7u83 141
	E_lib_bad_usage(library, shape_key, key, use);
142
	THROW(XX_library_error);
2 7u83 143
	UNREACHED;
144
    } else if (no_mult && (use & U_MULT)) {
7 7u83 145
	E_lib_illegally_mult_defined(library, shape_key, key);
146
	THROW(XX_library_error);
2 7u83 147
	UNREACHED;
148
    } else if (need_dec &&
7 7u83 149
	      (((use & (U_DEFD | U_DECD)) == U_DEFD) ||
2 7u83 150
		((use & (U_MULT | U_DECD)) == U_MULT))) {
7 7u83 151
	E_lib_defined_but_not_declared(library, shape_key, key);
152
	THROW(XX_library_error);
2 7u83 153
	UNREACHED;
154
    }
155
    if ((use & U_DEFD) && (name_use & U_DEFD)) {
7 7u83 156
	LibCapsuleP definition = name_entry_get_lib_definition(name_entry);
2 7u83 157
 
7 7u83 158
	E_lib_multiply_defined(library, shape_key, key, definition);
2 7u83 159
    } else if ((use & U_MULT) && (name_use & U_MULT) &&
7 7u83 160
	      (!(use & U_DEFD)) && (!(name_use & U_DEFD))) {
161
	name_entry_set_lib_definition(name_entry, NIL(LibCapsuleP));
2 7u83 162
    } else if ((use & U_DEFD) ||
7 7u83 163
	      ((use & U_MULT) && (!(name_use & (U_MULT | U_DEFD))))) {
164
	name_entry_set_lib_definition(name_entry, lib_capsule);
2 7u83 165
    }
7 7u83 166
    debug_info_r_index_entry(key, use, name_use, name_entry_key(name_entry),
167
			      lib_capsule_name(lib_capsule));
168
    name_entry_merge_use(name_entry, use);
2 7u83 169
}
170
 
171
static unsigned
7 7u83 172
library_read_version_0_capsules(LibraryP library)
2 7u83 173
{
7 7u83 174
    TDFReaderP reader       = library_reader(library);
175
    unsigned   num_capsules = tdf_read_int(reader);
2 7u83 176
    unsigned   i;
177
 
7 7u83 178
    debug_info_r_start_capsules(num_capsules);
2 7u83 179
    library->num_capsules = num_capsules;
7 7u83 180
    library->capsules     = ALLOCATE_VECTOR(LibCapsuleT, num_capsules);
181
    for (i = 0; i < num_capsules; i++) {
182
	NStringP contents = & (library->capsules[i].contents);
2 7u83 183
	NStringT nstring;
184
	unsigned length;
185
 
7 7u83 186
	tdf_read_string(reader, &nstring);
187
	if (nstring_contains(&nstring, '\0')) {
188
	    E_null_in_file_name(library, &nstring);
189
	    THROW(XX_library_error);
2 7u83 190
	    UNREACHED;
191
	}
7 7u83 192
	library->capsules[i].name    = nstring_to_cstring(&nstring);
193
	library->capsules[i].library = library;
194
	library->capsules[i].loaded  = FALSE;
195
	length = tdf_read_int(reader);
196
	nstring_init_length(contents, length);
197
	tdf_read_bytes(reader, contents);
198
	debug_info_r_capsule(&nstring, length);
199
	nstring_destroy(&nstring);
2 7u83 200
    }
7 7u83 201
    return(num_capsules);
2 7u83 202
}
203
 
204
static void
7 7u83 205
library_read_version_0(LibraryP    library,				ShapeTableP shapes)
2 7u83 206
{
7 7u83 207
    TDFReaderP  reader       = library_reader(library);
208
    unsigned    num_capsules = library_read_version_0_capsules(library);
209
    ShapeEntryP token_entry  = shape_table_get_token_entry(shapes);
210
    ShapeEntryP tag_entry    = shape_table_get_tag_entry(shapes);
211
    unsigned    num_shapes   = tdf_read_int(reader);
2 7u83 212
    unsigned    i;
213
 
7 7u83 214
    debug_info_r_start_index(num_shapes);
215
    for (i = 0; i < num_shapes; i++) {
2 7u83 216
	NStringT    name;
217
	ShapeEntryP entry;
218
	BoolT       need_dec;
219
	BoolT       no_mult;
220
	NameTableP  table;
221
	unsigned    num_names;
222
	unsigned    j;
223
 
7 7u83 224
	tdf_read_string(reader, &name);
225
	entry     = shape_table_add(shapes, &name);
2 7u83 226
	need_dec  = (entry == tag_entry);
227
	no_mult   = (entry == token_entry);
7 7u83 228
	table     = shape_entry_name_table(entry);
229
	num_names = tdf_read_int(reader);
230
	debug_info_r_start_shape_index(&name, num_names);
231
	for (j = 0; j < num_names; j++) {
2 7u83 232
	    NameKeyT    external_name;
233
	    unsigned    use;
234
	    unsigned    capsule_index;
235
	    LibCapsuleP lib_capsule;
236
 
7 7u83 237
	    tdf_read_name(reader, &external_name);
238
	    use           = tdf_read_int(reader);
239
	    capsule_index = tdf_read_int(reader);
2 7u83 240
	    if (capsule_index >= num_capsules) {
7 7u83 241
		E_capsule_index_too_big(library, &name, &external_name,
2 7u83 242
					 capsule_index, num_capsules);
7 7u83 243
		THROW(XX_library_error);
2 7u83 244
		UNREACHED;
245
	    }
7 7u83 246
	    lib_capsule = & (library->capsules[capsule_index]);
247
	    library_check_index_entry(library, entry, need_dec, no_mult,
2 7u83 248
				       &name, &external_name, use, lib_capsule,
249
				       table);
7 7u83 250
	    name_key_destroy(&external_name);
2 7u83 251
	}
7 7u83 252
	nstring_destroy(&name);
2 7u83 253
    }
7 7u83 254
    tdf_read_eof(reader);
2 7u83 255
}
256
 
257
static void
7 7u83 258
library_extract_1(LibCapsuleP capsule,			   BoolT       use_basename)
2 7u83 259
{
7 7u83 260
    CStringP   old_name = lib_capsule_name(capsule);
2 7u83 261
    CStringP   name     = old_name;
7 7u83 262
    NStringP   contents = lib_capsule_contents(capsule);
2 7u83 263
    TDFWriterT writer;
264
 
265
    if (use_basename) {
7 7u83 266
	name = file_name_basename(name);
2 7u83 267
    }
7 7u83 268
    file_name_populate(name);
269
    if (tdf_writer_open(&writer, name)) {
270
	E_extracting_capsule(old_name, name);
271
	tdf_write_bytes(&writer, contents);
272
	tdf_writer_close(&writer);
2 7u83 273
    } else {
7 7u83 274
	E_cannot_open_output_file(name);
2 7u83 275
    }
276
    if (use_basename) {
7 7u83 277
	DEALLOCATE(name);
2 7u83 278
    }
279
}
280
 
281
/*--------------------------------------------------------------------------*/
282
 
7 7u83 283
typedef void(*LibTypeProcP)
284
(LibraryP, ShapeTableP);
2 7u83 285
 
7 7u83 286
static LibTypeProcP library_type_jump_table[] = {
2 7u83 287
    library_read_version_0
288
};
289
 
290
#define LIBRARY_TYPE_JUMP_TABLE_SIZE \
7 7u83 291
	((unsigned)(sizeof(library_type_jump_table) / sizeof(LibTypeProcP)))
2 7u83 292
 
293
/*--------------------------------------------------------------------------*/
294
 
295
static NStringP
7 7u83 296
library_magic(void)
2 7u83 297
{
298
    static NStringT const_magic;
299
    static BoolT    inited = FALSE;
300
 
301
    if (!inited) {
7 7u83 302
	nstring_copy_cstring(&const_magic, "TDFL");
2 7u83 303
	inited = TRUE;
304
    }
7 7u83 305
    return(&const_magic);
2 7u83 306
}
307
 
308
/*--------------------------------------------------------------------------*/
309
 
310
static void
7 7u83 311
library_read_header(LibraryP library)
2 7u83 312
{
7 7u83 313
    TDFReaderP reader        = library_reader(library);
314
    NStringP   const_magic   = library_magic();
315
    unsigned   capsule_major = capsule_get_major_version();
2 7u83 316
    NStringT   magic;
317
    unsigned   major;
318
    unsigned   minor;
319
 
7 7u83 320
    nstring_init_length(&magic,(unsigned)4);
321
    tdf_read_bytes(reader, &magic);
322
    if (!nstring_equal(&magic, const_magic)) {
323
	E_library_bad_magic(library, &magic, const_magic);
324
	THROW(XX_library_error);
2 7u83 325
	UNREACHED;
326
    }
7 7u83 327
    nstring_destroy(&magic);
328
    major = tdf_read_int(reader);
329
    minor = tdf_read_int(reader);
330
    debug_info_r_lib_versions(major, minor);
2 7u83 331
    if (major < 4) {
7 7u83 332
	E_library_bad_version(library, major);
333
	THROW(XX_library_error);
2 7u83 334
	UNREACHED;
335
    } else if (capsule_major == 0) {
7 7u83 336
	capsule_set_major_version(major);
2 7u83 337
    } else if (capsule_major != major) {
7 7u83 338
	E_library_version_mismatch(library, capsule_major, major);
339
	THROW(XX_library_error);
2 7u83 340
	UNREACHED;
341
    }
342
    library->major = major;
343
    library->minor = minor;
7 7u83 344
    tdf_read_align(reader);
2 7u83 345
}
346
 
347
/*--------------------------------------------------------------------------*/
348
 
349
static void
7 7u83 350
library_write_header(LibraryP library)
2 7u83 351
{
7 7u83 352
    TDFWriterP writer      = library_writer(library);
353
    NStringP   const_magic = library_magic();
354
    unsigned   major       = capsule_get_major_version();
355
    unsigned   minor       = capsule_get_minor_version();
2 7u83 356
 
7 7u83 357
    tdf_write_bytes(writer, const_magic);
358
    ASSERT(major >= 4);
359
    tdf_write_int(writer, major);
360
    tdf_write_int(writer, minor);
361
    debug_info_w_lib_versions(major, minor);
362
    tdf_write_align(writer);
2 7u83 363
}
364
 
365
 
366
/*--------------------------------------------------------------------------*/
367
 
368
CStringP
7 7u83 369
lib_capsule_name(LibCapsuleP capsule)
2 7u83 370
{
7 7u83 371
    return(capsule->name);
2 7u83 372
}
373
 
374
CStringP
7 7u83 375
lib_capsule_full_name(LibCapsuleP capsule)
2 7u83 376
{
7 7u83 377
    CStringP lib_name   = library_name(capsule->library);
378
    unsigned lib_length = cstring_length(lib_name);
379
    CStringP name       = lib_capsule_name(capsule);
380
    unsigned length     = cstring_length(name);
381
    CStringP full_name  = ALLOCATE_VECTOR(char, lib_length + length + 3);
2 7u83 382
    CStringP tmp        = full_name;
383
 
7 7u83 384
   (void)memcpy((GenericP)tmp,(GenericP)lib_name,(SizeT)lib_length);
2 7u83 385
    tmp += lib_length;
386
    *tmp = '(';
7 7u83 387
    tmp++;
388
   (void)memcpy((GenericP)tmp,(GenericP)name,(SizeT)length);
2 7u83 389
    tmp += length;
390
    *tmp = ')';
7 7u83 391
    tmp++;
2 7u83 392
    *tmp = '\0';
7 7u83 393
    return(full_name);
2 7u83 394
}
395
 
396
NStringP
7 7u83 397
lib_capsule_contents(LibCapsuleP capsule)
2 7u83 398
{
7 7u83 399
    return(& (capsule->contents));
2 7u83 400
}
401
 
402
BoolT
7 7u83 403
lib_capsule_is_loaded(LibCapsuleP capsule)
2 7u83 404
{
7 7u83 405
    return(capsule->loaded);
2 7u83 406
}
407
 
408
void
7 7u83 409
lib_capsule_loaded(LibCapsuleP capsule)
2 7u83 410
{
411
    capsule->loaded = TRUE;
412
}
413
 
414
/*--------------------------------------------------------------------------*/
415
 
416
void
7 7u83 417
write_lib_capsule_full_name(OStreamP    ostream,				     LibCapsuleP capsule)
2 7u83 418
{
7 7u83 419
    write_cstring(ostream, library_name(capsule->library));
420
    write_char(ostream, '(');
421
    write_cstring(ostream, lib_capsule_name(capsule));
422
    write_char(ostream, ')');
2 7u83 423
}
424
 
425
/*--------------------------------------------------------------------------*/
426
 
427
LibraryP
7 7u83 428
library_create_stream_input(CStringP name)
2 7u83 429
{
7 7u83 430
    LibraryP library = ALLOCATE(LibraryT);
2 7u83 431
 
432
    library->type = LT_INPUT;
7 7u83 433
    if (!tdf_reader_open(library_reader(library), name)) {
434
	DEALLOCATE(library);
435
	return(NIL(LibraryP));
2 7u83 436
    }
437
    library->name     = name;
438
    library->complete = FALSE;
7 7u83 439
    return(library);
2 7u83 440
}
441
 
442
LibraryP
7 7u83 443
library_create_stream_output(CStringP name)
2 7u83 444
{
7 7u83 445
    LibraryP library = ALLOCATE(LibraryT);
2 7u83 446
 
447
    library->type = LT_OUTPUT;
7 7u83 448
    if (!tdf_writer_open(library_writer(library), name)) {
449
	DEALLOCATE(library);
450
	return(NIL(LibraryP));
2 7u83 451
    }
452
    library->name     = name;
453
    library->complete = FALSE;
7 7u83 454
    return(library);
2 7u83 455
}
456
 
457
CStringP
7 7u83 458
library_name(LibraryP library)
2 7u83 459
{
7 7u83 460
    return(library->name);
2 7u83 461
}
462
 
463
unsigned
7 7u83 464
library_num_capsules(LibraryP library)
2 7u83 465
{
7 7u83 466
    return(library->num_capsules);
2 7u83 467
}
468
 
469
LibCapsuleP
7 7u83 470
library_get_capsule(LibraryP library,			     unsigned capsule_index)
2 7u83 471
{
7 7u83 472
    ASSERT(capsule_index < library->num_capsules);
473
    return(& (library->capsules[capsule_index]));
2 7u83 474
}
475
 
476
unsigned
7 7u83 477
library_byte(LibraryP library)
2 7u83 478
{
7 7u83 479
    return(tdf_reader_byte(library_reader(library)));
2 7u83 480
}
481
 
482
void
7 7u83 483
library_content(LibraryP library,			 BoolT    want_index, 
484
			 BoolT    want_size, 
2 7u83 485
		         BoolT    want_version)
486
{
7 7u83 487
    ShapeTableP shapes = shape_table_create();
2 7u83 488
 
7 7u83 489
    library_read(library, shapes);
2 7u83 490
    if (library->complete) {
491
	unsigned i;
492
 
493
	if (want_version) {
7 7u83 494
	    write_char    (ostream_output, '[');
495
	    write_unsigned(ostream_output, library->major);
496
	    write_cstring (ostream_output, ", ");
497
	    write_unsigned(ostream_output, library->minor);
498
	    write_char    (ostream_output, ']');
499
	    write_newline (ostream_output);
2 7u83 500
	}
7 7u83 501
	for (i = 0; i < library->num_capsules; i++) {
502
	    LibCapsuleP capsule = & (library->capsules[i]);
2 7u83 503
 
7 7u83 504
	    write_cstring(ostream_output, lib_capsule_name(capsule));
2 7u83 505
	    if (want_size) {
7 7u83 506
		NStringP body = lib_capsule_contents(capsule);
2 7u83 507
 
7 7u83 508
		write_cstring(ostream_output, " (");
509
		write_unsigned(ostream_output, nstring_length(body));
510
		write_char(ostream_output, ')');
2 7u83 511
	    }
7 7u83 512
	    write_newline(ostream_output);
2 7u83 513
	}
514
	if (want_index) {
7 7u83 515
	    shape_table_iter(shapes, shape_entry_show_content,
516
			      NIL(GenericP));
2 7u83 517
	}
518
    }
519
}
520
 
521
void
7 7u83 522
library_extract_all(LibraryP library,			     BoolT    use_basename)
2 7u83 523
{
7 7u83 524
    ShapeTableP shapes = shape_table_create();
2 7u83 525
 
7 7u83 526
    library_read(library, shapes);
2 7u83 527
    if (library->complete) {
528
	unsigned i;
529
 
7 7u83 530
	for (i = 0; i < library->num_capsules; i++) {
531
	    LibCapsuleP capsule = & (library->capsules[i]);
2 7u83 532
 
7 7u83 533
	    library_extract_1(capsule, use_basename);
2 7u83 534
	}
535
    }
536
}
537
 
538
void
7 7u83 539
library_extract(LibraryP library, BoolT use_basename, BoolT match_basename,
540
		unsigned num_files, CStringP *files)
2 7u83 541
{
7 7u83 542
    ShapeTableP shapes = shape_table_create();
2 7u83 543
 
7 7u83 544
    library_read(library, shapes);
2 7u83 545
    if (library->complete) {
546
	unsigned i;
547
 
7 7u83 548
	for (i = 0; i < num_files; i++) {
2 7u83 549
	    BoolT    matched = FALSE;
550
	    unsigned j;
551
 
7 7u83 552
	    for (j = 0; j < library->num_capsules; j++) {
553
		LibCapsuleP capsule   = & (library->capsules[j]);
554
		CStringP    file_name = (files[i]);
555
		CStringP    lib_name  = lib_capsule_name(capsule);
556
		CStringP    base_name = NIL(CStringP);
2 7u83 557
 
558
		if (match_basename) {
7 7u83 559
		    base_name = file_name_basename(lib_name);
2 7u83 560
		}
7 7u83 561
		if ((cstring_equal(file_name, lib_name)) ||
562
		   (match_basename && cstring_equal(file_name, base_name))) {
563
		    library_extract_1(capsule, use_basename);
2 7u83 564
		    matched = TRUE;
565
		}
566
		if (match_basename) {
7 7u83 567
		    DEALLOCATE(base_name);
2 7u83 568
		}
569
	    }
570
	    if (!matched) {
7 7u83 571
		E_capsule_not_found(files[i], library_name(library));
2 7u83 572
	    }
573
	}
574
    }
575
}
576
 
577
void
7 7u83 578
library_read(LibraryP    library,		      ShapeTableP shapes)
2 7u83 579
{
580
    HANDLE {
7 7u83 581
	TDFReaderP reader = library_reader(library);
2 7u83 582
	unsigned   library_type;
583
 
7 7u83 584
	debug_info_r_start_library(library_name(library));
585
	library_read_header(library);
586
	library_type = tdf_read_int(reader);
2 7u83 587
	if (library_type >= LIBRARY_TYPE_JUMP_TABLE_SIZE) {
7 7u83 588
	    E_lib_unknown_type(library, library_type);
589
	    THROW(XX_library_error);
2 7u83 590
	    UNREACHED;
591
	}
7 7u83 592
	debug_info_r_library_version(library_type);
593
	(*(library_type_jump_table[library_type]))(library, shapes);
594
	debug_info_r_end_library();
2 7u83 595
	library->complete = TRUE;
596
    } WITH {
7 7u83 597
	ExceptionP exception = EXCEPTION_EXCEPTION();
2 7u83 598
 
7 7u83 599
	debug_info_r_abort_library();
2 7u83 600
	if ((exception != XX_tdf_read_error) &&
7 7u83 601
	   (exception != XX_library_error)) {
602
	    RETHROW();
2 7u83 603
	}
604
    } END_HANDLE
605
}
606
 
607
void
7 7u83 608
library_write(LibraryP    library,		       ShapeTableP shapes, 
609
		       unsigned    num_capsules, 
2 7u83 610
		       CapsuleP   *capsules)
611
{
7 7u83 612
    TDFWriterP writer     = library_writer(library);
2 7u83 613
    unsigned   num_shapes = 0;
614
    unsigned   i;
615
 
7 7u83 616
    debug_info_w_start_library(library_name(library));
617
    library_write_header(library);
618
    debug_info_w_library_version((unsigned)0);
619
    tdf_write_int(writer,(unsigned)0);
620
    debug_info_w_start_capsules(num_capsules);
621
    tdf_write_int(writer, num_capsules);
622
    for (i = 0; i < num_capsules; i++) {
623
	CapsuleP capsule  = capsules[i];
624
	CStringP name     = capsule_name(capsule);
625
	NStringP contents = capsule_contents(capsule);
626
	unsigned length   = nstring_length(contents);
2 7u83 627
	NStringT nstring;
628
 
7 7u83 629
	debug_info_w_capsule(name, length);
630
	nstring_copy_cstring(&nstring, name);
631
	tdf_write_string(writer, &nstring);
632
	nstring_destroy(&nstring);
633
	tdf_write_int(writer, length);
634
	tdf_write_bytes(writer, contents);
2 7u83 635
    }
7 7u83 636
    shape_table_iter(shapes, shape_entry_do_lib_count,
637
		     (GenericP) &num_shapes);
638
    debug_info_w_start_index(num_shapes);
639
    tdf_write_int(writer, num_shapes);
640
    shape_table_iter(shapes, shape_entry_do_lib_write,(GenericP)writer);
641
    debug_info_w_end_library();
2 7u83 642
}
643
 
644
void
7 7u83 645
library_close(LibraryP library)
2 7u83 646
{
7 7u83 647
    switch (library->type)EXHAUSTIVE {
2 7u83 648
      case CT_INPUT:
7 7u83 649
	tdf_reader_close(library_reader(library));
2 7u83 650
	break;
651
      case CT_OUTPUT:
7 7u83 652
	tdf_writer_close(library_writer(library));
2 7u83 653
	break;
654
    }
655
}
656
 
657
/*
658
 * Local variables(smf):
659
 * eval: (include::add-path-entry "../os-interface" "../library")
660
 * eval: (include::add-path-entry "../generated")
661
 * end:
662
**/