File: binio.h

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (720 lines) | stat: -rw-r--r-- 33,638 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
    BINIO.H
    Declare structures and functions for arrays of data that have
    meaningful interpretations on disk.

    $Id: binio.h,v 1.1 1993/08/27 18:32:09 munro Exp $

    Yorick supports the following data types:

       char   short   int   long   float   double
       string   pointer   complex   struct(instance)
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#ifndef BINIO_H
#define BINIO_H

#include "hash.h"

/*--------------------------------------------------------------------------*/

/* IOStream is a complicated data structure which completely represents
   a binary I/O stream.  The data in the file(s) can be decribed using
   the Contents Log (CLOG) language.  Major components of the IOStream
   are:
      1. A table of data primitive and compound data types (structTable).
      2. A table of variables (dataTable), which associate a variable
         name with its data type, dimensions, and disk address.
      3. An optional block of history information, detailing the
         disk addresses (and files, if this is a family of files) of
	 the history records, if there are any.
      4. An optional list of pointees, associating memory and disk
         addresses of indirectly referenced data (not explicitly
	 in the dataTable).
      5. An optional file containing the (plain text) CLOG description
         of this file.
      6. The list of cache blocks associated with this file, and the
         last address read or written (since ftell doesn't know about
	 the cache buffers).
 */
typedef struct IOStream IOStream;

/* StructDef is the definition of a data structure, explicating the
   meaning of data either on disk or in memory, and its relationship to
   other data types.  All structure definitions representing disk data
   are "owned" by the IOStream associated with the disk file.  Each
   disk structure has a "model" data structure representing the
   memory version of the same data.  A conversion procedure converts
   from a representation to its model or vice versa -- a feature
   designed to handle disk data written in a non-native format.
   (A structure representing data in memory may also have a "model"
   and a corresponding conversion routine to handle automatic memory-to-
   memory type conversions.)
   A StructDef may represent either a primitive data type, or a compound
   data type consisting of dimensioned members of simpler data types.
   All data structures have an ASCII name, which is used to make
   default correspondences between in-memory and on-disk structures.  */
typedef struct StructDef StructDef;

/* Array is a block of data with a specific data type (specified by
   a StructDef) and dimensions.  All objects of type pointer must be
   either 0, or point to the first address of an Array value.
   An Array represents scalar data if its dimension list is nil.  */
typedef struct Array Array;

/* Dimension is an element of a linked list of dimensions.  A Dimension
   list is always from slowest to fastest varying dimension.  Each
   dimension has an origin (suggested minimum index value) and length.  */
typedef struct Dimension Dimension;
struct Dimension {
  Dimension *next;   /* list goes from slowest to fastest varying */
  long number;       /* length of this dimension */
  long origin;       /* index of first element along this dimension */
  int references;    /* reference counter */
};

/* Member is a data type and dimension list.  The dataTable of an IOStream,
   the member list of a StructDef, and an Array all consist in part of
   one or more Member instances.  */
typedef struct Member Member;
struct Member { /* most general type of x:  base_type x[][]...[][]; */
  StructDef *base;   /* array base data type */
  Dimension *dims;   /* array dimension list (may be nil) */
  long number;       /* of items in array (product of dimensions) */
};

/* Strider is an element of a linked list of strides through data.  Each
   Strider consists of a stride (in bytes), a number of "footprints",
   and an optional Array of type long containing an index list.  */
typedef struct Strider Strider;
struct Strider {
  Strider *next;        /* logically next faster varying block */
  long stride;          /* stride length in bytes */
  long number;          /* number of strides to take + 1 (# footprints!) */
  Array *indexList;     /* if !=0, offset list (units of 'stride' bytes) */
};

/* Operations is a virtual function table for a DataBlock, the
   container class for Array, StructDef, and IOStream (and others).  */
typedef struct Operations Operations;
/* The Operations* members depend on the details of Yorick, and are
   therefore split out to allow this binary file package to be used
   in non-Yorick based codes.  These three values are explicitly
   needed:  */
extern Operations *yOpsStruct, *yOpsStream, *yOpsStructDef;

/* DataLayout is a complete description of a primitive data type,
   including byte size, alignment, byte order, and floating point layout
   (if the type represents a real number).  */
typedef struct DataLayout DataLayout;

