Back to index

im-sdk  12.3.91
Functions
hhentry.c File Reference
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utfchar.h"
#include "hhentry.h"

Go to the source code of this file.

Functions

void print_utfchar_hex_value (unsigned char *str, FILE *fp)
void put_int24_to_buffer (int i, char *buffer)
void put_int24_to_file (int i, FILE *fp)
void get_int24_from_buffer (int *val, char *buffer)
void get_int24_from_file (int *val, FILE *fp)
void hh_free (HH *hh)
HHhh_new ()
HHhh_new_with_data (unsigned char *utf_hangul, unsigned char *utf_hanja)
HHEntry hhitem_new ()
HHEntry hhitem_new_with_data (HH *hh)
void hhitem_init (HHEntry hhentry)
int hhitem_comp (HHEntry a, HHEntry b)
HHEntry hhitem_add_hanja (HHEntry hhentry, unsigned char *utf_hanja)
void hhentry_copy (HHEntry dst, HHEntry src)
int hhitem_serialize (HHEntry hhitem, int buffer_size, unsigned char *buffer_return)
void hhlist_dump_content (HHList *hhlist, FILE *fp)
void hash_table_read_content (FILE *fp, HHList **table, int *size)
void hash_table_dump_content (HHList table[], int size, FILE *fp)
void hhlist_read_from_file (FILE *fp, HHList *hhlist)
void hhitem_read_from_file (FILE *fp, HHEntry entry_return)
HHEntry hhlist_search_hhitem (HHList *hhlist, HH *hh)
void hhlist_add_hhitem (HHList *hhlist, HHEntry hhitem)
HHEntry hhlist_add_hh (HHList *hhlist, HH *item)
void hhlist_init (HHList *hhlist)
void hhlist_print_content (HHList *hhlist, FILE *fp)
void hhitem_print_string (HHItem *hhitem, FILE *fp)
void hhlist_print_string (HHList *hhlist, FILE *fp)
int hash (UTFCHAR *string)
void get_int8_from_file (int *val, FILE *fp)

Function Documentation

void get_int24_from_buffer ( int *  val,
char *  buffer 
)

Definition at line 612 of file hhentry.c.

{
  int i = 0;
  char *ptr = buffer;
  if ((val == NULL) || (buffer == NULL)){
    fprintf (stderr, "get_int24_from_buffer error: val or buffer is null\n");
    return;
  }
  i = *ptr++;
  i = (i << 8) | *ptr++;
  i = ( i << 8) | *ptr;

  *val = i;
  return;
}
void get_int24_from_file ( int *  val,
FILE *  fp 
)

Definition at line 630 of file hhentry.c.

{
  int i = 0;
  if ((val == NULL) || (fp == NULL)){
    fprintf (stderr, "get_int24_from_buffer error: val or fp is null\n");
    return;
  }
  i = fgetc (fp);
  i = (i << 8) | fgetc (fp);
  i = ( i << 8) | fgetc (fp);

  *val = i;
  return;
}

Here is the caller graph for this function:

void get_int8_from_file ( int *  val,
FILE *  fp 
)

Definition at line 646 of file hhentry.c.

{
  int i = 0;
  if ((val == NULL) || (fp == NULL)){
    fprintf (stderr, "get_int24_from_buffer error: val or fp is null\n");
    return;
  }
  i = fgetc (fp);
  *val = i;
  return;
}
int hash ( UTFCHAR string)

Definition at line 565 of file hhentry.c.

{
  UTFCHAR *p;
  int hv = 0;
  
  for (p = string; *p; p++){
    hv += *p;
  }
  hv = (hv >> 2) & 0x00ff;
  return hv;
}

Here is the caller graph for this function:

void hash_table_dump_content ( HHList  table[],
int  size,
FILE *  fp 
)

Definition at line 301 of file hhentry.c.

{
  int i;
  char *dict_name = IIIM_KO_LE_DIC;
  int major_version = 1;
  int minor_version = 5;
  int version_signature;

  int table_size  = size;

  version_signature = 0;
  
  version_signature = (major_version & 0x0f);
  version_signature = (version_signature << 8) | (minor_version & 0x0000000f);

  /* write dictionary name to file*/
  fwrite (dict_name, strlen(dict_name) + 1, 1, fp);

  /* write version signature */
  put_int24_to_file (version_signature, fp);

  /* write info for number of tables */
  put_int24_to_file (table_size, fp);
  
  for (i = 0  ; i < table_size; i++){
    hhlist_dump_content (&table[i], fp);
  }
}

