/* Chrysalide - Outil d'analyse de fichiers binaires
 * tmgt.c - compléments utiles à la mise en place d'arborescences
 *
 * Copyright (C) 2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 "tmgt.h"
#include 
#include 
#include "../common/extstr.h"
/******************************************************************************
*                                                                             *
*  Paramètres  : entry  = zone de texte avec un nouveau filtre d'affichage.   *
*                filter = expression régulière de filtrage à constituer. [OUT]*
*                                                                             *
*  Description : Met à jour un filtre selon un contenu recherché.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
void update_regex_on_search_entry_changed(GtkSearchEntry *entry, regex_t **filter)
{
    const gchar *text;                      /* Texte de l'utilisateur      */
    int ret;                                /* Bilan de mise en place      */
    GdkRGBA error;                          /* Couleur d'erreur            */
    if (*filter != NULL)
    {
        regfree(*filter);
        free(*filter);
        *filter = NULL;
    }
    text = gtk_entry_get_text(GTK_ENTRY(entry));
    if (strlen(text) > 0)
    {
        *filter = (regex_t *)calloc(1, sizeof(regex_t));
        ret = regcomp(*filter, text, REG_EXTENDED | REG_ICASE);
        if (ret != 0)
        {
            free(*filter);
            *filter = NULL;
            error.red = 1.0;
            error.green = 0.0;
            error.blue = 0.0;
            error.alpha = 1.0;
            gtk_widget_override_color(GTK_WIDGET(entry), GTK_STATE_NORMAL, &error);
            return;
        }
    }
    gtk_widget_override_color(GTK_WIDGET(entry), GTK_STATE_NORMAL, NULL);
}
/******************************************************************************
*                                                                             *
*  Paramètres  : filter  = expression régulière servant de filtre.            *
*                content = contenu textuel à analyser.                        *
*                match   = zone pointant vers une correspondance. [OUT]       *
*                                                                             *
*  Description : Détermine si un contenu correspond à un filtre donné.        *
*                                                                             *
*  Retour      : true si le contenu est qualifié pour un affichage, ou false. *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
bool is_content_matching(const regex_t *filter, const char *content, regmatch_t *match)
{
    bool result;                            /* Bilan à retourner           */
    int ret;                                /* Bilan du filtrage           */
    memset( match, 0, sizeof(regmatch_t));
    if (filter == NULL)
        result = true;
    else
    {
        ret = regexec(filter, content, 1, match, 0);
        result = (ret != REG_NOMATCH);
    }
    return result;
}
/******************************************************************************
*                                                                             *
*  Paramètres  : raw   = bribe de texte à traiter.                            *
*                match = portion de texte à mettre en évidence.               *
*                start = position du texte brute dans l'étiquette complète.   *
*                                                                             *
*  Description : Met en évidence le texte recherché en cas de correspondance. *
*                                                                             *
*  Retour      : Texte final destiné à être inséré, à libérer après usage.    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
char *build_highlighted_name(const char *raw, const regmatch_t *match, size_t start)
{
    char *result;                           /* Construction à retourner    */
    size_t len;                             /* Taille du texte d'entrée    */
    regmatch_t selection;                   /* Sélection relative au texte */
    char *valid;                            /* Retouche partielle          */
    len = strlen(raw);
    /* Si aucune sélection ou texte hors champ... */
    if ((match->rm_eo - match->rm_so) == 0 || (start + len) <= match->rm_so || match->rm_eo < start)
    {
        result = strdup(raw);
        result = strrpl(result, "<", "<");
    }
    /* Sinon, il y a forcément correspondance quelque part ! */
    else
    {
        /* Adaptations */
        if (match->rm_so < start)
            selection.rm_so = 0;
        else
            selection.rm_so = match->rm_so - start;
        selection.rm_eo = match->rm_eo - start;
        if (selection.rm_eo > len)
            selection.rm_eo = len;
        /* Impressions */
        if (selection.rm_so > 0)
        {
            result = strndup(raw, selection.rm_so);
            result = strrpl(result, "<", "<");
        }
        else
            result = NULL;
        result = stradd(result, "");
        valid = strndup(&raw[selection.rm_so], selection.rm_eo - selection.rm_so);
        valid = strrpl(valid, "<", "<");
        result = stradd(result, valid);
        free(valid);
        result = stradd(result, "");
        valid = strdup(&raw[selection.rm_eo]);
        valid = strrpl(valid, "<", "<");
        result = stradd(result, valid);
        free(valid);
    }
    return result;
}