/* FPLayout is a description of the format of a floating point number,
   detailing the bit addresses and number of bits in the exponent and
   mantissa.  The StructDefs of an IOStream corresponding to the float
   and double primitive types must be describable by an FPLayout.  */
typedef struct FPLayout FPLayout;

/* IOOperations is an additional virtual function table for an IOStream.  */
typedef struct IOOperations IOOperations;

/* HistoryInfo keeps history record information for an IOStream.  */
typedef struct HistoryInfo HistoryInfo;

/* PointeeList keeps an association of disk and memory addresses for an
   IOStream.  This allows multiply referenced pointers to be written
   only once.  More subtlely, with a PointeeList, the algorithm to
   write an arbitrarily complex tree of pointers requires a bounded
   amount of stack space.  */
typedef struct PointeeList PointeeList;
typedef struct MDinfo MDinfo;  /* for PointeeList */
struct PointeeList {
  int writing;         /* 0 if table keyed to disk address,
			  1 if keyed to memory address */
  long nValid;         /* number of valid objects (i.e.- where both
			  disk data address and m pointer contain
			  valid data) */
  HashXTable table;    /* generic hash table for address correspondence */
  MDinfo *mdInfo;      /* memory-disk correspondence list */
};

/* CLbuffer describes the state of the binary data description language
   file associated with an IOStream.  Defined in clog.c.  */
typedef struct CLbuffer CLbuffer;

/* CacheBlock is required by IOStream, but is opaque here.  See cache.c.  */
typedef struct CacheBlock CacheBlock;

/*--------------------------------------------------------------------------*/

struct IOStream {
  int references;      /* reference counter */
  Operations *ops;     /* virtual function table */
  void *stream;        /* usually the FILE*, but opaque here --
			  0 indicates file has been closed */
  char *fullname;      /* filename after YExpandName */
  int permissions;     /* +1 read permission, +2 write permission
			  +4 append mode, +8 binary mode */

  IOOperations *ioOps; /* virtual functions to read and write file */

  long blockSize;         /* MUST be a power of 2 minus 1 (default 0x3fff)
			     all cache blocks will be multiples of
			     blockSize+1 bytes */
  CacheBlock *blockList;  /* list of cache blocks for this file,
			     stored highest to lowest address */
  long seqAddress;        /* address immediately after last address
			     actually read or written using YcRead or
			     YcWrite -- different from ftell(stream)
			     because of caching */

  int structAlign;        /* byte offset of any struct in another struct
			     must be multiple of this (the alignment of
			     some structs may be more strict than this) */
  int dataAlign;          /* 0  -- align variables to addresses as if
			           the file were a giant struct instance
			     >0 -- byte offset of any variable in file
			           must be a multiple of this
			     WARNING WARNING WARNING WARNING WARNING
			     This MUST be 0 in order to correctly write
			     a PDB file with history records.  */

  HashTable structTable;  /* table of StructDef names for this file */
  StructDef **structList; /* corresponding to structTable->names */

  HashTable dataTable;    /* table of variable names for this file */
  Member *types;          /* corresponding types and dimensions */
  long *addresses;        /* corresponding addresses */
  long nextAddress;       /* next free address (may be end of pointee) */
  long offset;            /* to be applied to all addresses */

  HistoryInfo *history;   /* non-0 if there is a history record structure
			     -- If so, then no changes to the dataTable
			     (which represents static variables) are
			     permitted.
			     Note that this IOStream may be either
			     history->parent or history->child.  */

  CLbuffer *contentsLog;  /* if non-0, contains "CLOG" textual equivalent
			     of structTable, dataTable, and history */

  PointeeList pointeeList;  /* list of memory<->disk address correspondence
			       to aid in pointee reads or writes */
  void (*CloseHook)(IOStream *file);  /* if non-0, called before close */
};

/*--------------------------------------------------------------------------*/

/* A Copier copies data of type base from t to s.  If t==s, the Copier
   must appropriately zero s, releasing pointees if appropriate.  If
   t==s, non-pointered data should be left unchanged (the Copier is a
   no-op).  There are only 4 possible Copiers-- CopyX should be used
   for StructDefs which contain no pointers, CopyS for StructDefs
   whose members include pointers or strings.  CopyQ is for stringStruct
   and CopyP is for pointerStruct.  */
typedef void Copier(StructDef *base, void *dst, const void *src, long n);
extern Copier CopyX, CopyQ, CopyP, CopyS;