Here is the caller graph for this function:

void hash_table_read_content ( FILE *  fp,
HHList **  table,
int *  size 
)

Definition at line 266 of file hhentry.c.

{
  int i;
  char dict_name[100];
  int version_signature;
  int table_size;

  fread (dict_name, strlen(IIIM_KO_LE_DIC) + 1, 1,  fp);
  if (strcmp (dict_name, IIIM_KO_LE_DIC)){
    /* this is not ko-le dictionary, cancelling... */
    exit (-1);
  }
  /* read version signature from the file.
     but, I don't care about version for now.. */  
  get_int24_from_file (&version_signature, fp);

  /* read info for number of tables */
  get_int24_from_file (&table_size, fp);
  *size = table_size;

  if (table_size > 0){
    *table = (HHList *) calloc (table_size, sizeof (HHList));
    for (i = 0 ; i < table_size; i++)
      hhlist_init ((*table) + i);
  }
  
  for (i = 0 ; i < table_size; i++){
#if 0
    fprintf (stdout, "reading table : %d\n", i);
#endif
    hhlist_read_from_file (fp, (*table) + i);
  }
}

Here is the call graph for this function:

void hh_free ( HH hh)

Definition at line 16 of file hhentry.c.

{
  assert (hh != NULL);
  if (hh == NULL)
    return;
  free (hh->utf_hangul);
  free (hh->utf_hanja);
  free (hh);
  hh = NULL;
}

Here is the caller graph for this function:

HH* hh_new ( )

Definition at line 28 of file hhentry.c.

{
  HH *new_hh;
  new_hh = (HH *) calloc (1, sizeof (HH));
  return new_hh;
}
HH* hh_new_with_data ( unsigned char *  utf_hangul,
unsigned char *  utf_hanja 
)

Definition at line 37 of file hhentry.c.

{
  
  HH *new_hh;
  
  assert (utf_hangul != NULL);
  assert (utf_hanja != NULL);

  if (!utf_hangul || !utf_hanja)
    return NULL;
  

  new_hh = (HH *) calloc (1, sizeof (HH));
  new_hh->utf_hangul = (unsigned char *) strdup (utf_hangul);
  new_hh->utf_hanja =  (unsigned char *) strdup (utf_hanja);
  
  return new_hh;
}
void hhentry_copy ( HHEntry  dst,
HHEntry  src 
)

Definition at line 141 of file hhentry.c.

{
  int n_hanja;
  int utf_len;
  assert (dst != NULL);
  assert (src != NULL);

  if (dst == NULL || src == NULL)
    return ;
  utf_len = strlen (src->hangul);
  dst->hangul = (unsigned char *) calloc (utf_len +1, sizeof (unsigned char));
  strcpy (dst->hangul, src->hangul);
  
  dst->n_hanja = src->n_hanja;

  dst->hanja_list =
    (unsigned char **) calloc (src->n_hanja, sizeof (unsigned char *));
  for (n_hanja = 0; n_hanja < dst->n_hanja; n_hanja++){
    utf_len = strlen (src->hanja_list[n_hanja]);
    dst->hanja_list[n_hanja] =
      (unsigned char *) calloc (utf_len +1, sizeof (unsigned char));
    strcpy (dst->hanja_list[n_hanja], src->hanja_list[n_hanja]);
  }
}

Here is the caller graph for this function:

HHEntry hhitem_add_hanja ( HHEntry  hhentry,
unsigned char *  utf_hanja 
)

Definition at line 118 of file hhentry.c.

{
  int n_hanja;
  unsigned char **tmp;
  
  assert (hhentry != NULL);
  assert (utf_hanja != NULL);
  if (hhentry == NULL || utf_hanja == NULL)
    return NULL;
  n_hanja = hhentry->n_hanja;

  tmp = (unsigned char **) calloc (n_hanja + 1, sizeof (unsigned char *));
  memcpy (tmp, hhentry->hanja_list, n_hanja * sizeof (unsigned char *));
  tmp[n_hanja] = (unsigned char *) strdup (utf_hanja);
  
  free (hhentry->hanja_list);
  hhentry->hanja_list = tmp;
  hhentry->n_hanja += 1;
  return hhentry;
}

