/* OpenIDA - Outil d'analyse de fichiers binaires
 * text.c - raccord avec les opérandes de chaînes de caractères
 *
 * Copyright (C) 2012 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  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 <http://www.gnu.org/licenses/>.
 */


#include "text.h"


#include <string.h>


#include "../expression-int.h"



/* Définition d'une expression de valeur immédiate (instance) */
struct _GStrExpression
{
    GDecExpression parent;                  /* A laisser en premier        */

    char *value;                            /* Chaîne représentée          */
    size_t len;                             /* Taille du texte             */

};


/* Définition d'une expression de valeur immédiate (classe) */
struct _GStrExpressionClass
{
    GDecExpressionClass parent;             /* A laisser en premier        */

};



/* Initialise la classe des expressions de valeur immédiate. */
static void g_str_expression_class_init(GStrExpressionClass *);

/* Initialise une instance d'expression de valeur immédiate. */
static void g_str_expression_init(GStrExpression *);

/* Imprime pour l'écran un version humaine d'une expression. */
static GBufferLine *g_str_expression_print(const GStrExpression *, GCodeBuffer *, GBufferLine *, GLangOutput *);



/* Indique le type défini pour une expression de valeur immédiate. */
G_DEFINE_TYPE(GStrExpression, g_str_expression, G_TYPE_DEC_EXPRESSION);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des expressions de valeur immédiate.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_str_expression_class_init(GStrExpressionClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : expr = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise une instance d'expression de valeur immédiate.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_str_expression_init(GStrExpression *expr)
{
    GDecInstruction *instr;                 /* Autre version de l'objet    */

    instr = G_DEC_INSTRUCTION(expr);

    instr->print = (dec_instr_print_fc)g_str_expression_print;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : value = chaîne de caractères à représenter.                  *
*                                                                             *
*  Description : Construit une expression à partir d'une valeur immédiate.    *
*                                                                             *
*  Retour      : Expression mise en place.                                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDecInstruction *g_str_expression_new(const char *value)
{
    GStrExpression *result;                 /* Expression à retourner      */

    result = g_object_new(G_TYPE_STR_EXPRESSION, NULL);

    result->value = strdup(value);
    result->len = strlen(value);

    return G_DEC_INSTRUCTION(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : expr   = expression à transcrire en version humaine.         *
*                buffer = tampon où doit se réaliser l'insertion.             *
*                line   = ligne d'impression prête à emploi ou NULL.          *
*                output = langage de programmation de sortie.                 *
*                                                                             *
*  Description : Imprime pour l'écran un version humaine d'une expression.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GBufferLine *g_str_expression_print(const GStrExpression *expr, GCodeBuffer *buffer, GBufferLine *line, GLangOutput *output)
{
    g_buffer_line_insert_text(line, BLC_ASSEMBLY_HEAD, "\"", 1, RTT_STRING);
    g_buffer_line_insert_text(line, BLC_ASSEMBLY_HEAD, expr->value, expr->len, RTT_STRING);
    g_buffer_line_insert_text(line, BLC_ASSEMBLY_HEAD, "\"", 1, RTT_STRING);

    return line;

}