[go: up one dir, main page]

Menu

[r990]: / mia2 / vistaio / fileio.c  Maximize  Restore  History

Download this file

997 lines (819 with data), 24.4 kB

  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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*
* Copyrigh (C) 2004 Max-Planck-Institute of Cognitive Neurosience
*
* The origional VISTA library is copyrighted of University of British Columbia.
* Copyright © 1993, 1994 University of British Columbia.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* $Id: fileio.c 890 2006-03-03 12:48:52Z write1 $ */
/*! \file fileio.c
* \brief reading attributes from files and writing attributes to files
* \author Arthur Pope, UBC Laboratory for Computational Intelligentce
*/
#include <ctype.h>
#include "vista.h"
/*
* Local definitions.
*/
/* Macro used in WriteFile, WriteAttrList, etc.: */
#define FailTest(put) if ((put) == EOF) goto Fail
/*! \struct DataBlock
* \brief Description of object with data block to be written later by VWriteFile
*/
typedef struct {
VAttrListPosn posn; /* identify of object's attribute */
VAttrList list; /* attr list value referring to data */
size_t length; /* length of data block */
} DataBlock;
typedef struct {
char *buf;
size_t max_len;
} ReadStringBuf;
/* Local variables: */
static long offset; /* current offset into file's binary data */
static VList data_list; /* list of data blocks to write later */
/* Later in this file: */
static VBoolean ReadHeader (FILE *);
static VAttrList ReadAttrList (FILE *, ReadStringBuf *);
static char *ReadString (FILE *, char, VStringConst, ReadStringBuf *);
static VBoolean ReadDelimiter (FILE *);
static VBoolean ReadData (FILE *, VAttrList, VReadFileFilterProc *);
static VBoolean WriteAttrList (FILE *, VAttrList, int);
static VBoolean WriteAttr (FILE *, VAttrListPosn *, int);
static VBoolean WriteString (FILE *, const char *);
static VBoolean MySeek (FILE *, long);
static void EmptyShowProgress(int pos, int length, void *data)
{
}
static VShowProgressFunc VShowReadProgress = EmptyShowProgress;
static VShowProgressFunc VShowWriteProgress = EmptyShowProgress;
static void *VProgressData = NULL;
EXPORT_VISTA void VSetProgressIndicator(VShowProgressFunc show_read, VShowProgressFunc show_write, void *data)
{
VShowReadProgress = show_read;
VShowWriteProgress = show_write;
VProgressData = data;
}
EXPORT_VISTA void VResetProgressIndicator(void)
{
VShowReadProgress = EmptyShowProgress;
VShowWriteProgress = EmptyShowProgress;
VProgressData = NULL;
}
/*! \brief Open an input or output file, with "-" representing stdin or stdout.
*
* \param filename
* \param nofail
* \return If nofail is TRUE, any failure is a fatal error.
*/
FILE *VOpenInputFile (VStringConst filename, VBoolean nofail)
{
FILE *f;
if (filename == NULL || strcmp (filename, "-") == 0)
f = stdin;
else if (!(f = fopen (filename, "r")))
(nofail ? &VError : &VWarning)
("Unable to open input file %s", filename);
return f;
}
/*! \brief
*
* \param filename
* \param nofail
* \return FILE
*/
FILE *VOpenOutputFile (VStringConst filename, VBoolean nofail)
{
FILE *f;
if (filename == NULL || strcmp (filename, "-") == 0)
f = stdout;
else if (!(f = fopen (filename, "w")))
(nofail ? &VError : &VWarning)
("Unable to open output file %s", filename);
return f;
}
/*! \brief Read a Vista data file, extract object of a specified type, and
* return a vector of them plus a list of anything else found in the file.
*
* \param file
* \param repn
* \param attributes
* \param objects
* \return int
*/
int VReadObjects (FILE * file, VRepnKind repn, VAttrList * attributes,
VPointer ** objects)
{
VAttrList list;
VAttrListPosn posn;
int i, nobjects = 0;
VPointer *vector;
/* Read the file's contents: */
list = VReadFile (file, NULL);
if (!list)
return FALSE;
/* Count the objects found: */
for (VFirstAttr (list, &posn); VAttrExists (&posn); VNextAttr (&posn))
nobjects += (VGetAttrRepn (&posn) == repn);
if (nobjects == 0) {
VWarning ("VReadObjects: No %s objects present in stream",
VRepnName (repn));
VDestroyAttrList (list);
return FALSE;
}
/* Allocate a vector of that many object pointers: */
vector = VMalloc (nobjects * sizeof (VPointer));
/* Extract the objects from the attribute list and place them in the
vector: */
for (VFirstAttr (list, &posn), i = 0; VAttrExists (&posn);)
if (VGetAttrRepn (&posn) == repn) {
VGetAttrValue (&posn, NULL, repn, vector + i);
VDeleteAttr (&posn);
i++;
} else
VNextAttr (&posn);
/* Return the objects and the remaining attributes: */
*attributes = list;
*objects = vector;
return nobjects;
}
/*! \brief Read a Vista data file, returning an attribute list of its contents.
*
* \param f
* \param filter
* \return VAttrList
*/
EXPORT_VISTA VAttrList VReadFile (FILE * f, VReadFileFilterProc * filter)
{
VAttrList list;
int i;
ReadStringBuf sbuf = {0,0};
#ifdef SupportUbcIff
/* If the first byte of the file is "I", it looks like a UBC IFF file: */
{
i = fgetc (f);
ungetc (i, f);
if (i == 'I') {
VImage image = VReadUbcIff (f);
if (!image)
return FALSE;
list = VCreateAttrList ();
VSetAttr (list, "image", NULL, VImageRepn, image);
return list;
}
}
#endif
/* Ensure that the correct Vista data file header is there: */
if (!ReadHeader (f))
return NULL;
list = ReadAttrList (f, &sbuf);
if (sbuf.max_len)
VFree(sbuf.buf);
/* Read all attributes in the file: */
if (!list)
return NULL;
/* Swallow the delimiter and read the binary data following it: */
offset = 0;
if (!ReadDelimiter (f) || !ReadData (f, list, filter)) {
VDestroyAttrList (list);
return NULL;
}
/* Now we should be at the end of the file: */
i = fgetc (f);
if (i != EOF) {
ungetc (i, f);
VWarning ("VReadFile: File continues beyond expected EOF");
}
return list;
}
/*! \brief Read a Vista data file header.
*
* If the current version is 2 but the file is version 1, a warning message
* is produced but TRUE is still returned.
*
* \param f
* \return VBoolean
*/
static VBoolean ReadHeader (FILE * f)
{
int version;
if (fscanf (f, VFileHeader " %d", &version) != 1) {
VWarning ("VReadFile: Vista data file header not found");
return FALSE;
}
if (version == VFileVersion)
return TRUE;
if (version == 1 && VFileVersion == 2) {
VWarning ("VReadFile: Obsolete data file -- pipe it thru v1to2");
return TRUE;
}
VWarning ("VReadFile: Vista data file isn't version %d",
VFileVersion);
return FALSE;
}
/*
* ReadAttrList
*
* Read a list of attributes from a stream.
*/
static VAttrList ReadAttrList (FILE * f, ReadStringBuf *sbuf)
{
VAttrList sublist, list = VCreateAttrList ();
VAttrRec *a;
int ch = 0;
size_t name_size;
VBundle b;
char buf[2], *str, name_buf[VMaxAttrNameLength + 1];
/* Swallow a { marking the start of the attribute list: */
if (fscanf (f, " %1s", buf) != 1 || buf[0] != '{') {
VWarning ("VReadFile: Missing {");
goto Fail;
}
/* For each attribute up to the next "}": */
while (fscanf (f, " %255[^}: \t\n]", name_buf) == 1) {
name_size = strlen (name_buf);
/* Read a : */
if (fscanf (f, " %1s", buf) != 1 || buf[0] != ':') {
VWarning ("VReadFile: Invalid %s attribute", name_buf);
goto Fail;
}
do {
buf[0] = fgetc(f);
} while (buf[0] == ' ');
/* The first character of the value tells us whether its an attribute
list, quoted string, or unquoted string: */
if (buf[0] == '{') {
/* The attribute value is another list of attributes: */
ungetc ('{', f);
if (!(sublist = ReadAttrList (f, sbuf)))
goto Fail;
a = VMalloc (sizeof (VAttrRec) + name_size);
a->value = sublist;
a->repn = VAttrListRepn;
} else if (buf[0] != '}' && buf[0] != '\n') {// non-empty attr
/* The value doesn't start with '{' -- parse a word or string: */
if (!(str = ReadString (f, buf[0], name_buf, sbuf)))
goto Fail;
while ((ch = fgetc (f)) && (ch == ' ' || ch == '\t'));
ungetc (ch, f);
/* If the word is followed by an '{'... */
if (ch == '{') {
/* ...then it's a typed value -- the word is it's type name
and the { is the start of it's attribute list value. */
b = VCreateBundle (str, NULL, 0, NULL);
if (!(sublist = ReadAttrList (f, sbuf))) {
VFree (b);
goto Fail;
}
b->list = sublist;
a = VMalloc (sizeof (VAttrRec) + name_size);
a->repn = VBundleRepn;
a->value = b;
} else {
/* ...otherwise store it as a simple string value: */
a = VMalloc (sizeof (VAttrRec) + name_size +
strlen (str) + 1);
a->repn = VStringRepn;
a->value = a->name + name_size + 1;
strcpy (a->value, str);
}
}else{
ungetc (buf[0], f);
a = VMalloc (sizeof (VAttrRec) + name_size +
strlen ("") + 1);
a->repn = VStringRepn;
a->value = a->name + name_size + 1;
strcpy (a->value, "");
}
/* Copy the attribute's name into the newly allocated node: */
strcpy (a->name, name_buf);
/* Place the new node on the end of the growing attribute list: */
a->next = NULL;
if ((a->prev = list->prev))
a->prev->next = a;
else
list->next = a;
list->prev = a;
}
/* Swallow the terminating "}": */
if (fscanf (f, " %1s", buf) != 1 || buf[0] != '}') {
VWarning ("VReadFile: Missing }");
Fail:VDestroyAttrList (list);
return NULL;
}
return list;
}
/*
* ReadString
*
* Read an word, string, or number that may or may not be quoted.
* If ch is " then parse a quoted string, otherwise parse to the next
* whitespace character. The string is returned in a reusable buffer.
*
* The first several characters are read into a fixed-sized buffer
* large enough to hold most values. When it overflows, we begin
* allocating and reallocating larger buffers as we continue to read
* the value.
*/
#define StringAllocIncrement 100 /* each round of buffer resizing */
static char *ReadString (FILE * f, char ch, VStringConst name, ReadStringBuf *buf)
{
VBoolean escaped = (ch == '"');
size_t len = 0;
char *cp;
if (!buf->buf) {
buf->buf = VMalloc (StringAllocIncrement);
buf->max_len = StringAllocIncrement;
}
if (!escaped)
ungetc (ch, f);
cp = buf->buf;
while (1) {
ch = fgetc (f);
/* Check for premature EOF: */
if (ch == EOF) {
VWarning ("VReadFile: EOF encountered in %s attribute", name);
return NULL;
}
/* Check for closing " or escape sequence: */
if (escaped) {
if (ch == '"')
break;
if (ch == '\\')
switch (ch = fgetc (f)) {
case '\n':
continue;
case 'n':
ch = '\n';
}
} else if (isspace (ch))
break;
/* If the buffer in which we're accumulating the value is full,
allocate a larger one: */
if (++len == buf->max_len) {
buf->buf = VRealloc (buf->buf, buf->max_len += StringAllocIncrement);
cp = buf->buf + len - 1;
}
/* Store the character in the current buffer: */
*cp++ = ch;
}
*cp = 0;
/* Allocate a node of the correct size, or trim one already allocated so
it is the correct size: */
return buf->buf;
}
/*
* ReadDelimiter
*
* Read a Vista data file delimiter.
*/
static VBoolean ReadDelimiter (FILE * f)
{
int ch;
const char *cp;
static char *msg = "VReadFile: Vista data file delimiter not found";
/* Skip whitespace up to the first character of the delimeter: */
while ((ch = fgetc (f)) != VFileDelimiter[0])
if (ch == EOF || !isspace (ch & 0x7F)) {
VWarning (msg);
return FALSE;
}
/* Swallow remaining characters of the delimiter: */
for (cp = &VFileDelimiter[1]; *cp; cp++)
if (*cp != fgetc (f)) {
VWarning (msg);
return FALSE;
}
return TRUE;
}
/*
* ReadData
*
* Read the binary data accompanying attributes.
*/
static VBoolean ReadData (FILE * f, VAttrList list,
VReadFileFilterProc * filter)
{
VAttrListPosn posn, subposn;
VAttrList sublist;
VBundle b;
VRepnKind repn;
VBoolean read_data, data_found, length_found;
VLong data, length;
VTypeMethods *methods;
VPointer value;
for (VFirstAttr (list, &posn); VAttrExists (&posn); VNextAttr (&posn)) {
switch (VGetAttrRepn (&posn)) {
case VAttrListRepn:
/* Recurse on nested attribute list: */
VGetAttrValue (&posn, NULL, VAttrListRepn, &sublist);
if (!ReadData (f, sublist, filter))
return FALSE;
break;
case VBundleRepn:
VGetAttrValue (&posn, NULL, VBundleRepn, &b);
repn = VLookupType (b->type_name);
/* If a filter routine was supplied, ask it whether to bother
with the binary data: */
read_data = !filter || (*filter) (b, repn);
/* Extract any data and length attributes in the object's value: */
if ((data_found =
VLookupAttr (b->list, VDataAttr, &subposn))) {
if (!VGetAttrValue
(&subposn, NULL, VLongRepn, &data)) {
VWarning ("VReadFile: "
"%s attribute's data attribute incorrect",
VGetAttrName (&posn));
return FALSE;
}
VDeleteAttr (&subposn);
}
if ((length_found =
VLookupAttr (b->list, VLengthAttr, &subposn))) {
if (!(VGetAttrValue
(&subposn, NULL, VLongRepn, &length))) {
VWarning ("VReadFile: "
"%s attribute's length attribute incorrect",
VGetAttrName (&posn));
return FALSE;
}
VDeleteAttr (&subposn);
}
/* None or both must be present: */
if (data_found ^ length_found) {
VWarning ("VReadFile: %s attribute has %s but not %s", VGetAttrName (&posn), data_found ? "data" : "length", data_found ? "length" : "data");
return FALSE;
}
/* Read the binary data associated with the object: */
if (data_found) {
if (data < offset) {
VWarning ("VReadFile: "
"%s attribute's data attribute incorrect",
VGetAttrName (&posn));
return FALSE;
}
if (!read_data)
data += length;
/* To seek forward to the start of the data block we first
try fseek. That will fail on a pipe, in which case we
seek by reading. */
if (data != offset &&
fseek (f, (long)data - offset,
SEEK_CUR) == -1 && errno == ESPIPE
&& !MySeek (f, data - offset)) {
VSystemWarning
("VReadFile: Seek within file failed");
return FALSE;
}
if (read_data) {
int pos = 0;
int togo = length;
b->data = VMalloc (b->length = length);
if (!b->data) {
VWarning ("VReadFile: unable to allocate memory");
return FALSE;
}
while (togo) {
int read_length = togo < 100000 ? togo : 100000;
if (fread ((char *)(b->data) + pos, 1,
read_length, f) != read_length) {
VWarning ("VReadFile: Read from stream failed");
return FALSE;
}
pos += read_length;
togo -= read_length;
VShowReadProgress(pos, length, VProgressData);
}
offset = data + length;
} else
/* bug: read error occured when bundle was not read
by a filter function. FK 24/03/98 */
offset = data;
}
/* Recurse to read binary data for sublist attributes: */
if (!ReadData (f, b->list, filter))
return FALSE;
/* If the object's type is registered and has a decode method,
invoke it to decode the binary data: */
if (read_data && repn != VUnknownRepn &&
(methods = VRepnMethods (repn))
&& methods->decode) {
if (!
(value =
(methods->decode) (VGetAttrName (&posn),
b)))
return FALSE;
/* Replace the old typed value with the newly decoded one: */
VSetAttrValue (&posn, NULL, repn, value);
VDestroyBundle (b);
}
break;
default:
break;
}
}
return TRUE;
}
/*! \brief Write a list of objects, plus some other attributes, to a Vista data file.
*
* \param file
* \param repn
* \param attributes
* \param nobjects
* \param objects
* \return VBoolean
*/
VBoolean VWriteObjects (FILE * file, VRepnKind repn, VAttrList attributes,
int nobjects, VPointer objects[])
{
VAttrList list;
VAttrListPosn posn;
int i;
VBoolean result;
/* Create an attribute list if none was supplied: */
list = attributes ? attributes : VCreateAttrList ();
/* Prepend to the attribute list an attribute for each object: */
for (i = nobjects - 1; i >= 0; i--)
VPrependAttr (list, VRepnName (repn), NULL, repn, objects[i]);
/* Write the attribute list: */
result = VWriteFile (file, list);
/* Remove the attributes just prepended: */
VFirstAttr (list, &posn);
for (i = 0; i < nobjects; i++)
VDeleteAttr (&posn);
if (list != attributes)
VDestroyAttrList (list);
return result;
}
/*! \brief VWriteFile
*
* \param f
* \param list
* \return VBoolean
*/
EXPORT_VISTA VBoolean VWriteFile (FILE * f, VAttrList list)
{
DataBlock *db;
VBundle b;
VTypeMethods *methods;
VRepnKind repn;
VPointer value, ptr;
VBoolean free_it;
VBoolean result = 0;
/* Write the Vista data file header, attribute list, and delimeter
while queuing on data_list any binary data blocks to be written: */
offset = 0;
data_list = VListCreate ();
FailTest (fprintf (f, "%s %d ", VFileHeader, VFileVersion));
if (!WriteAttrList (f, list, 1)) {
VListDestroy (data_list, VFree);
return FALSE;
}
FailTest (fputs ("\n" VFileDelimiter, f));
fflush (f);
/* Traverse data_list to write the binary data blocks: */
for (db = VListFirst (data_list); db; db = VListNext (data_list)) {
repn = VGetAttrRepn (&db->posn);
if (repn == VBundleRepn) {
/* A typed value includes its binary data block explicitly: */
VGetAttrValue (&db->posn, NULL, VBundleRepn, &b);
ptr = b->data;
free_it = FALSE;
} else {
/* For any other representation, obtain the binary data block
from its encode_data method: */
VGetAttrValue (&db->posn, NULL, repn, &value);
methods = VRepnMethods (repn);
ptr = (methods->encode_data)
(value, db->list, db->length, &free_it);
if (!ptr)
goto Fail;
}
/* Write the binary data and free the buffer containing it if it was
allocated temporarily by an encode_data method: */
if (db->length > 0) {
int togo = db->length;
int pos = 0;
while (togo) {
int write_length = togo < 100000 ? togo: 100000;
result = fwrite (((char *)ptr) + pos, 1, write_length, f) == write_length;
pos += write_length;
togo -= write_length;
VShowWriteProgress(pos, db->length, VProgressData);
}
if (free_it)
VFree (ptr);
if (!result)
goto Fail;
}
}
VListDestroy (data_list, VFree);
return TRUE;
Fail:
VWarning ("VWriteFile: Write to stream failed");
VListDestroy (data_list, VFree);
return FALSE;
}
/*! \brief Write a list of attributes to a file. Lines are indented by indent tabs.
*
* \param f
*/
static VBoolean WriteAttrList (FILE * f, VAttrList list, int indent)
{
VAttrListPosn posn;
int i;
/* Write the { marking the beginning of the attribute list: */
FailTest (fputs ("{\n", f));
/* Write each attribute in the list: */
for (VFirstAttr (list, &posn); VAttrExists (&posn); VNextAttr (&posn))
if (!WriteAttr (f, &posn, indent))
return FALSE;
/* Write the } marking the end of the attribute list: */
for (i = indent - 1; i > 0; i--)
FailTest (fputc ('\t', f));
FailTest (fputc ('}', f));
return TRUE;
Fail:
VWarning ("VWriteFile: Write to stream failed");
return FALSE;
}
/*
* WriteAttr
*
* Write a single attribute to a file. If the attribute's value is
* itself an attribute list, it is indented by indent+1 tabs.
*/
static VBoolean WriteAttr (FILE * f, VAttrListPosn * posn, int indent)
{
int i;
char *str;
VRepnKind repn;
VAttrList sublist;
VBundle b;
DataBlock *db;
VTypeMethods *methods;
size_t length;
VPointer value;
VBoolean result;
VAttrListPosn subposn;
/* Indent by the specified amount: */
for (i = 0; i < indent; i++)
FailTest (fputc ('\t', f));
indent++;
/* Output the attribute's name: */
FailTest (fprintf (f, "%s: ", VGetAttrName (posn)));
/* Ouput its value: */
switch (repn = VGetAttrRepn (posn)) {
case VAttrListRepn:
VGetAttrValue (posn, NULL, VAttrListRepn,
(VPointer) & sublist);
result = WriteAttrList (f, sublist, indent);
break;
case VBundleRepn:
VGetAttrValue (posn, NULL, VBundleRepn, (VBundle) & b);
if (!WriteString (f, b->type_name))
return FALSE;
FailTest (fputc (' ', f));
/* If it's a typed value with binary data... */
if (b->length > 0) {
/* Include "data" and "length" attributes in its attribute list: */
VPrependAttr (b->list, VLengthAttr, NULL, VLongRepn,
(VLong) b->length);
VPrependAttr (b->list, VDataAttr, NULL, VLongRepn,
(VLong) offset);
/* Add it to the queue of binary data blocks to be written: */
offset += b->length;
db = VNew (DataBlock);
db->posn = *posn;
db->list = b->list;
db->length = b->length;
VListAppend (data_list, db);
}
/* Write the typed value's attribute list: */
result = WriteAttrList (f, b->list, indent);
/* Remove the "data" and "length" attributes added earlier: */
if (b->length > 0) {
VFirstAttr (b->list, &subposn);
VDeleteAttr (&subposn);
VDeleteAttr (&subposn);
}
break;
case VStringRepn:
VGetAttrValue (posn, NULL, VStringRepn, (VPointer) & str);
result = WriteString (f, str);
break;
default:
if (!(methods = VRepnMethods (repn)) ||
!methods->encode_attr || !methods->encode_data) {
VWarning ("VWriteFile: "
"%s attribute has unwriteable representation: %s",
VGetAttrName (posn), VRepnName (repn));
return FALSE;
}
/* Write the type name: */
if (!WriteString (f, VRepnName (repn)))
return FALSE;
FailTest (fputc (' ', f));
/* Invoke the object type's encode_attr method to obtain an
attribute list: */
VGetAttrValue (posn, NULL, repn, &value);
sublist = (methods->encode_attr) (value, &length);
/* If binary data is indicated... */
if (length > 0) {
/* Include "data" and "length" attributes in the attr list: */
VPrependAttr (sublist, VLengthAttr, NULL, VLongRepn,
(VLong) length);
VPrependAttr (sublist, VDataAttr, NULL, VLongRepn,
(VLong) offset);
offset += length;
}
/* Add the object to the queue of binary data blocks to be written: */
db = VNew (DataBlock);
db->posn = *posn;
db->list = sublist;
db->length = length;
VListAppend (data_list, db);
/* Write the typed value's attribute list: */
result = WriteAttrList (f, sublist, indent);
/* Remove the "data" and "length" attributes added earlier: */
if (length > 0) {
VFirstAttr (sublist, &subposn);
VDeleteAttr (&subposn);
VDeleteAttr (&subposn);
}
}
/* Output a trailing newline: */
if (result)
FailTest (fputc ('\n', f));
return result;
Fail:
VWarning ("VWriteFile: Write to stream failed");
return FALSE;
}
/*
* WriteString
*
* Write a string, quoting it if necessary.
*/
static VBoolean WriteString (FILE * f, const char *str)
{
const char *cp;
int ch;
/* Test for the presence of funny characters in the string value: */
for (cp = str; (ch = *cp); cp++)
if (!(isalnum (ch)) && (ch != '.') && (ch != '-') && (ch != '+')
&& (ch != '_'))
break;
/* If funny characters are present, output the string in quotes: */
if (ch) {
FailTest (fputc ('"', f));
for (cp = (char *)str; (ch = *cp); cp++)
switch (ch) {
case '\n':
FailTest (fputs ("\\n\\\n", f));
break;
case '"':
FailTest (fputs ("\\\"", f));
break;
default:
FailTest (fputc (ch, f));
}
FailTest (fputc ('"', f));
} else
FailTest (fputs (str, f));
return TRUE;
Fail:
VWarning ("VWriteFile: Write to stream failed");
return FALSE;
}
/*
* MySeek
*
* Seek forward in a stream by reading it. Needed because fseek() won't
* work on pipes.
*/
static VBoolean MySeek (FILE * f, long bytes)
{
int len;
char buf[1000];
while (bytes > 0) {
len = VMin (bytes, sizeof (buf));
if (fread (buf, 1, len, f) != len)
return FALSE;
bytes -= len;
}
return TRUE;
}