(root)/
glib-2.79.0/
girepository/
cmph/
cmph_structs.c
       1  #include "cmph_structs.h"
       2  
       3  #include <string.h>
       4  #include <errno.h>
       5  
       6  //#define DEBUG
       7  #include "debug.h"
       8  
       9  cmph_config_t *__config_new(cmph_io_adapter_t *key_source)
      10  {
      11  	cmph_config_t *mph = (cmph_config_t *)malloc(sizeof(cmph_config_t));
      12  	memset(mph, 0, sizeof(cmph_config_t));
      13  	if (mph == NULL) return NULL;
      14  	mph->key_source = key_source;
      15  	mph->verbosity = 0;
      16  	mph->data = NULL;
      17  	mph->c = 0;
      18  	return mph;
      19  }
      20  
      21  void __config_destroy(cmph_config_t *mph)
      22  {
      23  	free(mph);
      24  }
      25  
      26  void __cmph_dump(cmph_t *mphf, FILE *fd)
      27  {
      28  	register size_t nbytes;
      29  	nbytes = fwrite(cmph_names[mphf->algo], (size_t)(strlen(cmph_names[mphf->algo]) + 1), (size_t)1, fd);
      30  	nbytes = fwrite(&(mphf->size), sizeof(mphf->size), (size_t)1, fd);
      31          if (nbytes == 0 && ferror(fd)) {
      32            fprintf(stderr, "ERROR: %s\n", strerror(errno));
      33          }
      34  }
      35  cmph_t *__cmph_load(FILE *f) 
      36  {
      37  	cmph_t *mphf = NULL;
      38  	cmph_uint32 i;
      39  	char algo_name[BUFSIZ];
      40  	char *ptr = algo_name;
      41  	CMPH_ALGO algo = CMPH_COUNT;
      42  	register size_t nbytes;
      43  	
      44  	DEBUGP("Loading mphf\n");
      45  	while(1)
      46  	{
      47  		size_t c = fread(ptr, (size_t)1, (size_t)1, f);
      48  		if (c != 1) return NULL;
      49  		if (*ptr == 0) break;
      50  		++ptr;
      51  	}
      52  	for(i = 0; i < CMPH_COUNT; ++i)
      53  	{
      54  		if (strcmp(algo_name, cmph_names[i]) == 0)
      55  		{
      56  			algo = i;
      57  		}
      58  	}
      59  	if (algo == CMPH_COUNT) 
      60  	{
      61  		DEBUGP("Algorithm %s not found\n", algo_name);
      62  		return NULL;
      63  	}
      64  	mphf = (cmph_t *)malloc(sizeof(cmph_t));
      65  	mphf->algo = algo;
      66  	nbytes = fread(&(mphf->size), sizeof(mphf->size), (size_t)1, f);
      67  	mphf->data = NULL;
      68  	DEBUGP("Algorithm is %s and mphf is sized %u\n", cmph_names[algo],  mphf->size);
      69          if (nbytes == 0 && ferror(f)) {
      70            fprintf(stderr, "ERROR: %s\n", strerror(errno));
      71          }
      72  
      73  	return mphf;
      74  }
      75  
      76