/* A Converter converts n objects to or from type base, which will often
   be a foreign disk file data type.  The conversion is from baseData to
   modelData if toBase is 0, from modelData to baseData if toBase
   is non-0.  */
typedef void Converter(StructDef *base, void *baseData, void *modelData,
		       long n, int toBase);
extern Converter ConvertI, ConvertF, ConvertQ, ConvertP, ConvertS;

struct StructDef {
  int references;     /* reference counter */
  Operations *ops;    /* virtual function table */
  Operations *dataOps;  /* virtual functions for an instance */

  long size;            /* size of an instance of this data structure */
  int alignment;        /* byte address must be multiple of this */

  HashTable table;      /* rapid name->index and index->name associations */
  Member *members;      /* members[table->maxEntries] is Member array */
  long *offsets;        /* byte offsets corresponding to members */

  Copier *Copy;         /* copy/initialize/delete operation */

  IOStream *file;       /* non-zero if the data resides on disk
                           if file!=0, then model!=0 */
  long index;           /* if file!=0, index in file->structList,
			   else        index in yStructList */

  int addressType;      /* 0 -> object is in memory (file==0), and address
			        is void*
			   1 -> object is on disk (file!=0) and address
			        is long
			   2 -> address cannot be meaningfully incremented
			        to find members or array elements  */

  /* The remaining elements are to implement "transparent translation"
     of this data type into another -- usually to change from disk
     format to memory format.  */
  StructDef *model;     /* memory equivalent of disk StructDef
			   is NEVER 0 when file!=0,
			   also destination or source type for Convert */
  Converter *Convert;
         /* Convert may be 0 if no conversion is required.
	    The order and fpLayout members can be used as a context
	    by Convert.  In particular, fpLayout need not actually point
	    to an FPLayout, as long as the first member of the struct
	    it does point to is a reference counter.  */
  int order;           /* 1 MSB first, -1 LSB first,
			  -w for LSW first, MSB first within word size w
			  w for MSW first, LSB first within word size w */
  FPLayout *fpLayout;  /* if non-0, used by Export/Import to translate
			  data format */
};

/* Nine in-memory data structures are predefined, corresponding with
   the primitive data types in the C programming language:  */
extern StructDef charStruct;
extern StructDef shortStruct;
extern StructDef intStruct;
extern StructDef longStruct;
extern StructDef floatStruct;
extern StructDef doubleStruct;
extern StructDef stringStruct;   /* char *    always 0-terminated */
extern StructDef pointerStruct;  /* void *    all pointers generic */
extern StructDef complexStruct;  /* struct complex { double re, im; } */
/* The indirect types string and pointer are for restricted use only,
   in the sense that special memory management routines must be used
   to allocate and free the associated memory.
   An IOStream structTable always begins with the on-disk analogues
   of char, short, int, long, float, double, string, and pointer.  */

/*--------------------------------------------------------------------------*/

/* Every on-disk StructDef in the structList for an IOStream has a
   counterpart "model" StructDef representing the same data translated
   into memory format.  As for IOStreams, the first 8 StructDefs in
   yStructList are always, in order, char, short, int, long, float,
   double, string, pointer.  The 9th in yStructList is complex.  */

extern HashTable yStructTable;  /* table of StructDef names for memory */
extern StructDef **yStructList; /* corresponding to yStructTable->names */

/*--------------------------------------------------------------------------*/

struct Array {
  int references;   /* reference counter */
  Operations *ops;  /* virtual function table */
  Member type;      /* full data type, i.e.-    base_type x[]...[] */
#ifdef PAD_ARRAY
  /* on a pentium, 4-byte aligned doubles are legal, but access is
   * far slower than to 8-byte aligned doubles
   * since a Member is 12 bytes long, need to add 4 more here
   * -- no other platform at this time needs this hack */
  int pad;
#endif
  union {
    /* Appropriate member is selected by ops; there are only 10 possible ops,
       corresponding to char, short, int, long, float, double, complex,
       string, pointer, and struct instance.
       Actual array length is determined by type. */
    char c[1];
    short s[1];
    int i[1];
    long l[1];
    float f[1];
    double d[2]; /* also complex */
    char *q[1];  /* string */
    void *p[1];
  } value;
};

/*--------------------------------------------------------------------------*/

