[Anthy-dev 3784] janitor/textdict-no-mmap branch

Back to archive index

NIIBE Yutaka gniib****@fsij*****
2010年 6月 21日 (月) 17:11:28 JST


janitor/textdict-no-mmap branch として plain text の辞書の実装を書き直
しました。

2010-06-21  NIIBE Yutaka  <gniib****@fsij*****>

	* anthy/textdic.h: New file.
	* anthy/textdict.h: Removed.
	* src-worddic/textdic.c: New file.
	* src-worddic/textdict.c: Removed.
	* anthy/Makefile.am (noinst_HEADERS): Added textdic.h.
	Removed textdict.h.
	* src-worddic/Makefile.am (libanthydic_la_SOURCES): Added textdic.c.
	Removed textdict.c.
	* src-worddic/dic_main.h (anthy_ask_scan): Follow new API of textdic.
	* src-worddic/dic_util.c (anthy_private_text_dic): Follow new API
	of textdic.
	(anthy_priv_dic_delete, scan_one_word_cb)
	(select_first_entry_in_textdic, anthy_priv_dic_select_next_entry)
	(do_add_word_to_textdict): Likewise.
	(select_first_entry_in_textdic, add_word_to_textdic): Renamed.
	* src-worddic/priv_dic.c (anthy_private_text_dic)
	(anthy_imported_text_dic, anthy_ask_scan, anthy_init_private_dic)
	(anthy_release_private_dic): Follow new API of textdic.
	(open_textdic): Removed.
	(textdicname): New.
	* src-worddic/word_dic.c (gang_scan, scan_dict, request_scan):
	Follow new API of textdic.

diff --git a/anthy/Makefile.am b/anthy/Makefile.am
index 8f905e4..e11e8a8 100644
--- a/anthy/Makefile.am
+++ b/anthy/Makefile.am
@@ -6,7 +6,7 @@ noinst_HEADERS = xstr.h xchar.h dic.h wtype.h\
  filemap.h \
  logger.h segclass.h \
  depgraph.h \
- texttrie.h textdict.h matrix.h \
+ texttrie.h textdic.h matrix.h \
  prediction.h word_dic.h \
  diclib.h feature_set.h \
  corpus.h convdb.h
diff --git a/anthy/textdic.h b/anthy/textdic.h
new file mode 100644
index 0000000..c37cc18
--- /dev/null
+++ b/anthy/textdic.h
@@ -0,0 +1,9 @@
+#ifndef _textdic_h_included_
+#define _textdic_h_included_
+
+int anthy_textdic_scan(const char *fn, long offset, void *ptr,
+		       int (*func)(void *, long, const char *, const char *));
+int anthy_textdic_insert_line(const char *fn, long offset, const char *line);
+int anthy_textdic_delete_line(const char *fn, long offset);
+
+#endif
diff --git a/anthy/textdict.h b/anthy/textdict.h
deleted file mode 100644
index 6d79bee..0000000
--- a/anthy/textdict.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef _textdict_h_included_
-#define _textdict_h_included_
-
-struct textdict;
-
-struct textdict *anthy_textdict_open(const char *fn, int create);
-void anthy_textdict_close(struct textdict *td);
-/**/
-void anthy_textdict_scan(struct textdict *td, int offset, void *ptr,
-			 int (*fn)(void *, int, const char *, const char *));
-int anthy_textdict_insert_line(struct textdict *td,
-			       int offset, const char *line);
-int anthy_textdict_delete_line(struct textdict *td, int offset);
-
-#endif
diff --git a/src-worddic/Makefile.am b/src-worddic/Makefile.am
index cc135f5..c1476f6 100644
--- a/src-worddic/Makefile.am
+++ b/src-worddic/Makefile.am
@@ -3,7 +3,7 @@ INCLUDES = -I$(top_srcdir)/ -DCONF_DIR=\"$(sysconfdir)\"
 libanthydic_la_SOURCES = \
 	word_dic.c dic_util.c \
 	wtype.c\
-	texttrie.c textdict.c record.c\
+	texttrie.c textdic.c record.c\
 	word_lookup.c use_dic.c \
 	priv_dic.c mem_dic.c \
 	ext_ent.c matrix.c\
diff --git a/src-worddic/dic_main.h b/src-worddic/dic_main.h
index a497ed8..aba6c39 100644
--- a/src-worddic/dic_main.h
+++ b/src-worddic/dic_main.h
@@ -86,8 +86,6 @@ struct word_line {
   const char *word;
 };
 int anthy_parse_word_line(const char *line, struct word_line *res);
-struct textdict;
-void anthy_ask_scan(void (*request_scan)(struct textdict *, void *),
-		    void *arg);
+void anthy_ask_scan(void (*request_scan)(const char *, void *), void *arg);

 #endif
diff --git a/src-worddic/dic_util.c b/src-worddic/dic_util.c
index e038c39..c437d2d 100644
--- a/src-worddic/dic_util.c
+++ b/src-worddic/dic_util.c
@@ -2,14 +2,14 @@
  * 個人辞書管理用の関数群
  *
  * 互換性の都合で
- *  utf8の辞書はtextdict
+ *  utf8の辞書はtextdic
  *  eucjpの辞書はtexttrie
  *  およびrecordを使ってて混乱しまくり
- * textdictへ移行する
+ * textdicへ移行する
  *
  * 開発予定
  *
- *  新規登録はtextdictに対して行うようにする <- todo
+ *  新規登録はtextdicに対して行うようにする <- todo
  *  texttrieの単語は移行するようにする
  *  record関係は消す
  *
@@ -42,7 +42,7 @@
 #include <anthy/conf.h>
 #include <anthy/dic.h>
 #include <anthy/texttrie.h>
-#include <anthy/textdict.h>
+#include <anthy/textdic.h>
 #include <anthy/dicutil.h>

 #include "dic_main.h"
@@ -64,14 +64,14 @@ static int gIsInit;
 static int dic_util_encoding;

 extern struct text_trie *anthy_private_tt_dic;
