Copyright years udpated.
[m17n/m17n-lib.git] / src / internal.h
index 3354e29..71020db 100644 (file)
@@ -1,5 +1,5 @@
 /* internal.h -- common header file for the internal CORE and SHELL APIs.
-   Copyright (C) 2003, 2004                            
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
 
@@ -17,7 +17,7 @@
 
    You should have received a copy of the GNU Lesser General Public
    License along with the m17n library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    02111-1307, USA.  */
 
 #ifndef _M17N_INTERNAL_H_
@@ -35,6 +35,13 @@ extern int m17n__gui_initialized;
 
 extern int mdebug_hook ();
 
+#if ENABLE_NLS
+#include <libintl.h>
+#define _(String) dgettext ("m17n-lib", String)
+#else
+#define _(String) (String)
+#endif
+
 /** Return with code RET while setting merror_code to ERR.  */
 
 #define MERROR(err, ret)       \
@@ -60,6 +67,13 @@ extern int mdebug_hook ();
     goto warning;      \
   } while (0)
 
+#define MFATAL(err)    \
+  do {                 \
+    mdebug_hook ();    \
+    exit (err);                \
+  } while (0)
+
+#define MFAILP(cond) ((cond) ? 0 : mdebug_hook ())
 
 #define M_CHECK_CHAR(c, ret)           \
   if ((c) < 0 || (c) > MCHAR_MAX)      \
@@ -86,11 +100,10 @@ extern int mdebug_hook ();
     be one of enum MErrorCode.  If the allocation fails, the macro
     MEMORY_FULL () is called with argument ERR.  */
 
-#define MTABLE_MALLOC(p, size, err)            \
-  do {                                         \
-    int bytes = sizeof (*(p)) * (size);                \
-    if (! ((p) = (void *) malloc (bytes)))     \
-      MEMORY_FULL (err);                       \
+#define MTABLE_MALLOC(p, size, err)                            \
+  do {                                                         \
+    if (! ((p) = (void *) malloc (sizeof (*(p)) * (size))))    \
+      MEMORY_FULL (err);                                       \
   } while (0)
 
 
@@ -104,6 +117,9 @@ extern int mdebug_hook ();
       MEMORY_FULL (err);                                       \
   } while (0)
 
+#define MTABLE_CALLOC_SAFE(p, size)    \
+  ((p) = (void *) calloc (sizeof (*(p)), (size)))
+
 
 /** The macro MTABLE_REALLOC () changes the size of memory block
     pointed to by P to a size suitable for an array of SIZE objects.
@@ -126,10 +142,10 @@ extern int mdebug_hook ();
 
 #define MTABLE_ALLOCA(p, size, err)            \
   do {                                         \
-    int bytes = sizeof (*(p)) * (size);                \
-    if (! ((p) = (void *) alloca (bytes)))     \
+    int allocasize = sizeof (*(p)) * (size);   \
+    if (! ((p) = (void *) alloca (allocasize)))        \
       MEMORY_FULL (err);                       \
-    memset ((p), 0, bytes);                    \
+    memset ((p), 0, allocasize);               \
   } while (0)
 
 
@@ -149,6 +165,44 @@ extern int mdebug_hook ();
 
 #define MSTRUCT_CALLOC(p, err) MTABLE_CALLOC ((p), 1, (err))
 
+#define MSTRUCT_CALLOC_SAFE(p) MTABLE_CALLOC_SAFE ((p), 1)
+
+#define USE_SAFE_ALLOCA \
+  int sa_must_free = 0, sa_size = 0
+
+/* P must be the same in all calls to SAFE_ALLOCA and SAFE_FREE in a
+   function.  */
+
+#define SAFE_ALLOCA(P, SIZE)           \
+  do {                                 \
+    if (sa_size < (SIZE))              \
+      {                                        \
+       if (sa_must_free)               \
+         (P) = realloc ((P), (SIZE));  \
+       else                            \
+         {                             \
+           (P) = alloca ((SIZE));      \
+           if (! (P))                  \
+             {                         \
+               (P) = malloc (SIZE);    \
+               sa_must_free = 1;       \
+             }                         \
+         }                             \
+       if (! (P))                      \
+         MEMORY_FULL (1);              \
+       sa_size = (SIZE);               \
+      }                                        \
+  } while (0)
+
+#define SAFE_FREE(P)                   \
+  do {                                 \
+    if (sa_must_free && sa_size > 0)   \
+      {                                        \
+       free ((P));                     \
+       sa_must_free = sa_size = 0;     \
+      }                                        \
+  } while (0)
+
 \f
 /** Extendable array.  */
 
