Mercurial > hg > dmlib
diff minijss/jloadjss.c @ 777:ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 24 May 2013 01:19:11 +0300 |
parents | c430112449a7 |
children | 97ecc0a9c21f |
line wrap: on
line diff
--- a/minijss/jloadjss.c Thu May 23 23:48:24 2013 +0300 +++ b/minijss/jloadjss.c Fri May 24 01:19:11 2013 +0300 @@ -16,40 +16,12 @@ #endif -static BOOL jsGetBufData(Uint8 **buf, size_t *bufLeft, void *data, const size_t dataSize) -{ - if (*bufLeft >= dataSize) - { - memcpy(data, *buf, dataSize); - *buf += dataSize; - *bufLeft -= dataSize; - return TRUE; - } - else - return FALSE; -} - - -static BOOL jsGetBufByte(Uint8 **buf, size_t *bufLeft, Uint8 *data) -{ - if (*bufLeft > 0) - { - *data = **buf; - (*buf)++; - (*bufLeft)--; - return TRUE; - } - else - return FALSE; -} - - -#define JSGETBUF(XV, XT) if (!jsGetBufData(buf, bufLeft, XV, sizeof(XT))) return DMERR_OUT_OF_DATA -#define JSGETBYTE(XV) if (!jsGetBufByte(buf, bufLeft, XV)) return DMERR_OUT_OF_DATA +#define JSGETBUF(XV, XT) if (!dmf_read_str(inFile, XV, sizeof(XT))) return DMERR_OUT_OF_DATA +#define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA #if defined(JM_SUP_PATMODE_1) || defined(JM_SUP_PATMODE_3) -static int jssGetConvertedNote(Uint8 **buf, size_t *bufLeft, JSSNote *note) +static int jssGetConvertedNote(DMResource *inFile, JSSNote *note) { Uint8 tmp; @@ -80,7 +52,7 @@ #if defined(JM_SUP_PATMODE_2) || defined(JM_SUP_PATMODE_4) -static int jssGetCompressedNote(Uint8 **buf, size_t *bufLeft, JSSNote *note) +static int jssGetCompressedNote(DMResource *inFile, JSSNote *note) { Uint8 packb, tmp; @@ -151,7 +123,7 @@ #ifdef JM_SUP_PATMODE_2 -static int jssGetPatternCompHoriz(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternCompHoriz(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -162,7 +134,7 @@ for (channel = 0; channel < pattern->nchannels; channel++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetCompressedNote(&buf, bufLeft, note); + int res = jssGetCompressedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", row, channel); } @@ -173,7 +145,7 @@ #ifdef JM_SUP_PATMODE_4 -static int jssGetPatternCompVert(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternCompVert(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -184,7 +156,7 @@ for (row = 0; row < pattern->nrows; row++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetCompressedNote(&buf, bufLeft, note); + int res = jssGetCompressedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", row, channel); } @@ -195,7 +167,7 @@ #ifdef JM_SUP_PATMODE_1 -static int jssGetPatternRawHoriz(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternRawHoriz(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -206,7 +178,7 @@ for (channel = 0; channel < pattern->nchannels; channel++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetConvertedNote(&buf, bufLeft, note); + int res = jssGetConvertedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", row, channel); } @@ -217,7 +189,7 @@ #ifdef JM_SUP_PATMODE_3 -static int jssGetPatternRawVert(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternRawVert(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -228,7 +200,7 @@ for (row = 0; row < pattern->nrows; row++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetConvertedNote(&buf, bufLeft, note); + int res = jssGetConvertedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", row, channel); } @@ -241,7 +213,7 @@ #ifdef JM_SUP_PATMODE_5 #undef JSGETBYTE -#define JSGETBYTE(XV) if (!jsGetBufByte(&buf, bufLeft, XV)) return DMERR_OUT_OF_DATA +#define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA #define JSFOREACHNOTE1 \ for (channel = 0; channel < pattern->nchannels; channel++) \ @@ -250,7 +222,7 @@ #define JSFOREACHNOTE2 } -static int jssGetPatternRawVertElem(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternRawVertElem(DMResource *inFile, JSSPattern *pattern) { int row, channel; Uint8 tmp; @@ -296,11 +268,11 @@ #undef JSGETBUF #undef JSGETBYTE #define JSGETBUF(XV, XT) do { \ - if (!jsGetBufData(&buf, &bufLeft, XV, sizeof(XT))) \ + if (!dmf_read_str(inFile, XV, sizeof(XT))) \ JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, \ "Out of data at getting " # XT " (%d bytes)\n", sizeof(XT)); \ } while (0) -#define JSGETBYTE(XV) if (!jsGetBufByte(&buf, &bufLeft, XV)) return DMERR_OUT_OF_DATA +#define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA #ifdef JM_SUP_EXT_INSTR @@ -323,16 +295,12 @@ #endif -int jssLoadJSSMOD(Uint8 *bufStart, const size_t bufSize, JSSModule **ppModule) +int jssLoadJSSMOD(DMResource *inFile, JSSModule **ppModule) { JSSModule *module; JSSMODHeader jssH; - Uint8 *buf = bufStart; - size_t bufLeft = bufSize; int index; - assert(ppModule != NULL); - assert(bufStart != NULL); *ppModule = NULL; // Check the JSSMOD header @@ -386,17 +354,10 @@ { JSSMODPattern jssP; int result = DMERR_INVALID_DATA; - size_t bufSize; // Get header and check size memset(&jssP, 0, sizeof(jssP)); JSGETBUF(&jssP, JSSMODPattern); - bufSize = jssP.size; - if (bufLeft < jssP.size) - { - JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, - "Out of data for pattern #%d.\n", index); - } // Allocate pattern module->patterns[index] = jssAllocatePattern(jssP.nrows, module->nchannels); @@ -411,27 +372,27 @@ { #ifdef JM_SUP_PATMODE_1 case PATMODE_RAW_HORIZ: - result = jssGetPatternRawHoriz(buf, &bufSize, module->patterns[index]); + result = jssGetPatternRawHoriz(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_2 case PATMODE_COMP_HORIZ: - result = jssGetPatternCompHoriz(buf, &bufSize, module->patterns[index]); + result = jssGetPatternCompHoriz(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_3 case PATMODE_RAW_VERT: - result = jssGetPatternRawVert(buf, &bufSize, module->patterns[index]); + result = jssGetPatternRawVert(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_4 case PATMODE_COMP_VERT: - result = jssGetPatternCompVert(buf, &bufSize, module->patterns[index]); + result = jssGetPatternCompVert(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_5 case PATMODE_RAW_ELEM: - result = jssGetPatternRawVertElem(buf, &bufSize, module->patterns[index]); + result = jssGetPatternRawVertElem(inFile, module->patterns[index]); break; #endif default: @@ -440,19 +401,10 @@ break; } - if (bufSize > 0) - { - JSSWARNING(DMERR_EXTRA_DATA, DMERR_EXTRA_DATA, - "Unparsed data after pattern (%d bytes), possibly broken file.\n", bufSize); - } - if (result != DMERR_OK) { JSSERROR(result, result, "Error in unpacking pattern #%i data\n", index); } - - buf += jssP.size; - bufLeft -= jssP.size; } #ifdef JM_SUP_EXT_INSTR @@ -538,14 +490,6 @@ else sz = inst->size * sizeof(Uint8); - // Check if we can get as much? - if (bufLeft < sz) - { - JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, - "Out of data for instrument sample #%d (%d < %d)\n", - index, bufLeft, sz); - } - // Allocate if ((inst->data = dmMalloc(sz)) == NULL) { @@ -554,9 +498,11 @@ } // Copy data - memcpy(inst->data, buf, sz); - buf += sz; - bufLeft -= sz; + if (!dmf_read_str(inFile, inst->data, sz)) + { + JSSERROR(DMERR_FREAD, DMERR_FREAD, + "Could not read sample data for #%d\n", index); + } // Convert, if needed if (inst->flags & jsf16bit)