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 */
233 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
239 if (CallHookA (hook
, stream
, &wr
) == EOF
)
245 case SDT_IFILL_LONG
: { /* Fill x longs */
247 struct BEIOM_Write wr
= {BEIO_WRITE
, 0};
255 if (CallHookA (hook
, stream
, &wr
) == EOF
)
261 case SDT_SPECIAL
: { /* Call user hook */
265 data
.sdd_Dest
= ((APTR
)(curr
->s
+ IDESC
));
266 data
.sdd_Mode
= SDV_SPECIALMODE_WRITE
;
267 data
.sdd_Stream
= stream
;
269 uhook
= (struct Hook
*)IDESC
;
271 if (!CallHookA (uhook
, hook
, &data
))
281 /* End of the description list ? */
284 struct WriteLevel
* last
;
286 /* Remove the current level */
288 Remove ((struct Node
*)last
);
290 /* Get the last level */
291 if ((curr
= (struct WriteLevel
*)GetTail (list
)))
293 FreeMem (last
, sizeof (struct WriteLevel
));
302 FreeMem (curr
, sizeof (struct WriteLevel
));
308 while ((curr
= (struct WriteLevel
*)RemTail (list
)))
309 FreeMem (curr
, sizeof (struct WriteLevel
));