File: alutInputStream.c

package info (click to toggle)
freealut 1.1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,144 kB
  • sloc: sh: 8,617; ansic: 2,738; makefile: 89
file content (249 lines) | stat: -rw-r--r-- 5,696 bytes parent folder | download | duplicates (5)
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
#include "alutInternal.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#if HAVE_STAT
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#define structStat struct stat
#elif HAVE__STAT
#define stat(p,b) _stat((p),(b))
#define structStat struct _stat
#else
#error No stat-like function on this platform
#endif

struct InputStream_struct
{
  ALboolean isFileStream;
  char *fileName;
  size_t remainingLength;
  union
  {
    FILE *fileDescriptor;       /* for file streams */
    const ALvoid *data;         /* for memory streams */
  } u;
};

/****************************************************************************
 * The functions below know the internal InputStream representation.
 ****************************************************************************/

InputStream *
_alutInputStreamConstructFromFile (const char *fileName)
{
  InputStream *stream;
  structStat statBuf;
  FILE *fileDescriptor;
  char *fileNameBuffer;

  stream = (InputStream *) _alutMalloc (sizeof (InputStream));
  if (stream == NULL)
    {
      return NULL;
    }

  if (stat (fileName, &statBuf))
    {
      _alutSetError (ALUT_ERROR_IO_ERROR);
      free (stream);
      return NULL;
    }

  fileDescriptor = fopen (fileName, "rb");
  if (fileDescriptor == NULL)
    {
      _alutSetError (ALUT_ERROR_IO_ERROR);
      free (stream);
      return NULL;
    }

  fileNameBuffer = (char *) _alutMalloc (strlen (fileName) + 1);
  if (fileNameBuffer == NULL)
    {
      free (stream);
      return NULL;
    }

  stream->isFileStream = AL_TRUE;
  stream->fileName = strcpy (fileNameBuffer, fileName);
  stream->remainingLength = statBuf.st_size;
  stream->u.fileDescriptor = fileDescriptor;
  return stream;
}

InputStream *
_alutInputStreamConstructFromMemory (const ALvoid *data, size_t length)
{
  InputStream *stream = (InputStream *) _alutMalloc (sizeof (InputStream));
  if (stream == NULL)
    {
      return NULL;
    }

  stream->isFileStream = AL_FALSE;
  stream->fileName = NULL;
  stream->remainingLength = length;
  stream->u.data = data;
  return stream;
}

ALboolean
_alutInputStreamDestroy (InputStream *stream)
{
  ALboolean status =
    (stream->isFileStream && fclose (stream->u.fileDescriptor)) ?
    AL_FALSE : AL_TRUE;
  if (stream->fileName)
    {
      free (stream->fileName);
    }
  free (stream);
  return status;
}

const char *
_alutInputStreamGetFileName (const InputStream *stream)
{
  return stream->fileName;
}

size_t
_alutInputStreamGetRemainingLength (const InputStream *stream)
{
  return stream->remainingLength;
}

ALboolean
_alutInputStreamEOF (InputStream *stream)
{
  if (stream->isFileStream)
    {
      int c = fgetc (stream->u.fileDescriptor);
      if (c != EOF)
        {
          ungetc (c, stream->u.fileDescriptor);
        }
      return (c == EOF) ? AL_TRUE : AL_FALSE;
    }
  else
    {
      return (stream->remainingLength == 0) ? AL_TRUE : AL_FALSE;
    }
}

static ALboolean
streamRead (InputStream *stream, void *ptr, size_t numBytesToRead)
{
  if (stream->isFileStream)
    {
      size_t numBytesRead =
        fread (ptr, 1, numBytesToRead, stream->u.fileDescriptor);
      if (numBytesToRead != numBytesRead)
        {
          _alutSetError (ferror (stream->u.fileDescriptor) ?
                         ALUT_ERROR_IO_ERROR :
                         ALUT_ERROR_CORRUPT_OR_TRUNCATED_DATA);
          return AL_FALSE;
        }
      return AL_TRUE;
    }
  else
    {
      if (stream->remainingLength < numBytesToRead)
        {
          _alutSetError (ALUT_ERROR_CORRUPT_OR_TRUNCATED_DATA);
          return AL_FALSE;
        }
      memcpy (ptr, stream->u.data, numBytesToRead);
      stream->u.data = ((const char *) (stream->u.data) + numBytesToRead);
      return AL_TRUE;
    }
}

/****************************************************************************
 * The utility functions below do not know the internal InputStream
 * representation.
 ****************************************************************************/

ALvoid *
_alutInputStreamRead (InputStream *stream, size_t length)
{
  ALvoid *data = _alutMalloc (length);
  if (data == NULL)
    {
      return NULL;
    }

  if (!streamRead (stream, data, length))
    {
      free (data);
      return NULL;
    }

  return data;
}

ALboolean
_alutInputStreamSkip (InputStream *stream, size_t numBytesToSkip)
{
  ALboolean status;
  char *buf;
  if (numBytesToSkip == 0)
    {
      return AL_TRUE;
    }
  buf = (char *) _alutMalloc (numBytesToSkip);
  if (buf == NULL)
    {
      return AL_FALSE;
    }
  status = streamRead (stream, buf, numBytesToSkip);
  free (buf);
  return status;
}

ALboolean
_alutInputStreamReadUInt16LE (InputStream *stream, UInt16LittleEndian *value)
{
  unsigned char buf[2];
  if (!streamRead (stream, buf, sizeof (buf)))
    {
      return AL_FALSE;
    }
  *value = ((UInt16LittleEndian) buf[1] << 8) | ((UInt16LittleEndian) buf[0]);
  return AL_TRUE;
}

ALboolean
_alutInputStreamReadInt32BE (InputStream *stream, Int32BigEndian *value)
{
  unsigned char buf[4];
  if (!streamRead (stream, buf, sizeof (buf)))
    {
      return AL_FALSE;
    }
  *value =
    ((Int32BigEndian) buf[0] << 24) |
    ((Int32BigEndian) buf[1] << 16) |
    ((Int32BigEndian) buf[2] << 8) | ((Int32BigEndian) buf[3]);
  return AL_TRUE;
}

ALboolean
_alutInputStreamReadUInt32LE (InputStream *stream, UInt32LittleEndian *value)
{
  unsigned char buf[4];
  if (!streamRead (stream, buf, sizeof (buf)))
    {
      return AL_FALSE;
    }
  *value =
    ((UInt32LittleEndian) buf[3] << 24) |
    ((UInt32LittleEndian) buf[2] << 16) |
    ((UInt32LittleEndian) buf[1] << 8) | ((UInt32LittleEndian) buf[0]);
  return AL_TRUE;
}