2 Copyright © 1995-2018, 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
61 SDT_SPECIAL not implemented.
64 The function uses the Write*()-functions to write data into
67 Pointers are written as <valid><data structure>, where valid is
68 a byte with the values 1 (then the full data structure follows)
69 or 0 (then nothing follows and the pointer will be intialized as
70 NULL when the structure is read back).
73 ReadByte(), ReadWord(), ReadLong(), ReadFloat(), ReadDouble(),
74 ReadString(), ReadStruct(), WriteByte(), WriteWord(), WriteLong(),
75 WriteFloat(), WriteDouble(), WriteString(), WriteStruct()
79 ******************************************************************************/
82 struct WriteLevel
* curr
;
84 # define list ((struct List *)&_list)
88 if (!(curr
= AllocMem (sizeof (struct WriteLevel
), MEMF_ANY
)) )
91 AddTail (list
, (struct Node
*)curr
);
94 curr
->pos
= 1; /* Ignore size */
97 # define DESC (curr->sd[curr->pos])
98 # define IDESC (curr->sd[curr->pos ++])
100 while (DESC
!= SDT_END
)
104 case SDT_UBYTE
: /* Write one 8bit byte */
105 if (!WriteByte (hook
, *((UBYTE
*)(curr
->s
+ IDESC
)), stream
))
110 case SDT_UWORD
: /* Write one 16bit word */
111 if (!WriteWord (hook
, *((UWORD
*)(curr
->s
+ IDESC
)), stream
))
116 case SDT_ULONG
: /* Write one 32bit long */
117 if (!WriteLong (hook
, *((ULONG
*)(curr
->s
+ IDESC
)), stream
))
122 case SDT_FLOAT
: /* Write one 32bit IEEE */
123 if (!WriteFloat (hook
, *((FLOAT
*)(curr
->s
+ IDESC
)), stream
))
128 case SDT_DOUBLE
: /* Write one 64bit IEEE */
129 if (!WriteDouble (hook
, *((DOUBLE
*)(curr
->s
+ IDESC
)), stream
))
134 case SDT_COPY
: { /* Copy 'n' bytes */
135 int i
, count
, offset
;
140 for (i
=0; i
<count
; i
++)
142 if (!WriteByte (hook
, *((UBYTE
*)(curr
->s
+ offset
+ i
)), stream
))
148 case SDT_STRING
: { /* Write a string */
151 str
= *((STRPTR
*)(curr
->s
+ IDESC
));
155 if (!WriteByte (hook
, 1, stream
))
158 if (!WriteString (hook
, str
, stream
))
163 if (!WriteByte (hook
, 0, stream
))
171 case SDT_STRUCT
: { /* Write a structure */
172 struct WriteLevel
* next
;
177 ptr
= (APTR
)(curr
->s
+ IDESC
);
178 desc
= (IPTR
*)IDESC
;
180 if (!(next
= AllocMem (sizeof (struct WriteLevel
), MEMF_ANY
)) )
183 AddTail (list
, (struct Node
*)next
);
185 next
->pos
= 1; /* Ignore size */
192 case SDT_PTR
: { /* Follow a pointer */
193 struct WriteLevel
* next
;
198 ptr
= *((APTR
*)(curr
->s
+ IDESC
));
199 desc
= (IPTR
*)IDESC
;
203 if (!WriteByte (hook
, 1, stream
))
206 if (!(next
= AllocMem (sizeof (struct WriteLevel
), MEMF_ANY
)) )
209 AddTail (list
, (struct Node
*)next
);
218 if (!WriteByte (hook
, 0, stream
))
226 case SDT_IGNORE
: { /* Ignore x bytes */
228 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
234 if (CallHookA (hook
, stream
, &wr
) == EOF
)
240 case SDT_FILL_BYTE
: /* Fill x bytes */
241 case SDT_FILL_LONG
: /* Fill x longs */
245 case SDT_IFILL_BYTE
: { /* Fill x bytes */
248 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
254 if (CallHookA (hook
, stream
, &wr
) == EOF
)
260 case SDT_IFILL_LONG
: { /* Fill x longs */
262 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
270 if (CallHookA (hook
, stream
, &wr
) == EOF
)
276 case SDT_SPECIAL
: { /* Call user hook */
280 data
.sdd_Dest
= ((APTR
)(curr
->s
+ IDESC
));
281 data
.sdd_Mode
= SDV_SPECIALMODE_WRITE
;
282 data
.sdd_Stream
= stream
;
284 uhook
= (struct Hook
*)IDESC
;
286 if (!CallHookA (uhook
, hook
, &data
))
296 /* End of the description list ? */
299 struct WriteLevel
* last
;
301 /* Remove the current level */
303 Remove ((struct Node
*)last
);
305 /* Get the last level */
306 if ((curr
= (struct WriteLevel
*)GetTail (list
)))
308 FreeMem (last
, sizeof (struct WriteLevel
));
317 FreeMem (curr
, sizeof (struct WriteLevel
));
323 while ((curr
= (struct WriteLevel
*)RemTail (list
)))
324 FreeMem (curr
, sizeof (struct WriteLevel
));