Author: yamakenz
Date: Mon Aug 13 02:03:32 2007
New Revision: 4841

Modified:
   trunk/uim/anthy.c

Log:
* uim/anthy.c
  - (validate_segment_index): New static function
  - (init_anthy_lib, create_context, get_nr_segments,
    get_nr_candidates, get_nth_candidate, get_segment_length,
    get_nr_predictions, get_nth_prediction): Add exception-based error
    handling
  - (uim_plugin_instance_init): Require SRFI-1 for delete!


Modified: trunk/uim/anthy.c
==============================================================================
--- trunk/uim/anthy.c   (original)
+++ trunk/uim/anthy.c   Mon Aug 13 02:03:32 2007
@@ -49,6 +49,18 @@
 static uim_bool initialized;
 static uim_lisp context_list;
 
+static void
+validate_segment_index(anthy_context_t ac, int i)
+{
+  int err;
+  struct anthy_conv_stat cs;
+
+  err = anthy_get_stat(ac, &cs);
+  if (err)
+    uim_fatal_error("anthy_get_stat() failed");
+  if (!(0 <= i && i < cs.nr_segment))
+    uim_scm_error_obj("invalid segment index", uim_scm_make_int(i));
+}
 
 static uim_lisp
 anthy_version()
@@ -61,7 +73,7 @@
 {
   if (!initialized) {
     if (anthy_init() == -1)
-      return uim_scm_f();
+      uim_fatal_error("anthy_init() failed");
 
     initialized = UIM_TRUE;
   }
@@ -76,13 +88,13 @@
   uim_lisp ac_;
 
   ac = anthy_create_context();
-  if (ac) {
-    ac_ = uim_scm_make_ptr(ac);
-    context_list = uim_scm_callf("cons", "oo", ac_, context_list);
-    return ac_;
-  }
+  if (!ac)
+    uim_fatal_error("anthy_create_context() failed");
 
-  return uim_scm_f();
+  ac_ = uim_scm_make_ptr(ac);
+  context_list = uim_scm_callf("cons", "oo", ac_, context_list);
+
+  return ac_;
 }
 
 
@@ -122,7 +134,7 @@
   ac = uim_scm_c_ptr(ac_);
   err = anthy_get_stat(ac, &cs);
   if (err)
-    return uim_scm_f();
+    uim_fatal_error("anthy_get_stat() failed");
 
   return uim_scm_make_int(cs.nr_segment);
 }
@@ -132,19 +144,16 @@
 {
   anthy_context_t ac;
   int seg, err;
-  struct anthy_conv_stat cs;
   struct anthy_segment_stat ss;
 
   ac = uim_scm_c_ptr(ac_);
   seg = uim_scm_c_int(seg_);
 
-  err = anthy_get_stat(ac, &cs);
-  if (err || !(0 <= seg && seg < cs.nr_segment))
-    return uim_scm_f();  /* FIXME: uim_scm_error() */
+  validate_segment_index(ac, seg);
 
   err = anthy_get_segment_stat(ac, seg, &ss);
   if (err)
-    return uim_scm_f();
+    uim_fatal_error("anthy_get_segment_stat() failed");
 
   return uim_scm_make_int(ss.nr_candidate);
 }
@@ -163,13 +172,13 @@
 
   buflen = anthy_get_segment(ac, seg, nth, NULL, 0);
   if (buflen == -1)
-    return uim_scm_f();  /* FIXME: uim_scm_error() */
+    uim_fatal_error("anthy_get_segment() failed");
 
-  buf = malloc(buflen + 1);
+  buf = uim_malloc(buflen + 1);
   buflen = anthy_get_segment(ac, seg, nth, buf, buflen + 1);
   if (buflen == -1) {
     free(buf);
-    return uim_scm_f();
+    uim_fatal_error("anthy_get_segment() failed");
   }
   buf_ = uim_scm_make_str_directly(buf);
 
@@ -190,19 +199,16 @@
 {
   anthy_context_t ac;
   int seg, err;
-  struct anthy_conv_stat cs;
   struct anthy_segment_stat ss;
 
   ac = uim_scm_c_ptr(ac_);
   seg = uim_scm_c_int(seg_);
 
-  err = anthy_get_stat(ac, &cs);
-  if (err || !(0 <= seg && seg < cs.nr_segment))
-    return uim_scm_f();  /* FIXME: uim_scm_error() */
+  validate_segment_index(ac, seg);
 
   err = anthy_get_segment_stat(ac, seg, &ss);
   if (err)
-    return uim_scm_f();
+    uim_fatal_error("anthy_get_segment_stat() failed");
 
   return uim_scm_make_int(ss.seg_len);
 }
@@ -261,10 +267,12 @@
   ac = uim_scm_c_ptr(ac_);
 
   err = anthy_get_prediction_stat(ac, &ps);
-  if (!err)
-    return uim_scm_make_int(ps.nr_prediction);
-#endif
+  if (err)
+    uim_fatal_error("anthy_get_prediction_stat() failed");
+  return uim_scm_make_int(ps.nr_prediction);
+#else
   return uim_scm_f();
+#endif
 }
 
 static uim_lisp
@@ -281,13 +289,13 @@
 
   buflen = anthy_get_prediction(ac, nth, NULL, 0);
   if (buflen == -1)
-    return uim_scm_f();
+    uim_fatal_error("anthy_get_prediction() failed");
 
-  buf = malloc(buflen + 1);
+  buf = uim_malloc(buflen + 1);
   buflen = anthy_get_prediction(ac, nth, buf, buflen + 1);
   if (buflen == -1) {
     free(buf);
-    return uim_scm_f();
+    uim_fatal_error("anthy_get_prediction() failed");
   }
   buf_ = uim_scm_make_str_directly(buf);
 
@@ -325,6 +333,8 @@
 {
   context_list = uim_scm_null();
   uim_scm_gc_protect(&context_list);
+
+  uim_scm_eval_c_string("(require-extension (srfi 1))"); /* for delete! */
 
   uim_scm_init_subr_0("anthy-lib-init", init_anthy_lib);
   uim_scm_init_subr_0("anthy-lib-alloc-context", create_context);

Reply via email to