/* Each StructDef has an alignment, computed as the alignment of its
   most restrictively aligned member.  Some compilers/machines impose
   an additional restriction on all structs, regardless of the most
   restrictive member, in which case yStructAlign is set to that
   additional struct alignment restriction.
   It is also possible for a C compiler to require different alignment
   for an array than for a scalar.  Yorick cannot handle this.
   Finally, the (hopefully defunct?) hybrid C compiler under the
   (definitely defunct) NLTSS Cray operating system aligned a scalar
   char to an address with remainder 7 modulo 8, which is hopeless.  */
extern int yStructAlign;

struct DataLayout {
  int alignment;
  long size;
  int order;
  FPLayout *fpLayout;
};

/* Foreign formats for floating point data can be handled, provided they
   fit within the parametrization of the FPLayout struct.
   Foreign integer formats are described by a simple int (the order
   member of a StructDef above).  */
struct FPLayout {
  /* Addresses are in bits with 0 128-bit of the most significant byte
     of the object, 8 the 128-bit of the 2nd most significant byte,
     and so on until 8*size-1 is the 1-bit of the least significant
     byte.  The actual storage order of the bytes is given by the
     order member of the DataLayout.
     sgnAddr            - bit address of overall sign
     expAddr, expSize   - bit address and size of exponent
     manAddr, manSize   - bit address and size of mantissa
     manNorm            - if non-zero, address of leading 1 in mantissa
     expBias            - exponent bias
   */
  int references;      /* reference counter */
  int sgnAddr, expAddr, expSize, manAddr, manSize, manNorm;
  long expBias;
};

/* Floating point layouts for floats and doubles on this machine */
extern FPLayout *fltLayout, *dblLayout;

/*--------------------------------------------------------------------------*/

struct IOOperations {
  /* An IOStream requires the virtual functions Read, Write, Tell,
     Seek, and SeekEnd, which are similar to the C standard library
     routines fread, fwrite, ftell, fseek(,,SEEK_SET), and fseek(,,SEEK_END).
     All 5 routines must be provided, but they may simply call YError if
     the operation makes no sense.  The Read function should return the
     number (of objects of size size) actually read if EOF occured before
     the read was complete; n on success.
     In all cases, YError should be called if an error is detected,
     AFTER the error is cleared, on the presumption that the next
     operation will be valid.  */
  long (*Read)(IOStream *file, void *buf, long size, long n);
  void (*Write)(IOStream *file, const void *buf, long size, long n);
  long (*Tell)(IOStream *file, long offset);
  void (*Seek)(IOStream *file, long offset);
  void (*SeekEnd)(IOStream *file, long offset);
  void (*Close)(IOStream *file);
};

struct HistoryInfo {
  IOStream *parent;  /* original file always used for static data */
  IOStream *child;   /* separate IOStream used for history record traffic
			so that child->members[i].base->file has a place
			to point which may represent a different file in
			a history family starting from the original parent */

  int nFamily;       /* number of files in this family */
  char **famNames;   /* used for child->fullname,
			famNames[0]==parent->fullname
			allocated length is 4*(1 + (nFamily-1)/4) */
  int fileNumber;    /* 0 to nFamily-1, specifies index of current child */
  long fileSize;     /* approximate maximum size for a file in family */
#define DEFAULT_FILE_SIZE 0x400000
  int copyParent;    /* non-0 if parent (static variables) is to be copied
			when a new file is added to this family */

  long recordSize;   /* length of one record in bytes (not including
			any strings, pointees, or other indirections) */
  int recordAlign;   /* alignment for records -- either child->dataAlign,
			or most restrictive alignment in child->structList
			when 1st record was declared
			Unlike a StructDef, recordSize is not necessarily
			a multiple of recordAlign.  */
  long nRecords;     /* number of history records of this type */
  long recNumber;    /* index into ifile and offset
			for record represented by child (-1 if none) */

  /* allocated length of following arrays is 16*(1 + (nRecs-1)/16) */
  int *ifile;        /* list of indices into famNames */
  long *offset;      /* list of record byte addresses */
  double *time;      /* 0 or list of floating point values of records */
  long *ncyc;        /* 0 or list of integer values of records */
};

struct MDinfo {
  void *m;       /* memory address, always an Array->value.c; the
		    MDinfo owns a reference of the Array */
  long d;        /* disk address of the pointee header */
  long data;     /* disk address of the pointee data */
  StructDef *base;  /* disk StructDef for this data -- this pointer does
		       NOT own a reference to base */
};

