turbonss/src/cmph_structs.c

70 lines
1.4 KiB
C
Raw Normal View History

2004-12-23 15:16:30 +02:00
#include "cmph_structs.h"
#include <string.h>
//#define DEBUG
2004-12-23 15:16:30 +02:00
#include "debug.h"
2005-01-18 23:06:08 +02:00
cmph_mph_t *cmph__mph_new(CMPH_ALGO algo, cmph_key_source_t *key_source)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_mph_t *mph = (cmph_mph_t *)malloc(sizeof(cmph_mph_t));
DEBUGP("Creating mph with algorithm %s\n", cmph_names[algo]);
2004-12-23 15:16:30 +02:00
if (mph == NULL) return NULL;
mph->algo = algo;
mph->key_source = key_source;
mph->verbosity = 0;
float c = 0;
2004-12-23 15:16:30 +02:00
return mph;
}
2005-01-18 23:06:08 +02:00
void cmph__mph_destroy(cmph_mph_t *mph)
2004-12-23 15:16:30 +02:00
{
free(mph);
}
2005-01-18 23:06:08 +02:00
void cmph__mphf_dump(cmph_mphf_t *mphf, FILE *fd)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 nsize = htonl(mphf->size);
fwrite(cmph_names[mphf->algo], (cmph_uint32)(strlen(cmph_names[mphf->algo]) + 1), 1, fd);
2004-12-23 15:16:30 +02:00
fwrite(&nsize, sizeof(mphf->size), 1, fd);
}
2005-01-18 23:06:08 +02:00
cmph_mphf_t *cmph__mphf_load(FILE *f)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_mphf_t *mphf = NULL;
cmph_uint32 i;
2004-12-23 15:16:30 +02:00
char algo_name[BUFSIZ];
char *ptr = algo_name;
2005-01-18 23:06:08 +02:00
CMPH_ALGO algo = CMPH_COUNT;
2004-12-23 15:16:30 +02:00
DEBUGP("Loading mphf\n");
while(1)
{
2005-01-18 23:06:08 +02:00
cmph_uint32 c = fread(ptr, 1, 1, f);
2004-12-23 15:16:30 +02:00
if (c != 1) return NULL;
if (*ptr == 0) break;
++ptr;
}
2005-01-18 23:06:08 +02:00
for(i = 0; i < CMPH_COUNT; ++i)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
if (strcmp(algo_name, cmph_names[i]) == 0)
2004-12-23 15:16:30 +02:00
{
algo = i;
}
}
2005-01-18 23:06:08 +02:00
if (algo == CMPH_COUNT)
2004-12-23 15:16:30 +02:00
{
DEBUGP("Algorithm %s not found\n", algo_name);
return NULL;
}
2005-01-18 23:06:08 +02:00
mphf = (cmph_mphf_t *)malloc(sizeof(cmph_mphf_t));
2004-12-23 15:16:30 +02:00
mphf->algo = algo;
fread(&(mphf->size), sizeof(mphf->size), 1, f);
mphf->size = ntohl(mphf->size);
mphf->data = NULL;
2005-01-18 23:06:08 +02:00
DEBUGP("Algorithm is %s and mphf is sized %u\n", cmph_names[algo], mphf->size);
2004-12-23 15:16:30 +02:00
return mphf;
}