123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "Stream.h"
- #include <stdlib.h>
- void streamInit(Stream* s)
- {
- s->index = 0;
- s->size = BUFFER_SIZE;
- }
- int streamGetChar(Stream* in, char* c)
- {
- if(in->index < in->size)
- {
- *c = in->data[in->index];
- in->index++;
- return 0;
- }
- return -1;
- }
- int streamGetChars(Stream* in, char* buffer, int length)
- {
- int index = 0;
- length--;
- while(index < length)
- {
- char c;
- if(streamGetChar(in, &c) == -1)
- {
- buffer[index] = '\0';
- return index;
- }
- else
- {
- buffer[index] = c;
- }
- index++;
- }
- buffer[index] = '\0';
- return index;
- }
- int streamGetShort(Stream* in, short* s)
- {
- if(in->index + 1 < in->size)
- {
- *s = (in->data[in->index] & 0xFF) | ((in->data[in->index + 1] & 0xFF) << 8);
- in->index += 2;
- return 0;
- }
- return -1;
- }
- int streamGetInt(Stream* in, int* i)
- {
- if(in->index + 3 < in->size)
- {
- *i = (in->data[in->index] & 0xFF) | ((in->data[in->index + 1] & 0xFF) << 8) |
- ((in->data[in->index + 2] & 0xFF) << 16) | ((in->data[in->index + 3] & 0xFF) << 24);
- in->index += 4;
- return 0;
- }
- return -1;
- }
- int streamWriteChar(Stream* out, char c)
- {
- if(out->index < out->size)
- {
- out->data[out->index] = c;
- out->index++;
- return 0;
- }
- return -1;
- }
- int streamWriteChars(Stream* out, char* c)
- {
- int i = 0;
- while(c[i] != '\0')
- {
- if(streamWriteChar(out, c[i]) == -1)
- {
- return -1;
- }
- i++;
- }
- return 0;
- }
- int streamWriteShort(Stream* out, short s)
- {
- if(out->index + 1 < out->size)
- {
- out->data[out->index] = s & 0xFF;
- out->data[out->index + 1] = (s >> 8) & 0xFF;
- out->index += 2;
- return 0;
- }
- return -1;
- }
- int streamWriteInt(Stream* out, int i)
- {
- if(out->index + 3 < out->size)
- {
- out->data[out->index] = i & 0xFF;
- out->data[out->index + 1] = (i >> 8) & 0xFF;
- out->data[out->index + 2] = (i >> 16) & 0xFF;
- out->data[out->index + 3] = (i >> 24) & 0xFF;
- out->index += 4;
- return 0;
- }
- return -1;
- }
|