Here is the caller graph for this function:

int hhitem_comp ( HHEntry  a,
HHEntry  b 
)

Definition at line 111 of file hhentry.c.

{
  return strcmp (a->hangul ,b->hangul);

}

Here is the caller graph for this function:

void hhitem_init ( HHEntry  hhentry)

Definition at line 94 of file hhentry.c.

{
  int n;
  assert (hhentry != NULL);
  
  free (hhentry->hangul);
  hhentry->hangul = NULL;
  
  for (n = 0 ; n < hhentry->n_hanja; n++){
    free (hhentry->hanja_list[n]);
  }
  free (hhentry->hanja_list);
  hhentry->hanja_list = NULL;
  hhentry->n_hanja = 0;
}

Here is the caller graph for this function:

Definition at line 60 of file hhentry.c.

{
  HHEntry new_entry;
  new_entry = (HHEntry) calloc (1, sizeof (HHItem));
  new_entry->n_hanja = 0;
  new_entry->hanja_list = NULL;
  new_entry->hangul = NULL;

  return new_entry;
}

Here is the caller graph for this function:

Definition at line 72 of file hhentry.c.

{
  HHEntry new_entry;

  assert (hh != NULL);
  assert (hh->utf_hangul != NULL);
  assert (hh->utf_hanja != NULL);
  
  new_entry = (HHEntry) calloc (1, sizeof (HHItem));
  new_entry->n_hanja = 1;
  new_entry->hangul = (unsigned char *) strdup (hh->utf_hangul);
  new_entry->hanja_list =
    (unsigned char **) calloc (1, sizeof (unsigned char *));
  new_entry->hanja_list[0] =
    (unsigned char *) strdup (hh->utf_hanja);;

  return new_entry;
}

Here is the caller graph for this function:

void hhitem_print_string ( HHItem hhitem,
FILE *  fp 
)

Definition at line 514 of file hhentry.c.

{
  int n;
  assert (hhitem != NULL);
  if (fp == NULL)
    fp = stdout;
  
  fprintf (fp, "HANGUL[");
  fprintf (fp, hhitem->hangul);
#if 0
  _utfchar_print (hhitem->hangul);
#endif
  fprintf (fp, "]: HANJA[ ");
  
  for (n = 0 ; n < hhitem->n_hanja; n++){
#if 0
    _utfchar_print (hhitem->hanja_list[n]);
#endif
    fprintf (fp, hhitem->hanja_list[n]);
    fprintf (fp, " ");
  }
  fprintf (fp, "]");
}

Here is the caller graph for this function:

void hhitem_read_from_file ( FILE *  fp,
HHEntry  entry_return 
)

Definition at line 353 of file hhentry.c.

