/*compression.h*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
typedef struct letterCount {
char character;
int count;
void *left;
void *right;
}lCount;
int cmp (const void* a, const void* b);
/*compression.c*/
#include "compression.h"
lCount** makeArray(FILE *fi)
{
int i;
char* uniqueList;
lCount** nodeArray;
char a = '0';
const size_t lCountSize = sizeof(struct letterCount);
Buffer *longBuff = b_create(100,15,'m');
while ((a=fgetc(fi))!=EOF)
{
ca_addc(longBuff,a);
}
uniqueList = (char*) malloc (sizeof(char)*strlen(ca_tostring(longBuff))+1);
strcpy(uniqueList,ca_getunique(longBuff));
printf ("Unique list: %s|\n\nUnique buffer:%s\n",uniqueList,ca_tostring(longBuff));
int uchars = strlen(uniqueList);
nodeArray = (lCount**) malloc (lCountSize*(uchars));/*creates array of nodes for tree*/
for (i=0;i<uchars;i++)/*makes the array of nodes*/
{
lCount* nodePoint = (lCount*) malloc (lCountSize);
nodeArray[i] = nodePoint;
/*this should all be its own function called makeNode*/
nodeArray[i]->character = uniqueList[i];
nodeArray[i]->count = ca_count(uniqueList[i],longBuff);
}
qsort(nodeArray,(sizeof(nodeArray)/lCountSize),lCountSize,cmp);
return nodeArray;
}
int cmp (const void* a,const void* b)
{
struct letterCount *ia = (struct letterCount *)a;
struct letterCount *ib = (struct letterCount *)b;
double comp;//double to avoid integer over/underflow
comp = ia->count-ib->count;
if (comp == 0) {return 0;}
if (comp > 0) {return 1;}
if (comp < 0) {return -1;}
}