summaryrefslogtreecommitdiff
path: root/src/block-parser.c
blob: cfe90471124d047a050d27f3541a897a1832c788 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*   Cataract - Static web photo gallery generator
 *   Copyright (C) 2009 Tomas Bzatek <tbzatek@users.sourceforge.net>
 *
 *   This program 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 2
 *   of the License, or any later version.
 *
 *   This program 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 this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>

#include <glib.h>

#include "block-parser.h"
#include "gallery-utils.h"
#include "replace-table.h"



#define BUFFER_SIZE  65536    /*  line cannot be longer than this  */


struct BlockParser {
  GHashTable *table;
  GQueue *active_tree;
  gchar *current_line;
  GHashTable *conditionals;
  guint ignore_level;
};

typedef struct {
  gchar *replace_key;
  gchar *data;
  gboolean used;
  gboolean finished;

  gboolean is_conditional;
  gchar *conditional_key;
  gboolean should_ignore;
} BlockData;



static void
block_parser_destroy_notify (gpointer data)
{
  BlockData *d = data;

  if (data == NULL)
    return;

  g_free (d->replace_key);
  g_free (d->data);
  g_free (d->conditional_key);
  g_free (d);
}

BlockParser *
block_parser_new ()
{
  BlockParser *parser;

  parser = g_new0 (BlockParser, 1);
  parser->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, block_parser_destroy_notify);
  parser->active_tree = g_queue_new ();

  return parser;
}

void
block_parser_free (BlockParser *parser)
{
  g_return_if_fail (parser != NULL);

  g_hash_table_destroy (parser->table);
  g_queue_free (parser->active_tree);
  g_free (parser);
}

/*
 *   block_parser_set_conditionals: set a hash table to be used for conditionals lookup
 *
 */
void
block_parser_set_conditionals (BlockParser *parser, GHashTable *conditionals)
{
  g_return_if_fail (parser != NULL);

  parser->conditionals = conditionals;
}

/*
 *   block_parser_register_key: tell parser to expect the key to catch
 *      key: part of the BEGIN_xxx placeholder, matched BEGIN_xxx and END_xxx as one block
 *      replace_key: placeholder to replace finished block with, for later use in replace_table,
 *                   placeholder will be surrounded by "<!-- $(replace_key) -->"
 *
 */
void
block_parser_register_key (BlockParser *parser, const gchar *key, const gchar *replace_key)
{
  BlockData *data;

  g_return_if_fail (parser != NULL);

  data = g_new0 (BlockData, 1);
  if (replace_key)
    data->replace_key = g_strdup_printf ("<!-- $(%s) -->", replace_key);
  data->used = TRUE;    /*  buffer is empty, mask it for block_parser_has_unused_data()  */
  data->is_conditional = FALSE;
  g_hash_table_replace (parser->table, g_strdup (key), data);
}

/*
 *   block_parser_get_data: return retrieved data or NULL if none read yet
 *      returns newly allocated string, caller is responsible for freeing
 *
 */
gchar *
block_parser_get_data (BlockParser *parser, const gchar *key)
{
  BlockData *data = NULL;

  g_return_val_if_fail (parser != NULL, NULL);

  data = g_hash_table_lookup (parser->table, key);
  if (data != NULL) {
    data->used = TRUE;
    return g_strdup (data->data);
  }

  return NULL;
}

/*
 *   block_parser_has_unused_data: indicates whether the data have already been read and used (by calling block_parser_get_data)
 *
 */
gboolean
block_parser_has_unused_data (BlockParser *parser, const gchar *key)
{
  BlockData *data = NULL;

  g_return_val_if_fail (parser != NULL, FALSE);

  data = g_hash_table_lookup (parser->table, key);
  if (data != NULL) {
    return (data->used == FALSE) && (data->finished == TRUE);
  }

  return FALSE;
}

/*
 *   block_parser_set_as_used: manually set data as used - sometimes blocks can act as containers for other nested blocks and
 *                                                         cannot be used itself
 *
 */
void
block_parser_set_as_used (BlockParser *parser, const gchar *key)
{
  BlockData *data = NULL;

  g_assert (parser != NULL);

  data = g_hash_table_lookup (parser->table, key);
  if (data != NULL && data->finished == TRUE) {
    data->used = TRUE;
  }
}



/* -------------------------------------------------------------------------------------------------------- */

static void
push_string (BlockParser *parser, const gchar *piece)
{
  BlockData *data;
  gchar *s;
  guint i;

  /*  Test if the data should be ignored  */
  if (parser->ignore_level > 0)
    return;

  /*  Not ignoring, find first non-conditional record  */
  for (i = 0; (data = g_queue_peek_nth (parser->active_tree, i)) != NULL; i++)
    if (! data->is_conditional)
      break;

  /*  Actually push the string  */
  if (data) {
    if (data->data)
      s = g_strdup_printf ("%s%s", data->data, piece);
    else
      s = g_strdup (piece);
    g_free (data->data);
    data->data = s;
    data->used = FALSE;
  }
  else {
    s = g_strdup_printf ("%s%s", parser->current_line, piece);
    g_free (parser->current_line);
    parser->current_line = s;
  }
}

