123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #include "Stream.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void streamInit(Stream* s, int size)
- {
- s->index = 4;
- s->size = size;
- if(size > 0)
- {
- s->data = malloc(sizeof(char) * size);
- }
- else
- {
- s->data = NULL;
- }
- }
- void streamRemove(Stream* s)
- {
- s->index = 0;
- s->size = 0;
- free(s->data);
- s->data = NULL;
- }
- void streamUpdateIndex(Stream* s)
- {
- if(s->size >= 4)
- {
- s->data[0] = s->index & 0xFF;
- s->data[1] = (s->index >> 8) & 0xFF;
- s->data[2] = (s->index >> 16) & 0xFF;
- s->data[3] = (s->index >> 24) & 0xFF;
- }
- }
- int streamGetIndex(Stream* s)
- {
- if(s->size >= 4)
- {
- return (s->data[0] & 0xFF) | ((s->data[1] & 0xFF) << 8) | ((s->data[2] & 0xFF) << 16) | ((s->data[3] & 0xFF) << 24);
- }
- return -1;
- }
- int streamGetPosition(Stream* s)
- {
- return s->index;
- }
- void streamSetPosition(Stream* s, int pos)
- {
- if(pos >= 4 && pos < s->size)
- {
- s->index = pos;
- }
- }
- void streamEnsureCapacity(Stream* s, int length)
- {
- if(length >= s->size)
- {
- int newSize = s->size;
- if(newSize >= 0)
- {
- newSize = 1;
- }
- while(length >= newSize)
- {
- newSize *= 2;
- }
- char* newData = malloc(sizeof(char) * newSize);
- memcpy(newData, s->data, s->size);
- free(s->data);
- s->data = newData;
- //printf("RESIZE FROM %d to %d\n", s->size, newSize);
- s->size = newSize;
- }
- }
- 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)
- {
- streamEnsureCapacity(out, out->index);
- 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)
- {
- streamEnsureCapacity(out, out->index + 1);
- 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)
- {
- streamEnsureCapacity(out, out->index + 3);
- 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;
- }
|