(linenum→info "unix/slp.c:2238")

anthy/9100e/src-diclib/file_dic.c

    1: #include <stdio.h>
    2: #include <string.h>
    3: 
    4: #include <anthy/diclib.h>
    5: #include <anthy/filemap.h>
    6: #include <anthy/alloc.h>
    7: #include <anthy/conf.h>
    8: #include <anthy/logger.h>
    9: #include "diclib_inner.h"
   10: 
   11: /**
   12:    複数セクションがリンクされた辞書
   13:  */
   14: struct file_dic
   15: {
   16:   struct filemapping *mapping;
   17: };
   18: 
   19: static struct file_dic fdic;
   20: 
   21: void*
   22: anthy_file_dic_get_section(const char* section_name)
   23: {
   24:   int i;
   25:   char* head = anthy_mmap_address(fdic.mapping);
   26:   int* p = (int*)head;
   27:   int entry_num = anthy_dic_ntohl(*p++);
   28:   
   29:   for (i = 0; i < entry_num; ++i) {
   30:     int hash_offset = anthy_dic_ntohl(*p++);
   31:     int key_len =  anthy_dic_ntohl(*p++);
   32:     int contents_offset = anthy_dic_ntohl(*p++);
   33:     if (strncmp(section_name, head + hash_offset, key_len) == 0) {
   34:       return (void*)(head + contents_offset);
   35:     }
   36:   }
   37:   return NULL;
   38: }
   39: 
   40: int
   41: anthy_init_file_dic(void)
   42: {
   43:   const char *fn;
   44:   fn = anthy_conf_get_str("DIC_FILE");
   45:   if (!fn) {
   46:     anthy_log(0, "dictionary is not specified.\n");
   47:     return -1;
   48:   }
   49: 
   50:   /* 辞書をメモリ上にmapする */
   51:   fdic.mapping = anthy_mmap(fn, 0);
   52:   if (!fdic.mapping) {
   53:     anthy_log(0, "failed to init file dic.\n");
   54:     return -1;
   55:   }
   56: 
   57:   return 0;
   58: }
   59: 
   60: void
   61: anthy_quit_file_dic(void)
   62: {
   63:   anthy_munmap(fdic.mapping);  
   64: }
   65: 
Syntax (Markdown)