/*--------------------------------------------------------------------------*/

/* fullname is NOT copied and FreeIOStream will do StrFree(ios->fullname) */
extern IOStream *NewIOStream(char *fullname, void *stream, int permissions);
extern void FreeIOStream(void *ios);      /* *** Use Unref(ios) *** */

extern StructDef *NewStructDef(IOStream *file, long index);
extern void FreeStructDef(void *base);      /* *** Use Unref(base) *** */

extern Array *NewArray(StructDef *base, Dimension *dims);
extern void FreeArray(void *array);        /* *** Use Unref(array) *** */

/* NewTmpArray/ClearTmpArray may be used to get one or two temporary
   arrays.  Call ClearTmpArray first, then NewTmpArray several times,
   then ClearTmpArray.  At the third call to NewTmpArray, the Array
   returned by the first call is Unref'ed; at the fourth call to
   NewTmpArray, the second is Unref'ed; and so on.  */
extern Array *NewTmpArray(StructDef *base, Dimension *dims);
extern void ClearTmpArray(void);

/* NewDimension does NOT do Ref(next) */
extern int yForceOrigin; /* non-zero to ignore stated origin in Dimension
			    lists, and always assume 1-origin indices */
extern Dimension *NewDimension(long number, long origin, Dimension *next);
extern void FreeDimension(Dimension *dims);

extern Strider *NewStrider(long stride, long number);
extern void FreeStrider(Strider *strider);

/*--------------------------------------------------------------------------*/

#ifdef NOT_YORICK
/* This is the minimal Operations -- a virtual delete function.
   Without this, the Unref macro is unusable.  */
struct Operations {
  void (*Free)(void *);  /* crucial member for Unref -- first in struct
			    to allow alternate Operations to be used */
};
#endif

#define Ref(db) ((db)?++(db)->references:0 , (db))
#define Unref(db) {if ((db) && --(db)->references<0) (db)->ops->Free(db);}

extern long TotalNumber(const Dimension *dims);
extern int CountDims(const Dimension *dims);
/* Note-- like NewDimension, CopyDims does NOT do Ref(next) */
extern Dimension *CopyDims(Dimension *dims, Dimension *next, int copyOrigin);

/* CopyStrider also does NOT do Ref(next) */
extern Strider *CopyStrider(Strider *strider, Strider *next);

/* tmpDims can be used for "protected" scratch space to build dimension
   lists.  Before using tmpDims, always call FreeDimension(tmpDims).  */
extern Dimension *tmpDims;

/* Given a pointer to data (memory representation of a pointerStruct),
   return the associated Array.  */
extern void *Pointee(void *pointer);

/* StructEqual tests for two structures which are exactly the same.
   EquivStruct tests whether the file StructDef belonging to an IOStream
   is equivalent to the mem StructDef belonging to yStructTable.  */
extern int StructEqual(StructDef *l, StructDef *r);
extern int EquivStruct(StructDef *fil, StructDef *mem);

/* Adjust the address to a multiple of the alignment (no-op if <2).  */
extern long AlignAdjust(long address, int alignment);

/* StructName works for disk or memory based StructDefs,
   CopyStruct returns the StructDef in file->structTable equivalent
               to the given base, creating it if necessary */
extern char *StructName(StructDef *base);
extern StructDef *CopyStruct(IOStream *file, StructDef *base);

/*--------------------------------------------------------------------------*/

/* AddStruct adds an empty data structure to a file structTable,
   returning the new StructDef.  Returns 0 on error.  If the name
   is a duplicate, returns 0 to signal error, but the names "string"
   and "pointer" are excepted from this rule, provided that the
   associated StructDef has not been referenced.
   If file==0, the yStructTable is assumed.
   If n is non-0, it must be the exact length of name --
   if n is 0, name is presumed 0-terminated.
   If file->history!=0, the struct is added to the history child.
   This is illegal after the second history record has begun.  */
extern StructDef *AddStruct(IOStream *file, const char *name, long n);

