Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/freealut-1.1.0/src/alutOutputStream.c @ 14

Last change on this file since 14 was 14, checked in by landauf, 16 years ago

added freealut

File size: 2.3 KB
Line 
1#include "alutInternal.h"
2#include <string.h>
3
4struct OutputStream_struct
5{
6  char *data;
7  char *current;
8  size_t maximumLength;
9};
10
11/****************************************************************************
12 * The functions below know the internal OutputStream representation.
13 ****************************************************************************/
14
15OutputStream *
16_alutOutputStreamConstruct (size_t maximumLength)
17{
18  OutputStream *stream = (OutputStream *) _alutMalloc (sizeof (OutputStream));
19  if (stream == NULL)
20    {
21      return NULL;
22    }
23  stream->data = _alutMalloc (maximumLength);
24  if (stream->data == NULL)
25    {
26      free (stream);
27      return NULL;
28    }
29  stream->current = stream->data;
30  stream->maximumLength = maximumLength;
31  return stream;
32}
33
34ALboolean
35_alutOutputStreamDestroy (OutputStream *stream)
36{
37  free (stream->data);
38  free (stream);
39  return AL_TRUE;
40}
41
42void *
43_alutOutputStreamGetData (OutputStream *stream)
44{
45  return stream->data;
46}
47
48size_t
49_alutOutputStreamGetLength (OutputStream *stream)
50{
51  return stream->current - stream->data;
52}
53
54static ALboolean
55streamWrite (OutputStream *stream, const void *ptr, size_t numBytesToWrite)
56{
57  size_t remainingLength =
58    stream->maximumLength - _alutOutputStreamGetLength (stream);
59  if (remainingLength < numBytesToWrite)
60    {
61      /* this should never happen within our library */
62      _alutSetError (ALUT_ERROR_IO_ERROR);
63      return AL_FALSE;
64    }
65  memcpy (stream->current, ptr, numBytesToWrite);
66  stream->current += numBytesToWrite;
67  return AL_TRUE;
68}
69
70/****************************************************************************
71 * The utility functions below do not know the internal OutputStream
72 * representation.
73 ****************************************************************************/
74
75ALboolean
76_alutOutputStreamWriteInt16BE (OutputStream *stream, Int16BigEndian value)
77{
78  unsigned char buf[2];
79  buf[0] = (unsigned char) (value >> 8);
80  buf[1] = (unsigned char) value;
81  return streamWrite (stream, buf, 2);
82}
83
84ALboolean
85_alutOutputStreamWriteInt32BE (OutputStream *stream, Int32BigEndian value)
86{
87  unsigned char buf[4];
88  buf[0] = (unsigned char) (value >> 24);
89  buf[1] = (unsigned char) (value >> 16);
90  buf[2] = (unsigned char) (value >> 8);
91  buf[3] = (unsigned char) value;
92  return streamWrite (stream, buf, 4);
93}
Note: See TracBrowser for help on using the repository browser.