summaryrefslogtreecommitdiff
path: root/src/gtkext/tmgt.c
blob: ce6f9a80f061865a6f7a5ef78a604e3065cfde3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

/* 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 <http://www.gnu.org/licenses/>.
 */


#include "tmgt.h"


#include <malloc.h>
#include <string.h>


#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, "<", "&lt;");
    }

    /* 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, "<", "&lt;");
        }
        else
            result = NULL;

        result = stradd(result, "<b>");

        valid = strndup(&raw[selection.rm_so], selection.rm_eo - selection.rm_so);
        valid = strrpl(valid, "<", "&lt;");

        result = stradd(result, valid);

        free(valid);

        result = stradd(result, "</b>");

        valid = strdup(&raw[selection.rm_eo]);
        valid = strrpl(valid, "<", "&lt;");

        result = stradd(result, valid);

        free(valid);

    }

    return result;

}