Kouhei Sutou
kous****@users*****
2006年 11月 27日 (月) 15:46:41 JST
Index: tomoe/lib/tomoe-char.c diff -u tomoe/lib/tomoe-char.c:1.48 tomoe/lib/tomoe-char.c:1.49 --- tomoe/lib/tomoe-char.c:1.48 Mon Nov 27 14:24:26 2006 +++ tomoe/lib/tomoe-char.c Mon Nov 27 15:46:41 2006 @@ -18,7 +18,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-char.c,v 1.48 2006/11/27 05:24:26 ikezoe Exp $ + * $Id: tomoe-char.c,v 1.49 2006/11/27 06:46:41 kous Exp $ */ #include <stdlib.h> @@ -280,6 +280,110 @@ g_hash_table_foreach (priv->meta, func, user_data); } + +static void +tomoe_char_to_xml_char_code (TomoeChar *chr, TomoeCharPrivate *priv, + GString *output) +{ + gchar *code_point; + + if (!priv->char_code) return; + + code_point = g_markup_printf_escaped (" <code-point>%s</code-point>\n", + priv->char_code); + g_string_append (output, code_point); + g_free (code_point); +} + +static void +tomoe_char_to_xml_readings (TomoeChar *chr, TomoeCharPrivate *priv, + GString *output) +{ + GList *node; + + if (!priv->readings) return; + + g_string_append (output, " <readings>\n"); + for (node = priv->readings; node; node = g_list_next (node)) { + TomoeReading *reading = node->data; + gchar *xml; + + if (!TOMOE_IS_READING (reading)) continue; + + xml = g_markup_printf_escaped (" <reading>%s</reading>\n", + tomoe_reading_get_reading (reading)); + g_string_append (output, xml); + g_free (xml); + } + g_string_append (output, " </readings>\n"); +} + +static void +tomoe_char_to_xml_writing (TomoeChar *chr, TomoeCharPrivate *priv, + GString *output) +{ + gchar *xml; + + if (!priv->writing) return; + + xml = tomoe_writing_to_xml (priv->writing); + + if (xml && xml[0] != '\0') { + g_string_append (output, xml); + g_free (xml); + } +} + + +static void +tomoe_char_to_xml_meta_datum (gpointer key, gpointer value, gpointer user_data) +{ + GString *output = user_data; + gchar *meta_key = key; + gchar *meta_value = value; + gchar *result; + + result = g_markup_printf_escaped (" <%s>%s</%s>\n", + meta_key, meta_value, meta_key); + g_string_append (output, result); + g_free (result); +} + +static void +tomoe_char_to_xml_meta (TomoeChar *chr, TomoeCharPrivate *priv, GString *output) +{ + if (!tomoe_char_has_meta_data (chr)) return; + + g_string_append (output, " <meta>\n"); + tomoe_char_meta_data_foreach (chr, tomoe_char_to_xml_meta_datum, output); + g_string_append (output, " </meta>\n"); +} + +gchar * +tomoe_char_to_xml (TomoeChar* chr) +{ + TomoeCharPrivate *priv; + GString *output; + + g_return_val_if_fail (TOMOE_IS_CHAR (chr), NULL); + + priv = TOMOE_CHAR_GET_PRIVATE (chr); + output = g_string_new (""); + + tomoe_char_to_xml_char_code (chr, priv, output); + tomoe_char_to_xml_readings (chr, priv, output); + tomoe_char_to_xml_writing (chr, priv, output); + tomoe_char_to_xml_meta (chr, priv, output); + + if (output->len > 0) { + g_string_prepend (output, " <character>\n"); + g_string_append (output, " </character>\n"); + } + + return g_string_free (output, FALSE); +} + + /* vi:ts=4:nowrap:ai:expandtab */ Index: tomoe/lib/tomoe-char.h diff -u tomoe/lib/tomoe-char.h:1.44 tomoe/lib/tomoe-char.h:1.45 --- tomoe/lib/tomoe-char.h:1.44 Sun Nov 26 16:30:37 2006 +++ tomoe/lib/tomoe-char.h Mon Nov 27 15:46:41 2006 @@ -18,7 +18,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-char.h,v 1.44 2006/11/26 07:30:37 kous Exp $ + * $Id: tomoe-char.h,v 1.45 2006/11/27 06:46:41 kous Exp $ */ /** @file tomoe-char.h @@ -98,6 +98,8 @@ GHFunc func, gpointer user_data); +gchar *tomoe_char_to_xml (TomoeChar* chr); + G_END_DECLS #endif /* __TOMOE_CHAR_H__ */ Index: tomoe/lib/tomoe-dict.c diff -u tomoe/lib/tomoe-dict.c:1.98 tomoe/lib/tomoe-dict.c:1.99 --- tomoe/lib/tomoe-dict.c:1.98 Mon Nov 27 15:24:26 2006 +++ tomoe/lib/tomoe-dict.c Mon Nov 27 15:46:41 2006 @@ -21,7 +21,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.c,v 1.98 2006/11/27 06:24:26 makeinu Exp $ + * $Id: tomoe-dict.c,v 1.99 2006/11/27 06:46:41 kous Exp $ */ #include <stdio.h> @@ -798,108 +798,6 @@ return TRUE; } - - -static gboolean -_write_readings (TomoeChar *chr, FILE *f) -{ - GList *node = (GList*) tomoe_char_get_readings (chr); - const gchar *head = " <readings>\n"; - const gchar *foot = " </readings>\n"; - - if (fwrite (head, strlen (head), 1, f) < 1) return FALSE; - - for (; node; node = g_list_next (node)) { - TomoeReading *reading = node->data; - gchar *str; - - if (!TOMOE_IS_READING (reading)) continue; - - str = g_markup_printf_escaped (" <reading>%s</reading>\n", - tomoe_reading_get_reading (reading)); - if (fwrite (str, strlen (str), 1, f) < 1) { - g_free (str); - return FALSE; - } - g_free (str); - } - - if (fwrite (foot, strlen (foot), 1, f) < 1) return FALSE; - - return TRUE; -} - -static void -_write_meta_datum (gpointer key, gpointer value, gpointer user_data) -{ - FILE *f = user_data; - gchar *str; - - str = g_markup_printf_escaped (" <%s>%s</%s>\n", - (gchar*)key, (gchar*)value, (gchar*)key); - fwrite (str, strlen (str), 1, f); - g_free (str); -} - -static gboolean -_write_meta_data (TomoeChar *chr, FILE *f) -{ - const gchar *tag1 = " <meta>\n"; - const gchar *tag2 = " </meta>\n"; - - if (fwrite (tag1, strlen (tag1), 1, f) < 1) return FALSE; - tomoe_char_meta_data_foreach (chr, _write_meta_datum, f); - if (fwrite (tag2, strlen (tag2), 1, f) < 1) return FALSE; - - return TRUE; -} - -static gboolean -_write_character (TomoeChar *chr, FILE *f) -{ - gchar *head; - const gchar *foot = " </character>\n"; - gchar *output; - - g_return_val_if_fail (TOMOE_IS_CHAR (chr), FALSE); - - /* open character element */ - head = g_markup_printf_escaped (" <character>\n" - " <code-point>%s</code-point>\n", - tomoe_char_get_code (chr)); - if (fwrite (head, strlen (head), 1, f) < 1) { - g_free (head); - return FALSE; - } else { - g_free (head); - } - - /* reading */ - if (tomoe_char_get_readings (chr)) - if (!_write_readings (chr, f)) return FALSE; - - /* writing */ - if (tomoe_char_get_writing (chr)) { - gboolean failed; - - output = tomoe_writing_to_xml (tomoe_char_get_writing (chr)); - if (!output) return FALSE; - - failed = fwrite (output, strlen (output), 1, f) < 1; - g_free (output); - if (failed) return FALSE; - } - - /* meta */ - if (tomoe_char_has_meta_data (chr)) - if (!_write_meta_data (chr, f)) return FALSE; - - /* close character element */ - if (fwrite (foot, strlen (foot), 1, f) < 1) return FALSE; - - return TRUE; -} - static void tomoe_dict_save_xml (TomoeDict *dict) { @@ -933,8 +831,16 @@ /* write each characters */ for (i = 0; i < priv->letters->len; i++) { + gchar *xml; + gboolean failed; TomoeChar* chr = (TomoeChar*)g_ptr_array_index (priv->letters, i); - if (!_write_character (chr, f)) goto ERROR; + + xml = tomoe_char_to_xml (chr); + if (!xml) goto ERROR; + + failed = fwrite (xml, strlen (xml), 1, f) < 1; + g_free (xml); + if (failed) goto ERROR; } /* close root element */ Index: tomoe/lib/tomoe-writing.c diff -u tomoe/lib/tomoe-writing.c:1.9 tomoe/lib/tomoe-writing.c:1.10 --- tomoe/lib/tomoe-writing.c:1.9 Mon Nov 27 13:23:47 2006 +++ tomoe/lib/tomoe-writing.c Mon Nov 27 15:46:41 2006 @@ -18,7 +18,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-writing.c,v 1.9 2006/11/27 04:23:47 kous Exp $ + * $Id: tomoe-writing.c,v 1.10 2006/11/27 06:46:41 kous Exp $ */ #include <stdlib.h> @@ -183,7 +183,7 @@ g_return_val_if_fail (TOMOE_IS_WRITING (writing), NULL); priv = TOMOE_WRITING_GET_PRIVATE(writing); - if (!priv->stroke_first) return NULL; + if (!priv->stroke_first) return g_strdup (""); output = g_string_new (" <strokes>\n"); for (stroke_list = priv->stroke_first;