/***************************************************************************
 ** 
 **  File: writebin.c
 **
 **  Description: Routines for writing binary files.
 **
 **  Date: 17th July 2004
 **
 ***************************************************************************
 ***************************************************************************
 *
 *    Copyright (C) 2004 Scott A. Belmonte
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without 
 *    modification, are permitted provided that the following conditions
 *    are met:
 *
 *    Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 *    Neither the name of the copyright holder nor the names of any
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 * 
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 *    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#include <stdlib.h>
#include "writebin.h"

/****************************************************************************
 *
 * Function: write_bin_file
 *
 * Description: This function wraps the standard library function fwrite().
 *              It swaps the bytes of the raw data if the parameter swap
 *              is SWAP and size is either 2, 4 or 8. If size is not 2, 
 *              4 or 8 then the raw data are written without swapping.
 *
 * Input: data_p     - Pointer the buffer containing the data to be written.
 *        item_size  - The size in bytes of an individual item to be written.
 *        item_count - The number of items to write.
 *        stream_p   - File handle of an open file.
 *        swap       - If SWAP then swap the bytes in the buffer,
 *                     If DONT_SWAP then don't swap bytes.
 *
 * Output: size_t - The actual number of items (not bytes) written. May be
 *                  less than the number requested if an error occurred
 *                  while writing the file. 
 *
 ***************************************************************************/
size_t write_bin_file(const void *data_p,
                      size_t      item_size,
                      size_t      item_count,
                      FILE       *stream_p,
                      e_swap      swap)
{
    const unsigned char *orig_data_p;
    unsigned char *swapped_data_p;
    unsigned char *pointer;
    size_t         num_items_written;
    size_t         i;

    num_items_written = 0;

    if (stream_p != NULL && data_p != NULL)
    {
        orig_data_p = (const unsigned char *) data_p;

        /* Swap bytes if asked and if item_size is divisible by two. */ 
        if (swap == SWAP && (item_size % 2 == 0))
        {
            swapped_data_p = (unsigned char *) malloc(item_size*item_count);
            pointer        = swapped_data_p;

            if (swapped_data_p != NULL)
            {
                switch (item_size)
                {
                case 2:
                    for (i = 0; i < item_count; i++)          
                    {
                        *(pointer)     = *(orig_data_p + 1);
                        *(pointer + 1) = *(orig_data_p);
                        pointer     += item_size;
                        orig_data_p += item_size;
                    }
                    break;

                case 4:
                    for (i = 0; i < item_count; i++)          
                    {
                        *(pointer)     = *(orig_data_p + 3);
                        *(pointer + 1) = *(orig_data_p + 2);
                        *(pointer + 2) = *(orig_data_p + 1);
                        *(pointer + 3) = *(orig_data_p);
                        pointer     += item_size;
                        orig_data_p += item_size;
                    }
                    break;

                case 8:
                    for (i = 0; i < item_count; i++)          
                    {
                        *(pointer)     = *(orig_data_p + 7);
                        *(pointer + 1) = *(orig_data_p + 6);
                        *(pointer + 2) = *(orig_data_p + 5);
                        *(pointer + 3) = *(orig_data_p + 4);
                        *(pointer + 4) = *(orig_data_p + 3);
                        *(pointer + 5) = *(orig_data_p + 2);
                        *(pointer + 6) = *(orig_data_p + 1);
                        *(pointer + 7) = *(orig_data_p);
                        pointer     += item_size;
                        orig_data_p += item_size;
                    }
                    break;

                default:
                    /* Issue warning message and then write data without swapping */
                    fprintf(stderr, "write_bin_file: Cannot swap data. "); 
                    fprintf(stderr, "Unsupported data size: %d\n", item_size);
                    for (i = 0; i < item_count*item_size; i++)
                    {
                        *pointer++ = *orig_data_p++;
                    }
                    break;
                }

                /* Write the swapped items */
                num_items_written = fwrite(swapped_data_p, item_size, item_count, stream_p);
                free(swapped_data_p);
            }
        }
        else
        {
            /* Write the data directly */
            num_items_written = fwrite(data_p, item_size, item_count, stream_p);
        }
    }

    return num_items_written;
}