{
  int n_hanja;
  int total_length;
  int hangul_length;
  
  int i;
  int j;

  assert (entry_return != NULL);
  /* read length info of this HHItem
     this is not used for now
  */
  get_int24_from_file (&total_length, fp);

  /* read length info of hangul */
  get_int24_from_file (&hangul_length, fp);

  /* read hangul */
  entry_return->hangul =
    (unsigned char *)calloc (hangul_length + 1, sizeof (unsigned char));
  fread (entry_return->hangul, sizeof(unsigned char), hangul_length + 1, fp);

  /* read info for number of hanja */
  get_int24_from_file (&n_hanja, fp);
  entry_return->n_hanja = n_hanja;
  
  entry_return->hanja_list =
    (unsigned char **) calloc (n_hanja, sizeof (unsigned char *));
  for (i = 0 ; i < n_hanja; i++){
    /* now read length info of each hanja string */
    get_int24_from_file (&j, fp);
    entry_return->hanja_list[i] =
      (unsigned char *) calloc (j + 1, sizeof (unsigned char));
    fread (entry_return->hanja_list[i], sizeof (unsigned char), j + 1, fp);
  }
  return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int hhitem_serialize ( HHEntry  hhitem,
int  buffer_size,
unsigned char *  buffer_return 
)

Definition at line 169 of file hhentry.c.

{
  int i, i_len = 0;
  int total = 0;
  unsigned char *pchar;
  int buffer_overflow = 0;
    
  assert (buffer_return != NULL);
  if (buffer_return == NULL){
    fprintf (stderr, "fp can't be null\n");
    return 0;
  }
  pchar = buffer_return;
  /* length infomation */
  /* nothing is written at this time */
  /* just placeholder will be written to file */
  put_int24_to_buffer (i_len, pchar);
  pchar += 3;
  total += 3;
  
  /* hangul length */
  i_len = strlen (hhitem->hangul);
  put_int24_to_buffer (i_len, pchar);
  pchar += 3;
  total += 3;
  
  /* hangul string with terminating null */
  memcpy (pchar, hhitem->hangul, strlen (hhitem->hangul) + 1);
  total += strlen (hhitem->hangul) + 1;
  pchar += strlen (hhitem->hangul) + 1;

  /* dump info of 'number of hanjas' */
  put_int24_to_buffer (hhitem->n_hanja, pchar);
  total += 3;
  pchar += 3;

  /* dump hanja strings */
  for (i = 0 ; i < hhitem->n_hanja; i++){
    i_len = strlen (hhitem->hanja_list[i]);

    /* dump info of 'lenth of hanja' */
    put_int24_to_buffer (i_len, pchar);
    total += 3;
    if (total >= buffer_size){
      buffer_overflow = 1;
      break;
    }
      
    pchar += 3;

    memcpy (pchar, hhitem->hanja_list[i], i_len + 1);
    total += i_len + 1;
    pchar += i_len + 1;
  }

  if (buffer_overflow)
    return 0;
  
  pchar = buffer_return;
  put_int24_to_buffer (total, pchar);

  return total;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HHEntry hhlist_add_hh ( HHList hhlist,
HH item 
)

Definition at line 442 of file hhentry.c.

{
  HHEntry hhitem;
  HHEntry *tmp;
  int n_count;

  assert (hhlist != NULL);
  assert (item != NULL);

  if (hhlist == NULL || item == NULL)
    return NULL;

  n_count = hhlist->n_count;
  
  hhitem = hhlist_search_hhitem (hhlist, item);

  if (hhitem == NULL){
    /* need to add new item */
    hhitem = hhitem_new_with_data (item);
    tmp = (HHEntry *) calloc (n_count + 1, sizeof (HHEntry));
    memcpy (tmp, hhlist->list, n_count * sizeof (HHEntry ));
    tmp[n_count] = hhitem;
    free (hhlist->list);
    hhlist->list = tmp;
    hhlist->n_count = n_count +1;
    
  } else {
    /* need to update existing item */
    hhitem_add_hanja (hhitem, item->utf_hanja);
  }

  return hhitem;
}

Here is the caller graph for this function:

void hhlist_add_hhitem ( HHList hhlist,
HHEntry  hhitem 
)

Definition at line 423 of file hhentry.c.

{
  
  int n_count;
  HHEntry *tmp;
  assert (hhlist != NULL);
  assert (hhitem != NULL);

  n_count = hhlist->n_count;
  tmp = (HHEntry *) calloc (n_count + 1, sizeof (HHEntry));
  memcpy (tmp, hhlist->list, n_count * sizeof (HHEntry));

  tmp [n_count] =  hhitem_new ();
  hhentry_copy (tmp [n_count], hhitem);
  hhlist->n_count += 1;
  hhlist->list = tmp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void hhlist_dump_content ( HHList hhlist,
FILE *  fp 
)

Definition at line 234 of file hhentry.c.

{
  int i;
  int i_total;
  HHEntry p_cursor;
  unsigned char buffer_return[1024];
  unsigned char *tmp;
  int dump_return;

  i_total = hhlist->n_count;
  put_int24_to_file (i_total, fp);
  
  for (i = 0 ; i < hhlist->n_count; i++){
    p_cursor = hhlist->list[i];
    dump_return = hhitem_serialize ( p_cursor,
                                 sizeof (buffer_return), buffer_return);
    if (dump_return){
      fwrite (buffer_return, 1, dump_return, fp);
    } else {
      fprintf
       (stderr, "fatal error: hhlist_dump_content, buffer not enough\n");
      exit (-1);
      tmp = (unsigned char *) calloc (2048, sizeof (unsigned char));
      dump_return = hhitem_serialize (p_cursor, 2048, tmp);
      fwrite (tmp, 1, dump_return, fp);
      free (tmp);
    }
  }
}

Here is the caller graph for this function:

void hhlist_init ( HHList hhlist)

Definition at line 477 of file hhentry.c.

{
  assert (hhlist != NULL);
  hhlist->n_count = 0;
  hhlist = NULL;
}

Here is the caller graph for this function:

void hhlist_print_content ( HHList hhlist,
FILE *  fp 
)

Definition at line 485 of file hhentry.c.

{
  int n_hangul;
  int i, j;
  HHEntry p_cursor;
  
  if (fp == NULL)
    fp = stdout;

  n_hangul = hhlist->n_count;
  for (i = 0 ; i < n_hangul; i++){
    p_cursor = hhlist->list[i];
    fprintf (fp, "[ ");    
    print_utfchar_hex_value (p_cursor->hangul, fp);
    fprintf (fp, " ]");
    fprintf (fp, "\n\t");

    for (j = 0 ; j < p_cursor->n_hanja; j++){
      fprintf (fp, "[ ");    
      print_utfchar_hex_value (p_cursor->hanja_list[j], fp);
      fprintf (fp, " ] ");

    }
    fprintf (fp, "\n");

  }
}

Here is the call graph for this function:

void hhlist_print_string ( HHList hhlist,
FILE *  fp 
)

Definition at line 539 of file hhentry.c.

{
  int n_count;
  if (fp == NULL)
    fp = stdout;
  fprintf (fp, "item count: %d\n", hhlist->n_count);
  for (n_count = 0; n_count < hhlist->n_count; n_count++){
    hhitem_print_string (hhlist->list[n_count], fp);
    fprintf (fp, "\n");
  }
}

Here is the call graph for this function:

void hhlist_read_from_file ( FILE *  fp,
HHList hhlist 
)

Definition at line 332 of file hhentry.c.

{
  int i_total;
  int i;
  HHEntry hhentry;
  hhlist_init (hhlist);

  /* number of hangul-hanja pairs in this bucket */
  get_int24_from_file (&i_total, fp);

  hhentry = hhitem_new ();

  for (i = 0 ; i < i_total; i++){
    hhitem_init (hhentry);
    hhitem_read_from_file (fp, hhentry);
    hhlist_add_hhitem (hhlist, hhentry);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

HHEntry hhlist_search_hhitem ( HHList hhlist,
HH hh 
)

Definition at line 405 of file hhentry.c.

{
  int i;
  assert (hhlist != NULL);
  assert (hh != NULL);

  if (hhlist == NULL || hh == NULL){
    return NULL;
  }

  for (i = 0 ; i < hhlist->n_count; i++){
    if ( strcmp (hhlist->list[i]->hangul, hh->utf_hangul) == 0)
      return hhlist->list[i];
  }
  return NULL;
}

Here is the caller graph for this function:

void print_utfchar_hex_value ( unsigned char *  str,
FILE *  fp 
)

Definition at line 553 of file hhentry.c.

{
  unsigned char *p = str;

  while (*p){
    fprintf (fp, "0x%hhx, ", *p);
    p++;
  }
}

Here is the caller graph for this function:

void put_int24_to_buffer ( int  i,
char *  buffer 
)

Definition at line 578 of file hhentry.c.

{
  char *ptr = buffer;
  if (buffer == NULL){
    fprintf (stderr, "put_int24_to_buffer error: buffer is null\n");
    return;
  }
  *ptr++ = (i >> 16)  & 0xff;
  *ptr++ = (i >> 8)  & 0xff;
  *ptr   = (i)  & 0xff;
  
  return;
}

Here is the caller graph for this function:

void put_int24_to_file ( int  i,
FILE *  fp 
)

Definition at line 593 of file hhentry.c.

{
  int a, b, c;
  if (fp == NULL){
    fprintf (stderr, "put_int24_to_buffer error: fp is null\n");
    return;
  }
  a  = (i >> 16)  & 0x000000ff;
  b  = (i >> 8)  & 0x000000ff;
  c  = (i)  & 0x000000ff;
  
  fputc (a, fp);
  fputc (b, fp);
  fputc (c, fp);
  
  return;
}

Here is the caller graph for this function: