Mercurial > hg > dmlib
view src/dmbstr.c @ 1089:27d041472c8f
Cleanup, rename some variables, etc.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Mar 2015 20:25:44 +0200 |
parents | 1e5cf1144f36 |
children | 988a3397839e |
line wrap: on
line source
/* * DMLib * -- Simple bitstream I/O functions * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2012 Tecnic Software productions (TNSP) */ #include "dmbstr.h" static BOOL dmPutByteFILE(DMBitStreamContext *ctx, const Uint8 val) { return fputc(val, (FILE *) ctx->fp) == val; } void dmInitBitStreamContext(DMBitStreamContext *ctx) { ctx->outBuf = 0; ctx->outByteCount = 0; ctx->outBitCount = 8; } int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp) { if (ctx == NULL || fp == NULL) return DMERR_NULLPTR; ctx->putByte = dmPutByteFILE; ctx->fp = (void *) fp; dmInitBitStreamContext(ctx); return DMERR_OK; } BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n) { int i; unsigned int mask = 1 << (n - 1); for (i = 0; i < n; i++) { ctx->outBuf <<= 1; if (val & mask) ctx->outBuf |= 1; mask >>= 1; ctx->outBitCount--; if (ctx->outBitCount == 0) { if (!ctx->putByte(ctx, ctx->outBuf & 0xff)) return FALSE; ctx->outBitCount = 8; ctx->outByteCount++; } } return TRUE; } int dmFlushBitStream(DMBitStreamContext *ctx) { if (ctx == NULL) return DMERR_NULLPTR; if (ctx->outBitCount != 8) dmPutBits(ctx, 0, ctx->outBitCount); return 0; }