/* Chrysalide - Outil d'analyse de fichiers binaires
 * comment.c - gestion des commentaires dans du texte
 *
 * Copyright (C) 2014 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA 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 Foobar.  If not, see .
 */
#include "comment.h"
#include 
#include 
#include "../item-int.h"
#include "../misc/rlestr.h"
/* Commentaire à placer dans du texte quelconque (instance) */
struct _GDbComment
{
    GDbItem parent;                         /* A laisser en premier        */
    vmpa2t *addr;                           /* Adresse du commentaire      */
    rle_string text;                        /* Contenu du commentaire      */
};
/* Commentaire à placer dans du texte quelconque (classe) */
struct _GDbCommentClass
{
    GDbItemClass parent;                    /* A laisser en premier        */
};
/* Initialise la classe des commentaires dans une zone de texte. */
static void g_db_comment_class_init(GDbCommentClass *);
/* Initialise un commentaire dans une zone de texte. */
static void g_db_comment_init(GDbComment *);
/* Supprime toutes les références externes. */
static void g_db_comment_dispose(GDbComment *);
/* Procède à la libération totale de la mémoire. */
static void g_db_comment_finalize(GDbComment *);
/* Effectue la comparaison entre deux commentaires enregistrés. */
static gint g_db_comment_cmp(GDbComment *, GDbComment *);
/* Importe la définition d'un commentaire dans un flux réseau. */
static bool g_db_comment_recv_from_fd(GDbComment *, int, int);
/* Exporte la définition d'un commentaire dans un flux réseau. */
static bool g_db_comment_send_to_fd(const GDbComment *, int, int);
/******************************************************************************
*                                                                             *
*  Paramètres  : db = accès à la base de données.                             *
*                                                                             *
*  Description : Crée la table des commentaires dans une base de données.     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
bool create_comment_db_table(sqlite3 *db)
{
    char *sql;                              /* Requête à exécuter          */
    int ret;                                /* Bilan de la création        */
    char *msg;                              /* Message d'erreur            */
    sql = "CREATE TABLE Comments ("            \
             "id INT PRIMARY KEY NOT NULL, "    \
             "user TEXT NOT NULL, "             \
             "created INT NOT NULL, "           \
             "address INT NOT NULL, "           \
             "comment TEXT"                     \
          ");";
    ret = sqlite3_exec(db, sql, NULL, NULL, &msg);
    if (ret != SQLITE_OK)
    {
        fprintf(stderr, "sqlite3_exec(): %s\n", msg);
        sqlite3_free(msg);
    }
    return (ret == SQLITE_OK);
}
/* Indique le type défini pour un commentaire à l'intérieur d'une zone de texte. */
G_DEFINE_TYPE(GDbComment, g_db_comment, G_TYPE_DB_ITEM);
/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des commentaires dans une zone de texte.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static void g_db_comment_class_init(GDbCommentClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GDbItemClass *item;                     /* Encore une autre vision...  */
    object = G_OBJECT_CLASS(klass);
    object->dispose = (GObjectFinalizeFunc/* ! */)g_db_comment_dispose;
    object->finalize = (GObjectFinalizeFunc)g_db_comment_finalize;
    item = G_DB_ITEM_CLASS(klass);
    item->cmp = (GCompareFunc)g_db_comment_cmp;
    item->recv = (recv_db_item_fc)g_db_comment_recv_from_fd;
    item->send = (send_db_item_fc)g_db_comment_send_to_fd;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = instance à initialiser.                            *
*                                                                             *
*  Description : Initialise un commentaire dans une zone de texte.            *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static void g_db_comment_init(GDbComment *comment)
{
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = instance d'objet GLib à traiter.                   *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static void g_db_comment_dispose(GDbComment *comment)
{
    G_OBJECT_CLASS(g_db_comment_parent_class)->dispose(G_OBJECT(comment));
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = instance d'objet GLib à traiter.                   *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static void g_db_comment_finalize(GDbComment *comment)
{
    delete_vmpa(comment->addr);
    exit_rle_string(comment->text);
    G_OBJECT_CLASS(g_db_comment_parent_class)->finalize(G_OBJECT(comment));
}
/******************************************************************************
*                                                                             *
*  Paramètres  : addr        = adresse inamovible localisant une position.    *
*                text        = commentaire construit ou NULL.                 *
*                is_volatile = état du besoin en sauvegarde.                  *
*                                                                             *
*  Description : Crée une définition de commentaire dans une zone de texte.   *
*                                                                             *
*  Retour      : Commentaire mis en place ou NULL en cas d'erreur.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
GDbComment *g_db_comment_new(const vmpa2t *addr, const char *text, bool is_volatile)
{
    GDbComment *result;                    /* Instance à retourner        */
    result = g_object_new(G_TYPE_DB_COMMENT, NULL);
    result->addr = dup_vmpa(addr);
    g_db_comment_set_text(result, text);
    g_db_item_set_volatile(G_DB_ITEM(result), is_volatile);
    return result;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : a = premier élément à analyser.                              *
*                b = second élément à analyser.                               *
*                                                                             *
*  Description : Effectue la comparaison entre deux commentaires enregistrés. *
*                                                                             *
*  Retour      : Bilan de la comparaison : -1, 0 ou 1.                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static gint g_db_comment_cmp(GDbComment *a, GDbComment *b)
{
    gint result;                            /* Bilan de la comparaison     */
    result = cmp_vmpa_by_phy(a->addr, b->addr);
    if (result == 0)
        result = cmp_rle_string(&a->text, &b->text);
    return 0;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = commentaire avec informations sont à charger. [OUT]*
*                fd      = flux ouvert en lecture pour l'importation.         *
*                flags   = éventuelles options d'envoi supplémentaires.       *
*                                                                             *
*  Description : Importe la définition d'un commentaire dans un flux réseau.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static bool g_db_comment_recv_from_fd(GDbComment *comment, int fd, int flags)
{
    bool status;                            /* Bilan d'opération initiale  */
    status = G_DB_ITEM_CLASS(g_db_comment_parent_class)->recv(G_DB_ITEM(comment), fd, flags);
    if (!status) return false;
    if (!recv_vmpa(comment->addr, fd, 0))
        return false;
    if (!recv_rle_string(&comment->text, fd, 0))
        return false;
    return true;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = informations à sauvegarder.                        *
*                fd      = flux ouvert en écriture pour l'exportation.        *
*                flags   = éventuelles options d'envoi supplémentaires.       *
*                                                                             *
*  Description : Exporte la définition d'un commentaire dans un flux réseau.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
static bool g_db_comment_send_to_fd(const GDbComment *comment, int fd, int flags)
{
    bool status;                            /* Bilan d'opération initiale  */
    status = G_DB_ITEM_CLASS(g_db_comment_parent_class)->send(G_DB_ITEM(comment), fd, MSG_MORE | flags);
    if (!status) return false;
    if (!send_vmpa(comment->addr, fd, MSG_MORE | flags))
        return false;
    if (!send_rle_string(&comment->text, fd, flags))
        return false;
    return true;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = informations à consulter.                          *
*                                                                             *
*  Description : Fournit l'adresse associée à un commentaire.                 *
*                                                                             *
*  Retour      : Adresse mémoire.                                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
const vmpa2t *g_db_comment_get_address(GDbComment *comment)
{
    return comment->addr;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = informations à consulter.                          *
*                                                                             *
*  Description : Fournit le commentaire associé à un commentaire.             *
*                                                                             *
*  Retour      : Commentaire existant ou NULL.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
const char *g_db_comment_get_text(const GDbComment *comment)
{
    return get_rle_string(&comment->text);
}
/******************************************************************************
*                                                                             *
*  Paramètres  : comment = informations à consulter.                          *
*                text    = commentaire construit ou NULL.                     *
*                                                                             *
*  Description : Définit le commentaire associé à un commentaire.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
void g_db_comment_set_text(GDbComment *comment, const char *text)
{
    set_rle_string(&comment->text, text);
}