2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: Write a big endian structure to a streamhook
10 #include <exec/memory.h>
11 #include <proto/dos.h>
12 #include <proto/exec.h>
13 #include <aros/debug.h>
14 #include <utility/hooks.h>
24 /******************************************************************************
28 #include <aros/bigendianio.h>
29 #include <proto/alib.h>
40 Writes one big endian structure to a streamhook.
43 hook - Write to this streamhook
44 data - Data to be written
45 stream - Stream passed to streamhook
46 sd - Description of the structure to be written. The first element
47 is the size of the structure.
50 The function returns TRUE on success and FALSE otherwise. In error,
51 you can examine IoErr() to find out what was wrong.
54 This function writes big endian values to a file even on little
63 The function uses the Write*()-functions to write data into
66 Pointers are written as <valid><data structure>, where valid is
67 a byte with the values 1 (then the full data structure follows)
68 or 0 (then nothing follows and the pointer will be intialized as
69 NULL when the structure is read back).
72 ReadByte(), ReadWord(), ReadLong(), ReadFloat(), ReadDouble(),
73 ReadString(), ReadStruct(), WriteByte(), WriteWord(), WriteLong(),
74 WriteFloat(), WriteDouble(), WriteString(), WriteStruct()
78 ******************************************************************************/
81 struct WriteLevel
* curr
;
83 # define list ((struct List *)&_list)
87 if (!(curr
= AllocMem (sizeof (struct WriteLevel
), MEMF_ANY
)) )
90 AddTail (list
, (struct Node
*)curr
);
93 curr
->pos
= 1; /* Ignore size */
96 # define DESC (curr->sd[curr->pos])
97 # define IDESC (curr->sd[curr->pos ++])
99 while (DESC
!= SDT_END
)
103 case SDT_UBYTE
: /* Write one 8bit byte */
104 if (!WriteByte (hook
, *((UBYTE
*)(curr
->s
+ IDESC
)), stream
))
109 case SDT_UWORD
: /* Write one 16bit word */
110 if (!WriteWord (hook
, *((UWORD
*)(curr
->s
+ IDESC
)), stream
))
115 case SDT_ULONG
: /* Write one 32bit long */
116 if (!WriteLong (hook
, *((ULONG
*)(curr
->s
+ IDESC
)), stream
))
121 case SDT_FLOAT
: /* Write one 32bit IEEE */
122 if (!WriteFloat (hook
, *((FLOAT
*)(curr
->s
+ IDESC
)), stream
))
127 case SDT_DOUBLE
: /* Write one 64bit IEEE */
128 if (!WriteDouble (hook
, *((DOUBLE
*)(curr
->s
+ IDESC
)), stream
))
133 case SDT_STRING
: { /* Write a string */
136 str
= *((STRPTR
*)(curr
->s
+ IDESC
));
140 if (!WriteByte (hook
, 1, stream
))
143 if (!WriteString (hook
, str
, stream
))
148 if (!WriteByte (hook
, 0, stream
))
156 case SDT_STRUCT
: { /* Write a structure */
157 struct WriteLevel
* next
;
162 ptr
= (APTR
)(curr
->s
+ IDESC
);
163 desc
= (IPTR
*)IDESC
;
165 if (!(next
= AllocMem (sizeof (struct WriteLevel
), MEMF_ANY
)) )
168 AddTail (list
, (struct Node
*)next
);
170 next
->pos
= 1; /* Ignore size */
177 case SDT_PTR
: { /* Follow a pointer */
178 struct WriteLevel
* next
;
183 ptr
= *((APTR
*)(curr
->s
+ IDESC
));
184 desc
= (IPTR
*)IDESC
;
188 if (!WriteByte (hook
, 1, stream
))
191 if (!(next
= AllocMem (sizeof (struct WriteLevel
), MEMF_ANY
)) )
194 AddTail (list
, (struct Node
*)next
);
203 if (!WriteByte (hook
, 0, stream
))
211 case SDT_IGNORE
: { /* Ignore x bytes */
213 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
219 if (CallHookA (hook
, stream
, &wr
) == EOF
)
225 case SDT_FILL_BYTE
: /* Fill x bytes */
226 case SDT_FILL_LONG
: /* Fill x longs */
230 case SDT_IFILL_BYTE
: { /* Fill x bytes */
235 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
243 if (CallHookA (hook
, stream
, &wr
) == EOF
)
249 case SDT_IFILL_LONG
: { /* Fill x longs */
253 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
263 if (CallHookA (hook
, stream
, &wr
) == EOF
)
269 case SDT_SPECIAL
: { /* Call user hook */
273 data
.sdd_Dest
= ((APTR
)(curr
->s
+ IDESC
));
274 data
.sdd_Mode
= SDV_SPECIALMODE_WRITE
;
275 data
.sdd_Stream
= stream
;
277 uhook
= (struct Hook
*)IDESC
;
279 CallHookA (uhook
, hook
, &data
);
288 /* End of the description list ? */
291 struct WriteLevel
* last
;
293 /* Remove the current level */
295 Remove ((struct Node
*)last
);
297 /* Get the last level */
298 if ((curr
= (struct WriteLevel
*)GetTail (list
)))
300 FreeMem (last
, sizeof (struct WriteLevel
));
309 FreeMem (curr
, sizeof (struct WriteLevel
));
315 while ((curr
= (struct WriteLevel
*)RemTail (list
)))
316 FreeMem (curr
, sizeof (struct WriteLevel
));