[go: up one dir, main page]

File: main.c

package info (click to toggle)
template-glib 3.38.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 788 kB
  • sloc: ansic: 6,774; yacc: 288; lex: 237; javascript: 16; makefile: 14; python: 12
file content (99 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download | duplicates (5)
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
/* main.c
 *
 * Copyright (C) 2015-2017 Christian Hergert <chergert@redhat.com>
 *
 * This file is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This file 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 Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <stdio.h>
#include <stdlib.h>
#include <tmpl-glib.h>

static gboolean
method_missing (TmplScope    *scope,
                const gchar  *name,
                TmplSymbol  **symbol,
                gpointer      user_data)
{
  g_autofree gchar *str = g_strdup_printf ("missing symbol: %s", name);

  g_assert (scope != NULL);
  g_assert (name != NULL);
  g_assert (symbol != NULL);

  *symbol = tmpl_symbol_new ();
  tmpl_symbol_assign_string (*symbol, str);

  return TRUE;
}

gint
main (gint   argc,
      gchar *argv[])
{
  TmplTemplateLocator *locator = NULL;
  GOutputStream *stream = NULL;
  TmplTemplate *tmpl = NULL;
  TmplScope *scope = NULL;
  GFile *file = NULL;
  gchar *output;
  GError *error = NULL;
  gint ret = EXIT_FAILURE;
  gchar zero = 0;

  if (argc != 2)
    {
      g_printerr ("usage: %s TEMPLATE\n", argv [0]);
      return EXIT_FAILURE;
    }

  locator = tmpl_template_locator_new ();
  tmpl_template_locator_prepend_search_path (locator, ".");
  tmpl = tmpl_template_new (locator);
  file = g_file_new_for_commandline_arg (argv [1]);
  scope = tmpl_scope_new ();

  tmpl_scope_set_resolver (scope, method_missing, NULL, NULL);

  if (!tmpl_template_parse_file (tmpl, file, NULL, &error))
    {
      g_printerr ("ERROR: %s\n", error->message);
      g_clear_error (&error);
      goto cleanup;
    }

  stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);

  if (!tmpl_template_expand (tmpl, stream, scope, NULL, &error))
    {
      g_printerr ("ERROR: %s\n", error->message);
      g_clear_error (&error);
      goto cleanup;
    }

  g_output_stream_write (stream, &zero, 1, NULL, NULL);
  output = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (stream));
  g_print ("%s\n", output);

  ret = EXIT_SUCCESS;

cleanup:
  g_clear_object (&stream);
  g_clear_object (&locator);
  g_clear_object (&tmpl);
  g_clear_object (&file);

  return ret;
}