@@ -245,19 +299,23 @@ typedef struct
 typedef struct
 {
   /**en Reference count of the object.  */
+  /**ja ¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô.  */
   unsigned ref_count : 16;
 
   unsigned ref_count_extended : 1;
 
   /**en A flag bit used for various perpose.  */
+  /**ja ¤µ¤Þ¤¶¤Þ¤ÊÌÜŪ¤ËÍѤ¤¤é¤ì¤ë¥Õ¥é¥°¥Ó¥Ã¥È.  */
   unsigned flag : 15;
 
   union {
     /**en If <ref_count_extended> is zero, a function to free the
        object.  */
+    /**ja <ref_count_extended> ¤¬ 0 ¤Ê¤é¤Ð¥ª¥Ö¥¸¥§¥¯¥È¤ò²òÊü¤¹¤ë´Ø¿ô.  */
     void (*freer) (void *);
     /**en If <ref_count_extended> is nonzero, a pointer to the
        struct M17NObjectRecord.  */
+    /**ja <ref_count_extended> ¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð¹½Â¤ÂΠM17NObjectRecord ¤Ø¤Î¥Ý¥¤¥ó¥¿.  */
     M17NObjectRecord *record;
   } u;
 } M17NObject;
@@ -275,6 +333,7 @@ typedef struct
 
 /**en Increment the reference count of OBJECT if the count is not
    0.  */
