1
Fork 0
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-24 22:25:58 +02:00
cmph_config_t *__config_new(cmph_io_adapter_t *key_source)
2004-12-23 15:16:30 +02:00
{
cmph_config_t *mph = (cmph_config_t *)malloc(sizeof(cmph_config_t));
2004-12-23 15:16:30 +02:00
if (mph == NULL) return NULL;
memset(mph, 0, sizeof(cmph_config_t));
2004-12-23 15:16:30 +02:00
mph->key_source = key_source;
mph->verbosity = 0;
mph->data = NULL;
mph->c = 0;
2004-12-23 15:16:30 +02:00
return mph;
}
void __config_destroy(cmph_config_t *mph)
2004-12-23 15:16:30 +02:00
{
free(mph);
}
void __cmph_dump(cmph_t *mphf, FILE *fd)
2004-12-23 15:16:30 +02:00
{
register size_t nbytes;
2009-03-15 01:24:05 +02:00
nbytes = fwrite(cmph_names[mphf->algo], (size_t)(strlen(cmph_names[mphf->algo]) + 1), (size_t)1, fd);
nbytes = fwrite(&(mphf->size), sizeof(mphf->size), (size_t)1, fd);
2004-12-23 15:16:30 +02:00
}
cmph_t *__cmph_load(FILE *f)
2004-12-23 15:16:30 +02:00
{
cmph_t *mphf = NULL;
2005-01-18 23:06:08 +02:00
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;
register size_t nbytes;
2009-03-15 01:24:05 +02:00
2004-12-23 15:16:30 +02:00
DEBUGP("Loading mphf\n");
while(1)
{
size_t c = fread(ptr, (size_t)1, (size_t)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;
}
mphf = (cmph_t *)malloc(sizeof(cmph_t));
2004-12-23 15:16:30 +02:00
mphf->algo = algo;
2009-03-15 01:24:05 +02:00
nbytes = fread(&(mphf->size), sizeof(mphf->size), (size_t)1, f);
2004-12-23 15:16:30 +02:00
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;
}