/* vim:set et sts=4 sw=4 fileencoding=utf-8: */
/* ibus-hangul - The Hangul Engine For IBus
 * Copyright (C) 2008-2009 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2009-2011 Choe Hwanjin <choe.hwanjin@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>

#include "i18n.h"
#include "engine.h"
#include "utils.h"

const gchar * 
replace (const gchar *str, const gchar *olds, const gchar *news) {
    gchar *result, *str_recursive;
    size_t i, count = 0;
    size_t oldlen = strlen(olds);
    size_t newlen = strlen(news);

    if (oldlen < 1) {
        return str;
    }

    if (newlen != oldlen) {
        for (i = 0; str[i] != '\0';) {
            if (memcmp(&str[i], olds, oldlen) == 0) {
                count++;
                i += oldlen;
            } else {
                i++;
            }
        }
    } else {
        i = strlen(str);
    }

    result = (gchar *) malloc(i + 1 + count * (newlen - oldlen));
    if (result == NULL) {
        return NULL;
    }

    str_recursive = result;
    while (*str) {
        if (memcmp(str, olds, oldlen) == 0) {
            memcpy(str_recursive, news, newlen);
            str_recursive += newlen;
            str += oldlen;
        } else {
            *str_recursive++ = *str++;
        }
    }
    *str_recursive = '\0';

    return result;
}

gboolean
compare_engine_xml (XMLNode *node_home, 
                                        XMLNode *node_system)
{
    const char *version_home = NULL;
    const char *version_system = NULL;
    const char *longname_home = NULL;
    const char *longname_system = NULL;
    
    if (node_home && g_strcmp0 (node_home->name, "engines") == 0) {
        XMLNode *engine_node = node_home->sub_nodes->data;
        GList *p;

        if (g_strcmp0 (engine_node->name, "engine") == 0) {
            for (p = engine_node->sub_nodes; p != NULL; p = p->next) {
                XMLNode *sub_node = (XMLNode *) p->data;

                if (g_strcmp0 (sub_node->name, "version") == 0) {
                    version_home = sub_node->text;
                } else if (g_strcmp0 (sub_node->name, "longname") == 0) {
                    longname_home = sub_node->text;
                }
            }
        } else {
            g_warning ("<engines> element contains invalid element <%s>",
                            engine_node->name);
        }
    } else {
        g_warning ("failed to parse %s", "engines");
    }

    if (node_system && g_strcmp0 (node_system->name, "engines") == 0) {
        XMLNode *engine_node = node_system->sub_nodes->data;
        GList *p;

        if (g_strcmp0 (engine_node->name, "engine") == 0) {
            for (p = engine_node->sub_nodes; p != NULL; p = p->next) {
                XMLNode *sub_node = (XMLNode *) p->data;

                if (g_strcmp0 (sub_node->name, "version") == 0) {
                    version_system = sub_node->text;
                } else if (g_strcmp0 (sub_node->name, "longname") == 0) {
                    longname_system = sub_node->text;
                }
            }
        } else {
            g_warning ("<engines> element contains invalid element <%s>",
                            engine_node->name);
        }
    } else {
        g_warning ("failed to parse %s", "engines");
    }

    if ( (g_strcmp0 (version_home, version_system) != 0) ||
                 (g_strcmp0 (longname_home, longname_system) != 0) ) {
        return FALSE;
    }
    return TRUE;
}
