From 2340ff67b22b05cea7a6da42947c8fccede7d0ea Mon Sep 17 00:00:00 2001 From: EvanR Date: Sat, 27 Feb 2010 15:47:52 -0600 Subject: [PATCH] loader.c binary reading routines made sane. Previously they have no hope of reporting errors. I must have written this before knowing the significance of i/o errors. Luckily nothing of value utilized the broken interface. I need this working interface to complete the midi file reader. --- loader.c | 64 +++++++++++++++++++++++++++++++++++++++++----------------------- loader.h | 8 ++++---- sfx.c | 57 +++++++++++++++++---------------------------------------- 3 files changed, 62 insertions(+), 67 deletions(-) rewrite sfx.c (63%) diff --git a/loader.c b/loader.c index 371f46c..95af72b 100644 --- a/loader.c +++ b/loader.c @@ -173,35 +173,53 @@ int loader_scanline(reader* rd, char* format, ...){ /*binary i/o*/ -int read_byte(reader* rd){ - unsigned char c = 0; - loader_read(rd, &c, 1); - return c; +int read_byte(reader* rd, int* out){ + unsigned char c; + if(loader_read(rd, &c, 1) < 0){ + return -1; + } + else{ + *out = c; + return 0; + } } -int read_short(reader* rd){ - unsigned char c[2] = {0,0}; - loader_read(rd, c+0, 1); - loader_read(rd, c+1, 1); - return (c[0]<<8) | c[1]; +int read_short(reader* rd, int* out){ + unsigned char c[2]; + if(loader_read(rd, c, 2) < 0){ + return -1; + } + else{ + *out = (c[0]<<8) | c[1]; + return 0; + } } -int read_int(reader* rd){ - unsigned char c[4] = {0,0,0,0}; - loader_read(rd, c+0, 1); - loader_read(rd, c+1, 1); - loader_read(rd, c+2, 1); - loader_read(rd, c+3, 1); - return (c[0]<<24) | (c[1]<<16) | (c[2]<<8) | c[3]; +int read_int(reader* rd, int* out){ + unsigned char c[4]; + if(loader_read(rd, c, 4) < 0){ + return -1; + } + else{ + *out = (c[0]<<24) | (c[1]<<16) | (c[2]<<8) | c[3]; + return 0; + } } -char* read_string(reader* rd){ - unsigned int L = read_int(rd); - if(L==0) return NULL; - char* S = xmalloc(L+1); - S[L] = '\0'; - loader_read(rd, S, L); - return S; +int read_string(reader* rd, char** out){ + unsigned L; + if(read_int(rd, (int*)&L) < 0){ + return -1; + } + + *out = xmalloc(L+1); + *out[L] = '\0'; + if(loader_read(rd, *out, L) < 0){ + free(*out); + return -1; + } + + return 0; } diff --git a/loader.h b/loader.h index 3639ebf..9475a48 100644 --- a/loader.h +++ b/loader.h @@ -38,7 +38,7 @@ list* loader_readdir(char* path); void loader_freedirlist(list* dirs); /*binary i/o*/ -int read_byte(reader* rd); -int read_short(reader* rd); -int read_int(reader* rd); -char* read_string(reader* rd); +int read_byte(reader* rd, int* out); +int read_short(reader* rd, int* out); +int read_int(reader* rd, int* out); +int read_string(reader* rd, char** out); diff --git a/sfx.c b/sfx.c dissimilarity index 63% index c410b00..92ce6e8 100644 --- a/sfx.c +++ b/sfx.c @@ -1,40 +1,17 @@ -#include - -#include -#include -#include - -float* sfx[SFX_COUNT]; -int sfx_len[SFX_COUNT]; - - -int load_sound(char* filename, sfx_id id){ - /* load a wave file */ - reader* r = data_open("sounds", filename); - if(r==NULL){ - return -1; - } - - int i; - for(i=0; i<40; i++){ - read_byte(r); - } - - int N = read_int(r) / 2; - float* buffer = malloc(N); - - for(i=0; i + +#include +#include +#include + +float* sfx[SFX_COUNT]; +int sfx_len[SFX_COUNT]; + + +int load_sound(char* filename, sfx_id id){ + return -1; +} + +void play_sound(sfx_id id){ + +} -- 2.11.4.GIT