summaryrefslogtreecommitdiffstats
path: root/src/filter.c
blob: 7c99a0ebc2771cddb3281f58dd87aea418c8b91d (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com>             *
 * All rights reserved.                                                        *
 *                                                                             *
 * This file is a part of SC-IM                                                *
 *                                                                             *
 * SC-IM is a spreadsheet program that is based on SC. The original authors    *
 * of SC are James Gosling and Mark Weiser, and mods were later added by       *
 * Chuck Martin.                                                               *
 *                                                                             *
 * Redistribution and use in source and binary forms, with or without          *
 * modification, are permitted provided that the following conditions are met: *
 * 1. Redistributions of source code must retain the above copyright           *
 *    notice, this list of conditions and the following disclaimer.            *
 * 2. Redistributions in binary form must reproduce the above copyright        *
 *    notice, this list of conditions and the following disclaimer in the      *
 *    documentation and/or other materials provided with the distribution.     *
 * 3. All advertising materials mentioning features or use of this software    *
 *    must display the following acknowledgement:                              *
 *    This product includes software developed by Andrés Martinelli            *
 *    <andmarti@gmail.com>.                                                    *
 * 4. Neither the name of the Andrés Martinelli nor the                        *
 *   names of other contributors may be used to endorse or promote products    *
 *   derived from this software without specific prior written permission.     *
 *                                                                             *
 * THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY            *
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      *
 * DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY           *
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       *
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
 *******************************************************************************/

/**
 * \file filter.c
 * \author Andrés Martinelli <andmarti@gmail.com>
 * \date 2017-07-18
 * \brief TODO Write a tbrief file description.
 */

#include <string.h>
#include <stdio.h>
#include <ctype.h>             // for isalpha toupper
#include <stdlib.h>

#include "macros.h"
#include "tui.h"
#include "conf.h"
#include "xmalloc.h"
#include "filter.h"
#include "math.h"
#include "utils/string.h"
#include "sc.h"
#include "cmds.h"

static int howmany = 0;      /**< how many filters were definedi */
static int active = 0;       /**< indicates if those filters are applied or not */
static int * results = NULL; /**< this keeps the results of the applied filters */
static struct filter_item * filters = NULL;

/**
 * \brief Add a filter to filters structure
 *
 * \param[in] criteria
 *
 * \return none
 */

void add_filter(char * criteria) {
    int cp = 0;
    char c;

    while (criteria[cp]) {
        int pos = exists_freed_filter(); // we check if there exists a freed filter
        if (pos == -1) {            // if not we alloc a new one
            filters = (struct filter_item *) scxrealloc((char *) filters, (howmany++ + 1) * (sizeof(struct filter_item)));
            pos = howmany-1;
        }

        filters[pos].eval = (char *) scxmalloc(sizeof(char) * strlen(criteria) + 1);
        filters[pos].eval[0] = '\0';

        while (criteria[cp] && criteria[cp] != ';' && criteria[cp] != '\n') {
            c = criteria[cp];
            if (c == '"') { cp++; continue; }
            if (criteria[cp++] == '\'') c ='"';
            sprintf(filters[pos].eval + strlen(filters[pos].eval), "%c", c);
        }

        if (criteria[cp] == ';') cp++;
    }
    return;
}

/**
 * \brief Apply filters to a range
 *
 * \param[in] left
 * \param[in] right
 *
 * \return none
 */

void enable_filters(struct ent * left, struct ent * right) {
    int minr = left->row < right->row ? left->row : right->row;
    int maxr = left->row > right->row ? left->row : right->row;
    int i, r, c = 0;
    wchar_t cadena [BUFFERSIZE] = L"";
    wchar_t aux [BUFFERSIZE] = L"";
    results = (int *) scxrealloc((char *) results, (maxr - minr + 3) * sizeof(int));
    results[0] = minr; // keep in first position the first row of the range!
    results[1] = maxr; // keep in second position the last row of the range!
    if (filters == NULL) {
        sc_error("There are no filters defined");
        return;
    }
    active = 1;

    for (r = minr; r <= maxr; r++) {
        results[r-minr+2] = 0; // show row by default (0 = NOT HIDDEN)
        for (i = 0; i < howmany; i++, c=0) {
            cadena[0]=L'\0';
            if (filters[i].eval == NULL) continue;
            while (filters[i].eval[c] != '\0') {

                if (filters[i].eval[c] == '#' || filters[i].eval[c] == '$') {
                    if (isalpha(toupper(filters[i].eval[++c])))
                        swprintf(cadena + wcslen(cadena), BUFFERSIZE, L"%c", filters[i].eval[c]);
                    if (isalpha(toupper(filters[i].eval[++c])))
                        swprintf(cadena + wcslen(cadena), BUFFERSIZE, L"%c", filters[i].eval[c]);
                    swprintf(cadena + wcslen(cadena), BUFFERSIZE, L"%d", r);
                    continue;
                } else
                    swprintf(cadena + wcslen(cadena), BUFFERSIZE, L"%c", filters[i].eval[c]);
                c++;
            }

            swprintf(aux, BUFFERSIZE, L"eval %ls", cadena);
            send_to_interp(aux);
            if ( (! seval_result && str_in_str(filters[i].eval, "seval") != -1) || ! eval_result) {
                results[r-minr+2] = 1; // this row does not eval to expression. we hide it. (1 = HIDDEN)!
                i = howmany;
            }
            if (seval_result != NULL) free(seval_result);
        }
    }

    // Hide rows that don't match with filters
    for (r = results[0]; r <= results[1]; r++) {
        row_hidden[r] = results[r-results[0]+2];
    }
    return;
}

/**
 * \brief Disable any applied filters
 *
 * \return none
 */

void disable_filters() {
    if (results == NULL) {
        sc_error("There are no filters active");
        return;
    }
    // Hide rows that don't match with filters
    int r;
    for (r=results[0]; r<=results[1]; r++) {
        row_hidden[r] = 0;
    }
    active = 0;
    return;
}

// Show details of each filter
/**
 * \brief Show details of each filter
 *
 * \return none
 */

void show_filters() {
    if (filters == NULL) {
        sc_error("There are no filters defined");
        return;
    }

    int i, size = 0;
    char init_msg[BUFFERSIZE];
    sprintf(init_msg, "Filters status: %s\nFilters:\n", active == 1 ? "ON" : "OFF");

    size += sizeof(init_msg);
    for (i=0; i < howmany; i++)
        size += sizeof(filters[i].eval) + 4 + floor(log10(howmany));

    char valores[ size + howmany ];
    valores[0]='\0';

    strcpy(valores, init_msg);
    for (i=0; i < howmany; i++)
        if (filters[i].eval != NULL) sprintf(valores + strlen(valores), "%d + %s\n", i, filters[i].eval);

    ui_show_text(valores);
    return;
}

/**
 * \brief Free memory of entire filters structure
 *
 * \return none
 */

void free_filters() {
    if (filters == NULL) return;
    int i;
    for (i=0; i < howmany; i++)
        if (filters[i].eval != NULL) scxfree((char *) filters[i].eval);
    scxfree((char *) filters);
    filters = NULL;
    return;
}

/**
 * \brief Remove a filter, freeing its memory
 *
 * \param[in] id
 *
 * \return none
 */

void del_filter(int id) {
    if (filters == NULL || id < 0 || id > howmany) {
        sc_error("Cannot delete the filter");
        return;
    }
    if (filters[id].eval != NULL) {
        scxfree((char *) filters[id].eval);
        filters[id].eval = NULL;
    }
    return;
}

/**
 * \brief Check if a filter was deleted
 *
 * \details This function checks if a filter was deleted, so there would
 * be room in filters structure for a new filter and preventing
 * an unnecessary realloc.
 *
 * \return how many filters exist; -1 otherwise
 */

int exists_freed_filter() {
    if (filters == NULL) return -1;
    int i;
    for (i=0; i < howmany;