diff options
| -rw-r--r-- | src/block-parser.c | 30 | ||||
| -rw-r--r-- | src/block-parser.h | 4 | ||||
| -rw-r--r-- | src/generators.c | 52 |
3 files changed, 42 insertions, 44 deletions
diff --git a/src/block-parser.c b/src/block-parser.c index ab679df..5c806cf 100644 --- a/src/block-parser.c +++ b/src/block-parser.c @@ -29,10 +29,6 @@ #include "replace-table.h" - -#define BUFFER_SIZE 65536 /* line cannot be longer than this */ - - struct BlockParser { GHashTable *table; GQueue *active_tree; @@ -231,7 +227,7 @@ push_string (BlockParser *parser, const gchar *piece) /* Actually push the string */ if (data) { if (data->data) - s = g_strdup_printf ("%s%s", data->data, piece); + s = g_strconcat (data->data, piece, NULL); else s = g_strdup (piece); g_free (data->data); @@ -239,7 +235,7 @@ push_string (BlockParser *parser, const gchar *piece) data->used = FALSE; } else { - s = g_strdup_printf ("%s%s", parser->current_line, piece); + s = g_strconcat (parser->current_line, piece, NULL); g_free (parser->current_line); parser->current_line = s; } @@ -288,7 +284,7 @@ in_block_retrieval (BlockParser *parser) } /* - * block_parser_read_and_parse: reads input from the file and returns parsed line + * block_parser_process: parse input data * - 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 @@ -298,10 +294,8 @@ in_block_retrieval (BlockParser *parser) * */ gchar * -block_parser_read_and_parse (BlockParser *parser, FILE *stream) +block_parser_process (BlockParser *parser, const gchar *buffer) { - gchar *buffer; - gchar *b; gchar *token; gchar *start, *end; gchar *s; @@ -314,21 +308,16 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream) BlockParserFuncData *func_data; g_return_val_if_fail (parser != NULL, NULL); + g_return_val_if_fail (buffer != 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))) { + while ((token = get_next_token (buffer, &start, &end, NULL))) { handled = FALSE; /* push the string before the token */ - s = g_strndup (b, start - b); + s = g_strndup (buffer, start - buffer); push_string (parser, s); g_free (s); @@ -446,12 +435,11 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream) } g_free (token); - b = end + 1; + buffer = end + 1; } /* push rest of the buffer till the end of the line */ - push_string (parser, b); + push_string (parser, buffer); - g_free (buffer); return parser->current_line; } diff --git a/src/block-parser.h b/src/block-parser.h index edf6dd2..f66aa08 100644 --- a/src/block-parser.h +++ b/src/block-parser.h @@ -78,7 +78,7 @@ gboolean block_parser_has_unused_data (BlockParser *parser, const gchar *key); void block_parser_set_as_used (BlockParser *parser, const gchar *key); /* - * block_parser_read_and_parse: reads input from the file and returns parsed line + * block_parser_process: parse input data * - 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 @@ -87,7 +87,7 @@ void block_parser_set_as_used (BlockParser *parser, const gchar *key); * - returns newly allocated string, caller is responsible for freeing * */ -gchar * block_parser_read_and_parse (BlockParser *parser, FILE *stream); +gchar * block_parser_process (BlockParser *parser, const gchar *buffer); G_END_DECLS diff --git a/src/generators.c b/src/generators.c index 2b0928b..4dec760 100644 --- a/src/generators.c +++ b/src/generators.c @@ -41,6 +41,8 @@ (! parent_items->fullsize || item->force_nofullsize) && \ (item->force_nofullsize || parent_items->nofullsize || setup->nofullsize)) +#define BUFFER_SIZE 65536 /* line cannot be longer than this */ + static TImageSize * find_image_size_for_name (TGallerySetup *setup, const gchar *name) { @@ -272,8 +274,9 @@ write_html_album (TGallerySetup *setup, { FILE *fin; FILE *fout; + gpointer buffer; gchar *line; - gchar *block; + GString *block; gchar *templates_path; gchar *s1, *s2, *s3, *s4, *s5; TAlbum *parent; @@ -289,9 +292,9 @@ write_html_album (TGallerySetup *setup, gboolean album_protected; ReplaceTable *global_replace_table; ReplaceTable *local_replace_table; - BlockParser *block_parser; + BlockParser *block_parser, *local_block_parser; TImageSize *image_size, *thumb_image_size; - GHashTable *defines; + GHashTable *defines, *local_defines; fin = fopen (template_src, "r"); @@ -448,13 +451,12 @@ write_html_album (TGallerySetup *setup, /* Read through the template and replace placeholders with real data */ - while (! feof (fin)) { - line = block_parser_read_and_parse (block_parser, fin); - if (line == NULL) - break; + buffer = g_malloc0 (BUFFER_SIZE); + while (fgets (buffer, BUFFER_SIZE, fin)) { + line = block_parser_process (block_parser, buffer); if (block_parser_has_unused_data (block_parser, "IMG_LIST")) { - block = g_strdup (""); + block = g_string_new (""); /* Now we have all block placeholders read, generate the items: */ for (i = 0; i < items->items->len; i++) { @@ -465,7 +467,8 @@ write_html_album (TGallerySetup *setup, } local_replace_table = replace_table_new (); - replace_table_set_defines (local_replace_table, defines); + local_defines = clone_string_hash_table (defines); + replace_table_set_defines (local_replace_table, local_defines); s1 = NULL; switch (item->type) { @@ -534,17 +537,23 @@ write_html_album (TGallerySetup *setup, } if (s1) { - replace_table_process (&s1, local_replace_table); - s2 = g_strdup_printf ("%s%s", block, s1); - g_free (block); - block = s2; + local_block_parser = block_parser_new (); + block_parser_set_conditionals (local_block_parser, local_defines); + s2 = block_parser_process (local_block_parser, s1); + block_parser_free (local_block_parser); + + replace_table_process (&s2, local_replace_table); + g_string_append (block, s2); + g_free (s2); g_free (s1); } replace_table_free (local_replace_table); + g_hash_table_destroy (local_defines); } - replace_table_process (&block, global_replace_table); - replace_table_add_key (global_replace_table, "IMG_LIST", block); - g_free (block); + s1 = g_string_free (block, FALSE); + replace_table_process (&s1, global_replace_table); + replace_table_add_key (global_replace_table, "IMG_LIST", s1); + g_free (s1); /* We don't use data from this key directly, let's mark it as used since we've built the structure we needed. */ block_parser_set_as_used (block_parser, "IMG_LIST"); } @@ -564,6 +573,7 @@ write_html_album (TGallerySetup *setup, fclose (fout); fclose (fin); + g_free (buffer); g_hash_table_destroy (defines); replace_table_free (global_replace_table); block_parser_free (block_parser); @@ -625,6 +635,7 @@ write_html_image (TGallerySetup *setup, { FILE *fin; FILE *fout; + gpointer buffer; gchar *img_dst; gchar *img_dst_page; gchar *img_orig_src; @@ -640,7 +651,6 @@ write_html_image (TGallerySetup *setup, TAlbum *parent; int i; gchar *line; - gchar *block; gchar *s1, *s2, *s3, *s4, *s5; gchar *imgname, *preload_imgname; gchar *title, *title_desc; @@ -939,10 +949,9 @@ write_html_image (TGallerySetup *setup, /* Read through the template and replace placeholders with real data */ - while (! feof (fin)) { - line = block_parser_read_and_parse (block_parser, fin); - if (line == NULL) - break; + buffer = g_malloc0 (BUFFER_SIZE); + while (fgets (buffer, BUFFER_SIZE, fin)) { + line = block_parser_process (block_parser, buffer); /* Replace all known tags */ replace_table_process (&line, replace_table); @@ -959,6 +968,7 @@ write_html_image (TGallerySetup *setup, fclose (fout); fclose (fin); + g_free (buffer); g_free (title); g_free (title_desc); g_free (img_dst); |
