/*
 * $Id: zc.c,v 1.1 2001/01/29 15:10:04 york Exp $
 *
 * This program is used to build Palm OS database containing Bible text.
 * It compresses particular chapters using zlib compression library.
 *
 * To successfully build this program install zlib-devel-*.rpm package.
 */

#include <stdio.h>
#include <zlib.h>

#define BUF_SIZE (1024*64)

int main(int argc, char **argv)
{
    char src[BUF_SIZE], dst[BUF_SIZE];
    unsigned long src_len, dst_len = sizeof(dst);
    FILE *fd;
    int res;

    // check input params
    if(argc != 3)
    {
        printf("%s source destination\n", argv[0]);
        return 0;
    }

    // read file into the memory
    if((fd = fopen(argv[1], "rb")) != NULL)
    {
        src_len = fread(src, 1, sizeof(src), fd);
        fclose(fd);
    }
    else
    {
        printf("Can't open file '%s' for reading\n", argv[1]);
        return -1;
    }

    // compress the file
    res = compress(dst, &dst_len, src, src_len);
    if(res != Z_OK)
    {
        printf("Error in compression\n");
        return -1;
    }

    // write compressed file
    if((fd = fopen(argv[2], "wb")) != NULL)
    {
        fputc(0xFF & (src_len >> 8), fd);
        fputc(0xFF & (src_len     ), fd);
        
        fwrite(dst, 1, dst_len, fd);
        fclose(fd);
    }
    else
    {
        printf("Can't write file '%s'\n", argv[2]);
        return -1;
    }

    return 0;
}