/* AddMember increments references to memType, dims -- offset
   is computed if input offset<0.  Returns 0 on success, 1 if name is a
   duplicate, 2 if memType or a sub-structure has a type name conflict
   in the file which owns base.
   (If return non-0 memType, dims references NOT incremented).
   In any case, hashIndex set to index of member in base.
   In the case of a duplicate name, memType and dims are NOT
   checked for consistency with the existing member.
   *******
   WARNING-- if offset is specified, base->alignment cannot be updated,
             and the proper alignment must be set by hand  */
extern int AddMember(StructDef *base, long offset, const char *name,
		     StructDef *memType, Dimension *dims);

/* DefinePrimitive is called in lieu of AddMember to define a primitive
   data type after AddStruct has been called.  If model==0, then must
   have Convert==0, and DefinePrimitive will try to match the given
   size, order, and fpLayout to one of char, short, long, float, or
   double (never int) as best it can.  If model is one of the primitive
   types (char, short, int, long, float, double, string, or pointer),
   and size==0, DefinePrimitive will also set Convert appropriately.
   If order==0 (opaque primitive), a model must be supplied.
   If non-zero, fpLayout is copied.
   Returns 0 on success,
           1 on attempt to redefine char, short, int, long, float, or double
	     to a model other than the default one
           2 on attempt to alias another type to string or pointer without
	     providing an explicit Converter
	   3 if an opaque type model size differs and no Converter supplied,
	   4 if Converter supplied without a model
	   5 if no model supplied for opaque primitive */
extern int DefinePrimitive(StructDef *base, long size, int alignment,
			   int addressType, int order, FPLayout *fpLayout,
			   StructDef *model, Converter *Convert);

/* InstallStruct finishes a structure defintion begun with AddStruct
   and AddMember calls.  If base->file==0, base is added to yStructTable
   incrementing base->references.
   If base->file, then an equivalent in-memory model is found for
   base->model: First, yStructTable table is checked for an
   in-memory equivalent StructDef of the same name as base.  Failing
   this, if base is a primitive with non-zero base->order or base->size==1,
   it is matched to the "closest" of the primitive data types char, short,
   long, float, or double (never int).  Failing this, an in-memory model
   is constructed and installed on the spot.
   In all cases, base->dataOps is set to "validate" the StructDef.
   If model is non-zero, it is assumed to be the correct model for base.  */
extern void InstallStruct(StructDef *base, StructDef *model);

/* If DefInstallHook is non-0, InstallStruct calls it after
   installing base in yStructTable if base->file!=0 */
extern void (*DefInstallHook)(const char *name, StructDef *base);

/* AddVariable increments references to varType, dims -- address
   is computed if input address<0.  Returns 0 on success, 1 if name is
   a duplicate, and 2 if the name of varType or one of its sub-structures
   already belongs to a non-equivalent data structure (in either non-0
   case varType, dims references NOT incremented).
   For returns 0 or 1, hashIndex set to index of name in file->dataTable,
   or file->history->child->dataTable, if file->history!=0.
   In the case of a duplicate name, varType and dims are NOT
   checked for consistency with the existing variable.
   If file->history!=0, the variable is added to the history child
   (i.e.- it becomes a record variable).  This is illegal after the
   second history record has begun.  */
extern int AddVariable(IOStream *file, long address, const char *name,
		       StructDef *varType, Dimension *dims);

/* AddHistory adds a history child to an existing IOStream, returning the
   file->history if successful.  Size is the approximate maximum size of
   one file in the history family, or 0 for the default (4 Megabytes).  */
extern HistoryInfo *AddHistory(IOStream *file, long size);

/* AddRecord adds a record to the HistoryInfo, returning 0 on success.
   flags is +1 if time exists, +2 if ncyc exists
   The record is assumed to be in the current history child at the
   specified address, or at the next available address if address<0.  */
extern int AddRecord(HistoryInfo *history,
		     int flags, double time, long ncyc, long address);

/* AddNextFile adds a new file to the HistoryInfo, returning 0 on
   success.  If the fullname is 0, the NextFileName function is used.
   If the create flag is non-0, the file is created (possibly copying
   the parent data), otherwise the file is expected to exist.  */
extern int AddNextFile(HistoryInfo *history, char *filename, int create);