+/**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð 1 Áý¤ä¤¹.  */
 
 #define M17N_OBJECT_REF(object)                                \
   do {                                                 \
@@ -315,62 +374,82 @@ typedef struct
 
 
 /**en Decrement the reference count of OBJECT if the count is greater
-    than 0.  In that case, if the count becomes 0, free OBJECT.  */
+      than 0.  In that case, if the count becomes 0, free OBJECT.  */
+/**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤è¤êÂ礭¤±¤ì¤Ð 1 ¸º¤é¤¹¡£¸º¤é¤·¤Æ 0 ¤Ë¤Ê¤ì¤Ð
+      OBJECT ¤ò²òÊü¤¹¤ë.  */
 
-#define M17N_OBJECT_UNREF(object)                              \
-  do {                                                         \
-    if (object)                                                        \
-      {                                                                \
-       if (((M17NObject *) (object))->ref_count_extended)      \
-         m17n_object_unref (object);                           \
-       else if (((M17NObject *) (object))->ref_count == 0)     \
-         break;                                                \
-       else if (((M17NObject *) (object))->ref_count > 1)      \
-         ((M17NObject *) (object))->ref_count--;               \
-       else                                                    \
-         {                                                     \
-           if (((M17NObject *) (object))->u.freer)             \
-             (((M17NObject *) (object))->u.freer) (object);    \
-           else                                                \
-             free (object);                                    \
-           (object) = NULL;                                    \
-         }                                                     \
-      }                                                                \
+#define M17N_OBJECT_UNREF(object)                                      \
+  do {                                                                 \
+    if (object)                                                                \
+      {                                                                        \
+       if (((M17NObject *) (object))->ref_count_extended)              \
+         m17n_object_unref (object);                                   \
+       else if (((M17NObject *) (object))->ref_count == 0)             \
+         break;                                                        \
+       else                                                            \
+         {                                                             \
+           ((M17NObject *) (object))->ref_count--;                     \
+           if (((M17NObject *) (object))->ref_count == 0)              \
+             {                                                         \
+               if (((M17NObject *) (object))->u.freer)                 \
+                 (((M17NObject *) (object))->u.freer) (object);        \
+               else                                                    \
+                 free (object);                                        \
+               (object) = NULL;                                        \
+             }                                                         \
+         }                                                             \
+      }                                                                        \
   } while (0)
 
+typedef struct _M17NObjectArray M17NObjectArray;
 
-typedef struct
+struct _M17NObjectArray
 {
+  char *name;
   int count;
   int size, inc, used;
   void **objects;
-} M17NObjectArray;
+  M17NObjectArray *next;
+};
+
+extern void mdebug__add_object_array (M17NObjectArray *array, char *name);
+
+#define M17N_OBJECT_ADD_ARRAY(array, name)     \
+  if (mdebug__flags[MDEBUG_FINI])              \
+    mdebug__add_object_array (&array, name);   \
+  else
 
 extern void mdebug__register_object (M17NObjectArray *array, void *object);
 
 #define M17N_OBJECT_REGISTER(array, object)    \
-  if (mdebug__flag & MDEBUG_FINI)              \
+  if (mdebug__flags[MDEBUG_FINI])              \
     mdebug__register_object (&array, object);  \
   else
 
 extern void mdebug__unregister_object (M17NObjectArray *array, void *object);
 
 #define M17N_OBJECT_UNREGISTER(array, object)  \
-  if (mdebug__flag & MDEBUG_FINI)              \
+  if (mdebug__flags[MDEBUG_FINI])              \
     mdebug__unregister_object (&array, object);        \
   else
 
-extern void mdebug__report_object (char *name, M17NObjectArray *array);
-
 \f
 
 struct MTextPlist;
 
+enum MTextCoverage
+  {
+    MTEXT_COVERAGE_ASCII,
+    MTEXT_COVERAGE_UNICODE,
+    MTEXT_COVERAGE_FULL
+  };
+
 struct MText
 {
   M17NObject control;
 
-  enum MTextFormat format;
+  unsigned format : 16;
+  unsigned coverage : 16;
 
   /**en Number of characters in the M-text */
   /**ja M-text Ãæ¤Îʸ»ú¿ô */
@@ -461,85 +540,90 @@ struct MText
 
 \f
 
-enum MDebugMaskBit
+enum MDebugFlag
   {
-    MDEBUG_INIT =      0x01,
-    MDEBUG_FINI =      0x02,
-    MDEBUG_CHARSET =   0x04,
-    MDEBUG_CODING =    0x08,
-    MDEBUG_DATABASE =  0x10,
-    MDEBUG_FONT =      0x0100,
-    MDEBUG_FONT_FLT =  0x0200,
-    MDEBUG_FONT_OTF =  0x0400,
-    MDEBUG_INPUT =     0x0800,
-    MDEBUG_MAX
+    MDEBUG_INIT,
+    MDEBUG_FINI,
+    MDEBUG_CHARSET,
+    MDEBUG_CODING,
+    MDEBUG_DATABASE,
+    MDEBUG_FONT,
+    MDEBUG_FLT,
+    MDEBUG_FONTSET,
+    MDEBUG_INPUT,
+    MDEBUG_ALL,
+    MDEBUG_MAX = MDEBUG_ALL
   };
 
-extern int mdebug__flag;
+extern int mdebug__flags[MDEBUG_MAX];
+extern FILE *mdebug__output;
 extern void mdebug__push_time ();
 extern void mdebug__pop_time ();
 extern void mdebug__print_time ();
 
-#define MDEBUG_PRINT(msg)              \
-  do {                                 \
-    if (mdebug__flag & mdebug_mask)    \
-      fprintf (stderr, (msg));         \
-  } while (0)
+#define MDEBUG_FLAG() mdebug__flags[mdebug_flag]
 
-#define MDEBUG_PRINT1(fmt, arg)                \
+#define MDEBUG_PRINT0(FPRINTF)         \
   do {                                 \
-    if (mdebug__flag & mdebug_mask)    \
-      fprintf (stderr, (fmt), (arg));  \
+    if (MDEBUG_FLAG ())                        \
+      {                                        \
+       FPRINTF;                        \
+       fflush (mdebug__output);        \
+      }                                        \
   } while (0)
 
-#define MDEBUG_PRINT2(fmt, arg1, arg2)         \
-  do {                                         \
-    if (mdebug__flag & mdebug_mask)            \
-      fprintf (stderr, (fmt), (arg1), (arg2)); \
-  } while (0)
+#define MDEBUG_PRINT(msg) \
+  MDEBUG_PRINT0 (fprintf (mdebug__output, "%s", (msg)))
 
-#define MDEBUG_PRINT3(fmt, arg1, arg2, arg3)           \
-  do {                                                 \
-    if (mdebug__flag & mdebug_mask)                    \
-      fprintf (stderr, (fmt), (arg1), (arg2), (arg3)); \
-  } while (0)
+#define MDEBUG_PRINT1(fmt, arg) \
+  MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg)))
 
