/*************************************************************************** * Function : MakeBitFile * Description: This function naively wraps a standard file in a * bit_file_t structure. ANSI-C doesn't support file status * functions commonly found in other C variants, so the * caller must be passed as a parameter. * Parameters : stream - pointer to the standard file being wrapped. * mode - The mode of the file being wrapped. * Effects : A bit_file_t structure will be created for the stream * passed as a parameter. * Returned : Pointer to the bit_file_t structure for the bit file * or NULL on failure. errno will be set for all failure * cases. ***************************************************************************/ bit_file_t *MakeBitFile(FILE *stream, const BF_MODES mode) { bit_file_t *bf; if (stream == NULL) { /* can't wrapper empty steam */ errno = EBADF; bf = NULL; } else { bf = (bit_file_t *)malloc(sizeof(bit_file_t)); if (bf == NULL) { /* malloc failed */ errno = ENOMEM; } else { /* set structure data */ bf->fp = stream; bf->bitBuffer = 0; bf->bitCount = 0; bf->mode = mode; bf->endian = DetermineEndianess(); } } return (bf); }
bit_file_t *BitFileOpen(const char *fileName, const BF_MODES mode) { char modes[3][3] = {"rb", "wb", "ab"}; /* binary modes for fopen */ bit_file_t *bf; bf = (bit_file_t *)malloc(sizeof(bit_file_t)); if (bf == NULL) { /* malloc failed */ errno = ENOMEM; } else { bf->fp = fopen(fileName, modes[mode]); if (bf->fp == NULL) { /* fopen failed */ free(bf); bf = NULL; } else { /* fopen succeeded fill in remaining bf data */ bf->bitBuffer = 0; bf->bitCount = 0; bf->mode = mode; bf->endian = DetermineEndianess(); } } return (bf); }
/*************************************************************************** * Function : BitFileOpen * Description: This function opens a bit file for reading, writing, * or appending. If successful, a bit_file_t data * structure will be allocated and a pointer to the * structure will be returned. * Parameters : fileName - NULL terminated string containing the name of * the file to be opened. * mode - The mode of the file to be opened * Effects : The specified file will be opened and file structure will * be allocated. * Returned : Pointer to the bit_file_t structure for the bit file * opened, or NULL on failure. errno will be set for all * failure cases. ***************************************************************************/ bit_file_t *BitFileOpen(const char *fileName, const BF_MODES mode) { char modes[3][3] = {"rb", "wb", "ab"}; /* binary modes for fopen */ bit_file_t *bf; bf = (bit_file_t *)malloc(sizeof(bit_file_t)); if (bf == NULL) { /* malloc failed */ errno = ENOMEM; } else { bf->fp = fopen(fileName, modes[mode]); if (bf->fp == NULL) { /* fopen failed */ free(bf); bf = NULL; } else { /* fopen succeeded fill in remaining bf data */ bf->bitBuffer = 0; bf->bitCount = 0; bf->mode = mode; bf->endian = DetermineEndianess(); /*************************************************************** * TO DO: Consider using the last byte in a file to indicate * the number of bits in the previous byte that actually have * data. If I do that, I'll need special handling of files * opened with a mode of BF_APPEND. ***************************************************************/ } } return (bf); }