/* Return value must be freed with StrFree.
   Algorithm:
   (1) Strip directory (if any) from name.
   (2) If final character is a digit (0-9), it becomes increment character,
       else the character before the final dot (.) is the increment
            character (or the final character if there is no dot).
   (3) If the increment character is not A-Z, a-z, or 0-9, scan backwards
       through the name until a character in this range is found.
   (4) Loop:
         If the increment character is before the beginning of name,
            or if it is not in the range A-Z, a-z, or 0-9, insert the
	    character "A" and break out of this loop.
         If it is 9 or z or Z, it becomes 0,
	 else increment it and break out of this loop.
	 Back up so the increment character becomes the previous character.
 */
extern char *NextFileName(const char *name);

/* JumpToTime (JumpToCycle) jumps to the nearest time (ncyc) in the
   current record sequence to the specified time (ncyc).  Returns 0
   on success, non-0 on failure.  */
extern int JumpToTime(HistoryInfo *history, double time);
extern int JumpToCycle(HistoryInfo *history, long ncyc);
/* JumpRecord returns 0 on success, 1 on recNumber out of range.
   If recNumber is out of range, there is no current record.  */
extern int JumpRecord(HistoryInfo *history, long recNumber);

/*--------------------------------------------------------------------------*/

/* Parameters for adjusting caching system -- see cache.c */
extern long yMaxBlockSize, yCacheSize, yCacheTotal;
extern int yCacheNumber, yCacheN;

/* copy src from 0 to src->nextAddress to dst, return 0 on success */
extern int CopyFile(IOStream *dst, IOStream *src);

/* FlushFile flushes all cache blocks associated with file, optionally
   discarding them altogether.  */
extern void FlushFile(IOStream *file, int discardCache);

/* YcRead and YcWrite are the lowest level read and write primitives for
   moving data to or from an IOStream.  The addr and len are in bytes.
   All data goes through the cache buffer system defined in cache.c.  */
extern long YcRead(IOStream *file, void *buf, long addr, long len);
extern void YcWrite(IOStream *file, const void *buf, long addr, long len);

/* YRead and YWrite are the high level interface to an IOStream.
   The base->file MUST be non-zero.  In both cases, the void* is an
   in-memory buffer holding (or big enough to hold) number objects of size
   base->model->...->size.  If strider is non-0, number is ignored and
   the disk data referenced will be described by the strider.  (Use the
   Scatter and Gather routines from bcast.h to apply a Strider to arrays
   in memory.)
   These routines perform any data type conversions necessary to move
   foreign formats, strings, or pointers to and from disk.  */
extern void YWrite(void *src, long dst, StructDef *base, long number,
		   const Strider *strider);
extern void YRead(void *dst, long src, StructDef *base, long number,
		  const Strider *strider);

/*--------------------------------------------------------------------------*/

/* CLupdate ensures that file->contentsLog exists and is up to date.  */
extern void CLupdate(IOStream *file);

/* CLopen checks to see if file has a Clog description appended to it,
   returning 0 if it does, in which case the file is updated.  */
extern int CLopen(IOStream *file, int familyOK);

/* CLclose is suitable for use as a CloseHook -- appends Clog description
   to end of file and zaps file->contentsLog.  */
extern void CLclose(IOStream *file);

/* ZapClogFile frees file->contentsLog, and destroys the associated file.  */
extern void ZapClogFile(IOStream *file);

/* FreeClogFile frees file->contentsLog.  */
extern void FreeClogFile(IOStream *file);

/* DumpClogFile creates a Contents Log named clogName, describing file.
   This is completely independent of the file->contentsLog, if any.  */
extern int DumpClogFile(IOStream *file, const char *clogName);

/* ReadClogFile reads a Contents Log named clogName and makes file
   look like the description therein.  (CLopen with a separate file.)
   This is completely independent of the file->contentsLog, if any.  */
extern int ReadClogFile(IOStream *file, const char *clogName);

/* functions to manage file->pointeeList */
extern void ReadPointees(IOStream *file);
extern void WritePointees(IOStream *file);
extern void ClearPointees(IOStream *file, int writing);

/* functions to manage FPLayouts */
extern FPLayout *MakeFPLayout(FPLayout *model, long size);
extern void FreeFPLayout(FPLayout *layout);
extern int SameFPLayout(FPLayout *l, FPLayout *m);

/* ------------------------------------------------------------------------ */

/* YError and YWarning must be supplied by user of binary file package.
   YError should longjump or exit, YWarning may return.  */
extern void YWarning(const char *msg);
extern void YError(const char *msg);

/* ------------------------------------------------------------------------ */

#endif