-extern struct textdict *anthy_private_text_dic;
+extern const char *anthy_private_text_dic;
 /* 現在選択されている読み */
 static struct iterate_contex {
   /**/
   int in_tt;
   /* texttrie */
   char key_buf[MAX_KEY_LEN+32];
-  /* textdictの検索用 */
+  /* textdicの検索用 */
   int dicfile_offset;
   char *current_index;
   char *current_line;
@@ -196,13 +196,13 @@ anthy_priv_dic_delete(void)
 {
   delete_prefix(encoding_prefix(ANTHY_EUC_JP_ENCODING));
   /**/
-  while (!anthy_textdict_delete_line(anthy_private_text_dic, 0)) {
+  while (!anthy_textdic_delete_line(anthy_private_text_dic, 0)) {
     /**/
   }
 }

 static int
-scan_one_word_cb(void *p, int next_offset, const char *key, const char *n)
+scan_one_word_cb(void *p, long next_offset, const char *key, const char *n)
 {
   (void)p;
   set_current_line(key, n);
@@ -211,13 +211,13 @@ scan_one_word_cb(void *p, int next_offset, const char *key, const char *n)
 }

 static int
-select_first_entry_in_textdict(void)
+select_first_entry_in_textdic(void)
 {
   word_iterator.dicfile_offset = 0;
   set_current_line(NULL, NULL);
-  anthy_textdict_scan(anthy_private_text_dic,
-		      word_iterator.dicfile_offset, NULL,
-		      scan_one_word_cb);
+  anthy_textdic_scan(anthy_private_text_dic,
+		     word_iterator.dicfile_offset, NULL,
+		     scan_one_word_cb);
   if (word_iterator.current_line) {
     word_iterator.in_tt = 0;
     return 0;
@@ -231,7 +231,7 @@ int
 anthy_priv_dic_select_first_entry(void)
 {
   if (dic_util_encoding == ANTHY_UTF8_ENCODING) {
-    return select_first_entry_in_textdict();
+    return select_first_entry_in_textdic();
   }
   if (anthy_private_tt_dic) {
     sprintf(word_iterator.key_buf, "%s", encoding_prefix(dic_util_encoding));
@@ -241,8 +241,8 @@ anthy_priv_dic_select_first_entry(void)
       return 0;
     }
   }
-  /* 単語が無いのでtextdictに移動を試みる */
-  return select_first_entry_in_textdict();
+  /* 単語が無いのでtextdicに移動を試みる */
+  return select_first_entry_in_textdic();
 }

 /** (API) 現在選択されている単語の次の単語を選択する */
@@ -251,9 +251,9 @@ anthy_priv_dic_select_next_entry(void)
 {
   if (!word_iterator.in_tt) {
     set_current_line(NULL, NULL);
-    anthy_textdict_scan(anthy_private_text_dic, word_iterator.dicfile_offset,
-			NULL,
-			scan_one_word_cb);
+    anthy_textdic_scan(anthy_private_text_dic, word_iterator.dicfile_offset,
+		       NULL,
+		       scan_one_word_cb);
     if (word_iterator.current_line) {
       return 0;
     }
@@ -262,8 +262,8 @@ anthy_priv_dic_select_next_entry(void)
   if (find_next_key(encoding_prefix(dic_util_encoding))) {
     return 0;
   }
-  /* 単語が無いのでtextdictに移動を試みる */
-  return select_first_entry_in_textdict();
+  /* 単語が無いのでtextdicに移動を試みる */
+  return select_first_entry_in_textdic();
 }

 /** 未実装 */
@@ -371,7 +371,7 @@ anthy_priv_dic_get_word(char *buf, int len)
 }

 static int
-find_cb(void *p, int next_offset, const char *key, const char *n)
+find_cb(void *p, long next_offset, const char *key, const char *n)
 {
   struct scan_context *sc = p;
   struct word_line res;
@@ -390,7 +390,7 @@ find_cb(void *p, int next_offset, const char *key, const char *n)
 }

 static int
-order_cb(void *p, int next_offset, const char *key, const char *n)
+order_cb(void *p, long next_offset, const char *key, const char *n)
 {
   struct scan_context *sc = p;
   (void)n;
@@ -404,9 +404,9 @@ order_cb(void *p, int next_offset, const char *key, const char *n)

 /* 引数はutf8 */
 static int
-do_add_word_to_textdict(struct textdict *td, int offset,
-			const char *yomi, const char *word,
-			const char *wt_name, int freq)
+do_add_word_to_textdic(const char *td, int offset,
+		       const char *yomi, const char *word,
+		       const char *wt_name, int freq)
 {
   char *buf = malloc(strlen(yomi) + strlen(word) + strlen(wt_name) + 20);
   int rv;
@@ -414,7 +414,7 @@ do_add_word_to_textdict(struct textdict *td, int offset,
     return -1;
   }
   sprintf(buf, "%s %s*%d %s\n", yomi, wt_name, freq, word);
-  rv = anthy_textdict_insert_line(td, offset, buf);
+  rv = anthy_textdic_insert_line(td, offset, buf);
   free(buf);
   return rv;
 }
@@ -471,8 +471,8 @@ find_same_word(char *idx_buf, const char *yomi,
 }

 static int
-add_word_to_textdict(const char *yomi, const char *word,
-		     const char *wt_name, int freq)
+add_word_to_textdic(const char *yomi, const char *word,
+		    const char *wt_name, int freq)
 {
   struct scan_context sc;
   int rv;
@@ -502,10 +502,10 @@ add_word_to_textdict(const char *yomi, const char *word,
   /**/
   sc.offset = 0;
   sc.found_word = 0;
-  anthy_textdict_scan(anthy_private_text_dic, 0, &sc,
-		      find_cb);
+  anthy_textdic_scan(anthy_private_text_dic, 0, &sc,
+		     find_cb);
   if (sc.found_word == 1) {
-    anthy_textdict_delete_line(anthy_private_text_dic, sc.offset);
+    anthy_textdic_delete_line(anthy_private_text_dic, sc.offset);
   }
   if (freq == 0) {
     return ANTHY_DIC_UTIL_OK;
@@ -513,11 +513,11 @@ add_word_to_textdict(const char *yomi, const char *word,
   /* 追加する場所を探す */
   sc.offset = 0;
   sc.found_word = 0;
-  anthy_textdict_scan(anthy_private_text_dic, 0, &sc,
-		      order_cb);
+  anthy_textdic_scan(anthy_private_text_dic, 0, &sc,
+		     order_cb);
   /* 追加する */
-  rv = do_add_word_to_textdict(anthy_private_text_dic, sc.offset,
-			       yomi, word, wt_name, freq);
+  rv = do_add_word_to_textdic(anthy_private_text_dic, sc.offset,
+			      yomi, word, wt_name, freq);
   if (!rv) {
     return ANTHY_DIC_UTIL_OK;
   }
@@ -532,12 +532,12 @@ anthy_priv_dic_add_entry(const char *yomi, const char *word,
 			 const char *wt_name, int freq)
 {
   if (dic_util_encoding == ANTHY_UTF8_ENCODING) {
-    return add_word_to_textdict(yomi, word, wt_name, freq);
+    return add_word_to_textdic(yomi, word, wt_name, freq);
   } else {
     int rv;
     char *yomi_utf8 = anthy_conv_euc_to_utf8(yomi);
     char *word_utf8 = anthy_conv_euc_to_utf8(word);
-    rv = add_word_to_textdict(yomi_utf8, word_utf8, wt_name, freq);
+    rv = add_word_to_textdic(yomi_utf8, word_utf8, wt_name, freq);
     free(yomi_utf8);
     free(word_utf8);
     return rv;
diff --git a/src-worddic/priv_dic.c b/src-worddic/priv_dic.c
index 04e1f19..1a7de24 100644
--- a/src-worddic/priv_dic.c
+++ b/src-worddic/priv_dic.c
@@ -39,15 +39,15 @@
 #include <anthy/conf.h>
 #include <anthy/logger.h>
 #include <anthy/texttrie.h>
-#include <anthy/textdict.h>
+#include <anthy/textdic.h>
 #include <anthy/word_dic.h>
 #include "dic_main.h"
 #include "dic_ent.h"

 /* 個人辞書 */
 struct text_trie *anthy_private_tt_dic;
-struct textdict *anthy_private_text_dic;
-static struct textdict *anthy_imported_text_dic;
+const char *anthy_private_text_dic;
+static const char *anthy_imported_text_dic;
 static char *imported_dic_dir;
 /* ロック用の変数 */
 static char *lock_fn;
@@ -100,15 +100,12 @@ open_tt_dic(const char *home, const char *id)
   return tt;
 }

-static struct textdict *
-open_textdic(const char *home, const char *name, const char *id)
+static const char *
+textdicname (const char *home, const char *name, const char *id)
 {
   char *fn = malloc(strlen(home) + strlen(name) + strlen(id) + 10);
-  struct textdict *td;
   sprintf(fn, "%s/.anthy/%s%s", home, name, id);
-  td = anthy_textdict_open(fn, 0);
-  free(fn);
-  return td;
+  return (const char *)fn;
 }

 void
@@ -278,21 +275,19 @@ anthy_parse_word_line(const char *line, struct word_line *res)
 }

 void
-anthy_ask_scan(void (*request_scan)(struct textdict *, void *),
-	       void *arg)
+anthy_ask_scan (void (*request_scan)(const char *, void *), void *arg)
 {
   DIR *dir;
   struct dirent *de;
   int size = 0;
-  request_scan(anthy_private_text_dic, arg);
-  request_scan(anthy_imported_text_dic, arg);
+  request_scan (anthy_private_text_dic, arg);
+  request_scan (anthy_imported_text_dic, arg);
   dir = opendir(imported_dic_dir);
   if (!dir) {
     return ;
   }
   while ((de = readdir(dir))) {
     struct stat st_buf;
-    struct textdict *td;
     char *fn = malloc(strlen(imported_dic_dir) +
 		      strlen(de->d_name) + 3);
     if (!fn) {
@@ -312,9 +307,7 @@ anthy_ask_scan(void (*request_scan)(struct textdict *, void *),
       free(fn);
       break;
     }
-    td = anthy_textdict_open(fn, 0);
-    request_scan(td, arg);
-    anthy_textdict_close(td);
+    request_scan(fn, arg);
     free(fn);
   }
   closedir(dir);
@@ -382,17 +375,14 @@ anthy_init_private_dic(const char *id)
     anthy_trie_close(anthy_private_tt_dic);
   }
   /**/
-  anthy_textdict_close(anthy_private_text_dic);
-  anthy_textdict_close(anthy_imported_text_dic);
-  /**/
   if (lock_fn) {
     free(lock_fn);
   }
   init_lock_fn(home, id);
   anthy_private_tt_dic = open_tt_dic(home, id);
   /**/
-  anthy_private_text_dic = open_textdic(home, "private_words_", id);
-  anthy_imported_text_dic = open_textdic(home, "imported_words_", id);
+  anthy_private_text_dic = textdicname (home, "private_words_", id);
+  anthy_imported_text_dic = textdicname (home, "imported_words_", id);
   imported_dic_dir = malloc(strlen(home) + strlen(id) + 30);
   sprintf(imported_dic_dir, "%s/.anthy/imported_words_%s.d/", home, id);
 }
@@ -404,9 +394,8 @@ anthy_release_private_dic(void)
     anthy_trie_close(anthy_private_tt_dic);
     anthy_private_tt_dic = NULL;
   }
-  /**/
-  anthy_textdict_close(anthy_private_text_dic);
-  anthy_textdict_close(anthy_imported_text_dic);
+  free (anthy_private_text_dic);
+  free (anthy_imported_text_dic);
   free(imported_dic_dir);
   anthy_private_text_dic = NULL;
   anthy_imported_text_dic = NULL;
diff --git a/src-worddic/textdic.c b/src-worddic/textdic.c
new file mode 100644
index 0000000..24ec1bc
--- /dev/null
+++ b/src-worddic/textdic.c
@@ -0,0 +1,216 @@
+/*
+ * textdic.c -- handle dictionary of a plain text file
+ *
+ * Copyright (C) 2010 Free Software Initiative of Japan
+ * Author: NIIBE Yutaka <gniib****@fsij*****>
+ *
+ * This file is a part of Anthy, Japanese Kana-Kanji Conversion Engine
+ * Library.
+ *
+ * Anthy is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "anthy/textdic.h"
+
+#define BUFSIZE 1024
+
+int
+anthy_textdic_scan (const char *name, long offset, void *data,
+		    int (*func)(void *, long, const char *, const char *))
+{
+  FILE *fp;
+  char buf[BUFSIZE];
+
+  fp = fopen (name, "r");
+  if (!fp)
+    return -1;
+
+  if (fseek (fp, offset, SEEK_SET) < 0)
+    {
+      fclose (fp);
+      return -1;
+    }
+
+  /*
+   * Call FUNC for each line and if FUNC success, stop at there
+   */
+  while (fgets (buf, BUFSIZE, fp) != NULL)
+    {
+      int i;
+      const char *column0, *column1;
+      char *end_of_column0;
+
+      column0 = buf;
+      end_of_column0 = column1 = NULL;
+      for (i = 0; i < BUFSIZE; i++)
+	if (buf[i])
+	  {
+	    if (end_of_column0 == NULL)
+	      {
+		if (buf[i] == ' ')
+		  end_of_column0 = &buf[i];
+	      }
+	    else
+	      if (column1 == NULL && buf[i] != ' ')
+		column1 = &buf[i];
+	  }
+
+      offset += i;
+      if (column1 == NULL)
+	continue;
+
+      buf[i - 1] = '\0';	/* chop newline */
+      *end_of_column0 = '\0';
+
+      if (func (data, offset, column0, column1))
+	break;
+    }
+
+  fclose (fp);
+  return 0;
+}
+
+static char *
+tempfile (const char *orig_filename, FILE **fp_p)
+{
+  char *filename = (char *)malloc ((strlen (orig_filename) + 6 + 1));
+  int fd;
+  FILE *fp;
+
+  strcpy (filename, orig_filename);
+  strcat (filename, "XXXXXX");
+  fd = mkstemp (filename);
+  if (fd < 0)
+    {
+      free (filename);
+      return NULL;
+    }
+
+  if ((fp = fdopen (fd, "w")) == NULL)
+    {
+      close (fd);
+      unlink (filename);
+      free (filename);
+      return NULL;
+    }
+
+  *fp_p = fp;
+  return filename;
+}
+
+static int
+do_textdic_at (const char *name, long offset, void *data,
+	       int (*work) (void *, FILE *, FILE *))
+{
+  FILE *fp_r, *fp_w;
+  char buf[BUFSIZE];
+  char *filename;
+  int i, r;
+
+  if ((fp_r = fopen (name, "r")) == NULL)
+    return -1;
+
+  if ((filename = tempfile (name, &fp_w)) == NULL)
+    goto error_w;
+
+  for (i = 0; i < offset - BUFSIZE; i += BUFSIZE)
+    if ((r = fread (buf, 1, BUFSIZE, fp_r)) < BUFSIZE)
+      goto error_file;
+    else if ((r = fwrite (buf, 1, BUFSIZE, fp_w)) < BUFSIZE)
+      goto error_file;
+
+  if ((offset - i) > 0)
+    {
+      if ((r = fread (buf, 1, offset - i, fp_r)) < offset - i)
+	goto error_file;
+      else if ((r = fwrite (buf, 1, offset - i, fp_w))  < offset - i)
+	goto error_file;
+    }
+
+  if (work (data, fp_r, fp_w) < 0)
+    goto error_file;
+
+  while (1)
+    {
+      int r0;
+
+      r = fread (buf, 1, BUFSIZE, fp_r);
+      r0 = fwrite (buf, 1, r, fp_w);
+      if (r0 != r)
+	goto error_file;
+
+      if (r < BUFSIZE)
+	{
+	  if (feof (fp_r))
+	    break;
+	  else
+	    goto error_file;
+	}
+    }
+
+  fclose (fp_w);
+  fclose (fp_r);
+
+  rename (filename, name);
+  free (filename);
+  return 0;
+
+ error_file:
+  fclose (fp_w);
+  unlink (filename);
+ error_w:
+  fclose (fp_r);
+  free (filename);
+  return -1;
+}
+
+static int
+delete_line (void *data, FILE *fp_r, FILE *fp_w)
+{
+  char buf[BUFSIZE];
+
+  if (fgets (buf, BUFSIZE, fp_r) == NULL)
+    return -1;
+
+  return 0;
+}
+
+int
+anthy_textdic_delete_line (const char *name, long offset)
+{
+  return do_textdic_at (name, offset, NULL, delete_line);
+}
+
+static int
+insert_line (void *data, FILE *fp_r, FILE *fp_w)
+{
+  const char *line = (const char *)data;
+  int r;
+
+  r = fwrite (line, 1, strlen (line), fp_w);
+  if (r < strlen (line))
+    return -1;
+
+  return 0;
+}
+
+int
+anthy_textdic_insert_line (const char *name, long offset, const char *line)
+{
+  return do_textdic_at (name, offset, (void *)line, insert_line);
+}
diff --git a/src-worddic/textdict.c b/src-worddic/textdict.c
deleted file mode 100644
index 4d3d04b..0000000
--- a/src-worddic/textdict.c
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * ソートされたテキストから検索を行う
- */
-/*
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License as published by the Free Software Foundation; either
-  version 2 of the License, or (at your option) any later version.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with this library; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
- */
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <anthy/filemap.h>
-#include <anthy/textdict.h>
-#include "dic_main.h"
-
-struct textdict {
-  char *fn;
-  char *ptr;
-  struct filemapping *mapping;
-};
-
-struct textdict *
-anthy_textdict_open(const char *fn, int create)
-{
-  struct textdict *td = malloc(sizeof(struct textdict));
-  if (!td) {
-    return NULL;
-  }
-  td->fn = strdup(fn);
-  if (!td->fn) {
-    free(td);
-    return NULL;
-  }
-  td->mapping = NULL;
-  return td;
-}
-
-
-static void
-unmap(struct textdict *td)
-{
-  if (td->mapping) {
-    anthy_munmap(td->mapping);
-    td->mapping = NULL;
-  }
-}
-
-void
-anthy_textdict_close(struct textdict *td)
-{
-  if (!td) {
-    return ;
-  }
-  unmap(td);
-  free(td->fn);
-  free(td);
-}
-
-static int
-update_mapping(struct textdict *td)
-{
-  if (td->mapping) {
-    anthy_munmap(td->mapping);
-  }
-  td->mapping = anthy_mmap(td->fn, 1);
-  if (!td->mapping) {
-    td->ptr = NULL;
-    return 1;
-  }
-  td->ptr = anthy_mmap_address(td->mapping);
-  return 0;
-}
-
-static int
-expand_file(struct textdict *td, int len)
-{
-  FILE *fp;
-  char buf[256];
-  int c;
-  fp = fopen(td->fn, "a+");
-  if (!fp) {
-    return -1;
-  }
-  memset(buf, '\n', 256);
-  c = 1;
-  if (len > 256) {
-    c *= fwrite(buf, 256, len / 256, fp);
-  }
-  if (len % 256) {
-    c *= fwrite(buf, len % 256, 1, fp);
-  }
-  fclose(fp);
-  if (c == 0) {
-    return -1;
-  }
-  return 0;
-}
-
-void
-anthy_textdict_scan(struct textdict *td, int offset, void *ptr,
-		    int (*fun)(void *, int, const char *, const char *))
-{
-  FILE *fp;
-  char buf[1024];
-  if (!td) {
-    return ;
-  }
-  fp = fopen(td->fn, "r");
-  if (!fp) {
-    return ;
-  }
-  if (fseek(fp, offset, SEEK_SET)) {
-    fclose(fp);
-    return ;
-  }
-  while (fgets(buf, 1024, fp)) {
-    char *p = strchr(buf, ' ');
-    int len, r;
-    len = strlen(buf);
-    offset += len;
-    buf[len - 1] = 0;
-    if (!p) {
-      continue;
-    }
-    *p = 0;
-    p++;
-    while (*p == ' ') {
-      p++;
-    }
-    /* call it */
-    r = fun(ptr, offset, buf, p);
-    if (r) {
-      break;
-    }
-  }
-  fclose(fp);
-}
-
-int
-anthy_textdict_delete_line(struct textdict *td, int offset)
-{
-  FILE *fp;
-  char buf[1024];
-  int len, size;
-  fp = fopen(td->fn, "r");
-  if (!fp) {
-    return -1;
-  }
-  if (fseek(fp, offset, SEEK_SET)) {
-    fclose(fp);
-    return -1;
-  }
-  if (!fgets(buf, 1024, fp)) {
-    fclose(fp);
-    return -1;
-  }
-  len = strlen(buf);
-  fclose(fp);
-  update_mapping(td);
-  if (!td->mapping) {
-    return -1;
-  }
-  size = anthy_mmap_size(td->mapping);
-  memmove(&td->ptr[offset], &td->ptr[offset+len], size - offset - len);
-  unmap(td);
-  if (size - len == 0) {
-    unlink(td->fn);
-    return 0;
-  }
-  truncate(td->fn, size - len);
-  return 0;
-}
-
-int
-anthy_textdict_insert_line(struct textdict *td, int offset,
-			   const char *line)
-{
-  int len = strlen(line);
-  int size;
-  if (!td) {
-    return -1;
-  }
-  if (expand_file(td, len)) {
-    return -1;
-  }
-  update_mapping(td);
-  size = anthy_mmap_size(td->mapping);
-  memmove(&td->ptr[offset+len], &td->ptr[offset], size - offset - len);
-  memcpy(&td->ptr[offset], line, len);
-  return 0;
-}
diff --git a/src-worddic/word_dic.c b/src-worddic/word_dic.c
index 1b1df35..d14bedd 100644
--- a/src-worddic/word_dic.c
+++ b/src-worddic/word_dic.c
@@ -33,7 +33,7 @@
 #include <anthy/logger.h>
 #include <anthy/xchar.h>
 #include <anthy/feature_set.h>
-#include <anthy/textdict.h>
+#include <anthy/textdic.h>

 #include <anthy/diclib.h>

@@ -255,7 +255,7 @@ load_word(xstr *xs, const char *n, int is_reverse)
 }

 static int
-gang_scan(void *p, int offset, const char *key, const char *n)
+gang_scan(void *p, long offset, const char *key, const char *n)
 {
   struct gang_scan_context *gsc = p;
   struct gang_elm *elm;
@@ -284,13 +284,13 @@ gang_scan(void *p, int offset, const char *key, const char *n)
 }

 static void
-scan_dict(struct textdict *td, int nr, struct gang_elm **array)
+scan_dict(const char *td, int nr, struct gang_elm **array)
 {
   struct gang_scan_context gsc;
   gsc.nr = nr;
   gsc.array = array;
   gsc.nth = 0;
-  anthy_textdict_scan(td, 0, &gsc, gang_scan);
+  anthy_textdic_scan(td, 0, &gsc, gang_scan);
 }

 struct scan_arg {
@@ -299,10 +299,10 @@ struct scan_arg {
 };

 static void
-request_scan(struct textdict *td, void *arg)
+request_scan(const char *tdname, void *arg)
 {
   struct scan_arg *sarg = (struct scan_arg *)arg;
-  scan_dict(td, sarg->nr, sarg->array);
+  scan_dict(tdname, sarg->nr, sarg->array);
 }

 static void




Anthy-dev メーリングリストの案内
Back to archive index