-#define MDEBUG_PRINT4(fmt, arg1, arg2, arg3, arg4)             \
-  do {                                                         \
-    if (mdebug__flag & mdebug_mask)                            \
-      fprintf (stderr, (fmt), (arg1), (arg2), (arg3), (arg4)); \
-  } while (0)
+#define MDEBUG_PRINT2(fmt, arg1, arg2) \
+  MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2)))
 
-#define MDEBUG_PRINT5(fmt, arg1, arg2, arg3, arg4, arg5)               \
-  do {                                                                 \
-    if (mdebug__flag & mdebug_mask)                                    \
-      fprintf (stderr, (fmt), (arg1), (arg2), (arg3), (arg4), (arg5)); \
-  } while (0)
+#define MDEBUG_PRINT3(fmt, arg1, arg2, arg3) \
+  MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2), (arg3)))
 
+#define MDEBUG_PRINT4(fmt, arg1, arg2, arg3, arg4)     \
+  MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2), (arg3), (arg4)))
 
-#define MDEBUG_PUSH_TIME()             \
-  do {                                 \
-    if (mdebug__flag & mdebug_mask)    \
-      mdebug__push_time ();            \
+#define MDEBUG_PRINT5(fmt, arg1, arg2, arg3, arg4, arg5)       \
+  MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2), (arg3), (arg4), (arg5)))
+
+#define MDEBUG_DUMP(prefix, postfix, call)             \
+  do {                                                 \
+    if (MDEBUG_FLAG ())                                        \
+      {                                                        \
+       fprintf (mdebug__output, "%s", prefix);         \
+       call;                                           \
+       fprintf (mdebug__output, "%s", postfix);        \
+       fflush (mdebug__output);                        \
+      }                                                        \
   } while (0)
 
+#define MDEBUG_PUSH_TIME()     \
+  do {                         \
+    if (MDEBUG_FLAG ())                \
+      mdebug__push_time ();    \
+  } while (0)
 
-#define MDEBUG_POP_TIME()              \
-  do {                                 \
-    if (mdebug__flag & mdebug_mask)    \
-      mdebug__pop_time ();             \
+
+#define MDEBUG_POP_TIME()      \
+  do {                         \
+    if (MDEBUG_FLAG ())                \
+      mdebug__pop_time ();     \
   } while (0)
 
 
-#define MDEBUG_PRINT_TIME(tag, ARG_LIST)       \
-  do {                                         \
-    if (mdebug__flag & mdebug_mask)            \
-      {                                                \
-       fprintf (stderr, " [%s] ", tag);        \
-       mdebug__print_time ();                  \
-       fprintf ARG_LIST;                       \
-       fprintf (stderr, "\n");                 \
-      }                                                \
+#define MDEBUG_PRINT_TIME(tag, ARG_LIST)               \
+  do {                                                 \
+    if (MDEBUG_FLAG ())                                        \
+      {                                                        \
+       fprintf (mdebug__output, " [%s] ", tag);        \
+       mdebug__print_time ();                          \
+       fprintf ARG_LIST;                               \
+       fprintf (mdebug__output, "\n");                 \
+      }                                                        \
   } while (0)
 
 
@@ -550,10 +634,6 @@ extern void mdebug__print_time ();
    | (((c) & 0xFF00) << 8) | (((c) & 0xFF) << 24))
 
 
-extern void *(*mdatabase__finder) (MSymbol tag1, MSymbol tag2,
-                                  MSymbol tag3, MSymbol tag4);
-extern void *(*mdatabase__loader) (void *);
-
 /* Initialize/finalize function.  */
 
 extern int msymbol__init ();