/* * バイト配列をソースとする入力ストリーム */ #include "myio_byte_array.h" /* * size バイト分のデータを読み出す関数。 * 返り値は buf に読み出したバイト数。 * エラー時には -1 が返る。 */ int myio_read_ba ( MYIO *in , char *buf , int size ) { int nbytes = 0; myio_ba_ctx *ctx = (myio_ba_ctx*)(in->ptr); if (!ctx || ! ctx->ba) { return -1; } if (ctx->pos >= ctx->size) { return 0; } if (ctx->pos + size > ctx->size) { nbytes = ctx->size - ctx->pos; } else { nbytes = size; } { int i; for (i=0; iba[ctx->pos + i]; } } ctx->pos += nbytes; return nbytes; } MYIO_METHOD myio_ba_method = { MYIO_METHOD_NAME_BA , (void*)0 , myio_read_ba , (void*)0 , (void*)0 }; MYIO *MYIO_ba_new ( char *ba /* バイト配列 */ , int size /* バイト配列のサイズ */ , void *alloc_func /* メモリアロケーションの関数 */ , void *alloc_func_ctx /* メモリアロケーションの関数のコンテクストデータ */ ) { myio_ba_ctx *ctx = (void*)0; MYIO *myio = (void*)0; void * (*alloc_f)(void*,int) = alloc_func; if (!ba || size<=0) { return (void*)0; } if (alloc_f == (void*)0) { return (void*)0; } ctx = (myio_ba_ctx *)alloc_f(alloc_func_ctx, sizeof(myio_ba_ctx)); if (!ctx) { return (void*)0; } ctx->ba = ba; ctx->size = size; ctx->pos = 0; myio = (MYIO*)alloc_f(alloc_func_ctx, sizeof(MYIO)); if (!myio) { return (void*)0; } return MYIO_make(myio, &myio_ba_method, 1, 0, ctx); }