static gchar *
extract_arg (const gchar *str)
{
  const gchar *start;
  const gchar *end;

  start = strstr (str, "(");
  if (start == NULL)
    return NULL;
  start++;

  end = strstr (str, ")");
  if (end == NULL)
    return NULL;
  end++;

  return g_strndup (start, end - start - 1);
}

/*
 *   block_parser_read_and_parse: reads input from the file and returns parsed line
 *      - if there's a multiline block, string before the opening placeholder and
 *        string after closing placeholder are returned on one line,
 *        separated by "replace_key" placeholder as specified in registered blocks
 *      - only the outmost "replace_key" placeholder will be used,
 *        no matter how many nested blocks were inside
 *      - returns newly allocated string, caller is responsible for freeing
 *
 */
gchar *
block_parser_read_and_parse (BlockParser *parser, FILE *stream)
{
  gchar *buffer;
  gchar *b;
  gchar *token;
  gchar *start, *end;
  gchar *s;
  GHashTableIter iter;
  gchar *key;
  BlockData *data;
  gboolean handled;

  g_return_val_if_fail (parser != NULL, NULL);

  buffer = g_malloc0 (BUFFER_SIZE);
  if (! fgets (buffer, BUFFER_SIZE, stream) || strlen (buffer) == 0) {
    g_free (buffer);
    return NULL;
  }
  parser->current_line = g_strdup ("");

  /*  find tokens  */
  b = buffer;
  while ((token = get_next_token (b, &start, &end, NULL))) {
    handled = FALSE;

    /*  push the string before the token  */
    s = g_strndup (b, start - b);
    push_string (parser, s);
    g_free (s);

    /*  match conditionals  */
    if (parser->conditionals && (g_str_has_prefix (token, "ifdef(") || g_str_has_prefix (token, "ifndef("))) {
      data = g_new0 (BlockData, 1);
      data->is_conditional = TRUE;
      data->conditional_key = extract_arg (token);
      data->should_ignore = ((g_str_has_prefix (token, "ifdef(") && g_hash_table_lookup (parser->conditionals, data->conditional_key) == NULL) ||
                             (g_str_has_prefix (token, "ifndef(") && g_hash_table_lookup (parser->conditionals, data->conditional_key) != NULL));
      if (data->should_ignore && parser->ignore_level == 0)
        parser->ignore_level = 1;
      else
        if (parser->ignore_level > 0)
          parser->ignore_level++;
      g_queue_push_head (parser->active_tree, data);
      handled = TRUE;
    }

    if (parser->conditionals && g_str_has_prefix (token, "endif(")) {
      s = extract_arg (token);
      data = g_queue_peek_head (parser->active_tree);
      if (data == NULL || !data->is_conditional || strcmp (data->conditional_key, s) != 0) {
        log_error ("block_parser_read_and_parse: something is wrong with the parser table: expected '%s' but got '%s'\n", s, data->conditional_key);
      }
      else {
        g_queue_pop_head (parser->active_tree);
        if (parser->ignore_level > 0)
          parser->ignore_level--;
        block_parser_destroy_notify (data);
        handled = TRUE;
      }
      g_free (s);
    }

    if (parser->conditionals && g_str_has_prefix (token, "else(")) {
      s = extract_arg (token);
      data = g_queue_peek_head (parser->active_tree);
      if (data == NULL || !data->is_conditional || strcmp (data->conditional_key, s) != 0) {
        log_error ("block_parser_read_and_parse: something is wrong with the parser table: expected '%s' but got '%s'\n", s, data->conditional_key);
      }
      else {
        data->should_ignore = ! data->should_ignore;
        /*  if we're not ignoring, level is zero, let's start ignoring by setting to one  */
        if (data->should_ignore && parser->ignore_level == 0)
          parser->ignore_level = 1;
        else
        /*  if we're ignoring but should not, let's go down to zero  */
          if (! data->should_ignore && parser->ignore_level == 1)
            parser->ignore_level = 0;
        /*  in all other cases (level > 1) we're ignoring already and we're not the topmost block, so let's keep being ignored  */
        handled = TRUE;
      }
      g_free (s);
    }

    /*  match known tags  */
    g_hash_table_iter_init (&iter, parser->table);
    while (!handled && g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &data)) {
      /*  test BEGIN_ tokens  */
      s = g_strdup_printf ("BEGIN_%s", key);
      if (strcmp (s, token) == 0 && data) {
        g_queue_push_head (parser->active_tree, data);
        handled = TRUE;
      }
      g_free (s);
      if (handled)
        break;

      /*  test END_ tokens  */
      s = g_strdup_printf ("END_%s", key);
      if (strcmp (s, token) == 0) {
        if (data == NULL || data != g_queue_peek_head (parser->active_tree)) {
          log_error ("block_parser_read_and_parse: something is wrong with the parser table: expected '%s'\n", key);
        }
        else {
          g_queue_pop_head (parser->active_tree);
          /*  push the replacement placeholder  */
          if (data->replace_key)
            push_string (parser, data->replace_key);
          data->finished = TRUE;
          handled = TRUE;
        }
      }
      g_free (s);
    }

    /*  push the tag if not matched above  */
    if (! handled) {
      s = g_strndup (start, end - start + 1);
      push_string (parser, s);
      g_free (s);
    }

    g_free (token);
    b = end + 1;
  }

  /*  push rest of the buffer till the end of the line  */
  push_string (parser, b);

  g_free (buffer);
  return parser->current_line;
}