From d4efcb30e2647a86fc0b2df0a5b29bb1d84163d8 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 8 Apr 2017 10:44:43 +0200 Subject: [PATCH] Aggiunta interfaccia a ctpl. --- configure.ac | 5 ++ libsolipactpl.pc.in | 11 +++ src/Makefile.am | 13 +++ src/ctpl.c | 204 ++++++++++++++++++++++++++++++++++++++++++++ src/ctpl.h | 62 ++++++++++++++ 5 files changed, 295 insertions(+) create mode 100644 libsolipactpl.pc.in create mode 100644 src/ctpl.c create mode 100644 src/ctpl.h diff --git a/configure.ac b/configure.ac index bc294e4..8729386 100644 --- a/configure.ac +++ b/configure.ac @@ -85,6 +85,10 @@ AM_CONDITIONAL(HAVE_CAMEL3, test $camel3_found = yes) AC_SUBST(CAMEL_PKGCONFIG) +PKG_CHECK_MODULES(CTPL, [ctpl]) +AC_SUBST(CTPL_CFLAGS) +AC_SUBST(CTPL_LIBS) + PKG_CHECK_MODULES(PYTHON, [python]) AC_SUBST(PYTHON_CFLAGS) AC_SUBST(PYTHON_LIBS) @@ -119,6 +123,7 @@ AC_SUBST(SOLIPA_WIN32_LIBS) # Output files AC_CONFIG_FILES([ libsolipa.pc + libsolipactpl.pc libsolipamail.pc libsolipaooo.pc Makefile diff --git a/libsolipactpl.pc.in b/libsolipactpl.pc.in new file mode 100644 index 0000000..c0a861d --- /dev/null +++ b/libsolipactpl.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: @PACKAGE_NAME@ +Description: Classe con funzioni varie di utilità - Supporto per la libreria ctpl. +Version: @PACKAGE_VERSION@ +Requires: libsolipa ctpl +Libs: -L${libdir} -lsolipactpl +Cflags: -I${includedir} diff --git a/src/Makefile.am b/src/Makefile.am index b411d19..f946ccc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -33,6 +33,7 @@ AM_CPPFLAGS = $(SOLIPA_CFLAGS) \ $(CAMEL3) lib_LTLIBRARIES = libsolipa.la \ + libsolipactpl.la \ libsolipamail.la \ libsolipaooo.la @@ -57,6 +58,18 @@ libsolipa_include_HEADERS = libsolipa.h \ libsolipa_includedir = $(includedir)/libsolipa +libsolipactpl_la_LIBADD = libsolipa.la + +libsolipactpl_la_CFLAGS = + +libsolipactpl_la_LDFLAGS = -no-undefined + +libsolipactpl_la_SOURCES = ctpl.c + +libsolipactpl_include_HEADERS = ctpl.h + +libsolipactpl_includedir = $(includedir)/libsolipa + libsolipamail_la_LIBADD = libsolipa.la libsolipamail_la_CFLAGS = diff --git a/src/ctpl.c b/src/ctpl.c new file mode 100644 index 0000000..5af3384 --- /dev/null +++ b/src/ctpl.c @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2017 Andrea Zagli + * + * This library 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.1 of the License, or (at your option) any later version. + * + * This library 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 Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#ifdef G_OS_WIN32 +#include +#endif + +#include + +#include "solipa.h" +#include "ctpl.h" + +static void solipa_ctpl_class_init (SolipaCtplClass *class); +static void solipa_ctpl_init (SolipaCtpl *solipa); + +static void solipa_ctpl_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void solipa_ctpl_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void solipa_ctpl_dispose (GObject *gobject); +static void solipa_ctpl_finalize (GObject *gobject); + +#define SOLIPA_CTPL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SOLIPA_TYPE_CTPL, SolipaCtplPrivate)) + +typedef struct _SolipaCtplPrivate SolipaCtplPrivate; +struct _SolipaCtplPrivate + { + }; + +G_DEFINE_TYPE (SolipaCtpl, solipa_ctpl, G_TYPE_OBJECT) + +static void +solipa_ctpl_class_init (SolipaCtplClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = solipa_ctpl_set_property; + object_class->get_property = solipa_ctpl_get_property; + object_class->dispose = solipa_ctpl_dispose; + object_class->finalize = solipa_ctpl_finalize; + + g_type_class_add_private (object_class, sizeof (SolipaCtplPrivate)); +} + +static void +solipa_ctpl_init (SolipaCtpl *solipa) +{ + SolipaCtplPrivate *priv = SOLIPA_CTPL_GET_PRIVATE (solipa); +} + +/** + * solipa_ctpl_new: + * + * Returns: the newly created #SolipaCtpl object. + */ +SolipaCtpl +*solipa_ctpl_new () +{ + SolipaCtpl *solipa; + SolipaCtplPrivate *priv; + + solipa = SOLIPA_CTPL (g_object_new (solipa_ctpl_get_type (), NULL)); + priv = SOLIPA_CTPL_GET_PRIVATE (solipa); + + return solipa; +} + +gchar +*solipa_ctpl_get_filled (SolipaCtpl *solipa, const gchar *ctpl_file, CtplEnviron *env) +{ + SolipaCtplPrivate *priv; + + gchar *ret; + + GError *error; + + CtplInputStream *is; + + GOutputStream *mouts; + CtplOutputStream *ctplouts; + + CtplToken *tree; + + ret = g_strdup (""); + + g_return_val_if_fail (IS_SOLIPA (solipa), ret); + + error = NULL; + is = ctpl_input_stream_new_for_path (ctpl_file, &error); + if (is == NULL + || error != NULL) + { + g_warning ("Error opening template: %s", error != NULL && error->message != NULL ? error->message : "no details"); + } + + mouts = g_memory_output_stream_new_resizable (); + ctplouts = ctpl_output_stream_new (G_OUTPUT_STREAM (mouts)); + + /* first, create a token tree from the input template */ + error = NULL; + tree = ctpl_lexer_lex (is, &error); + if (tree != NULL) + { + /* then, parse this tree against the environment */ + error = NULL; + if (ctpl_parser_parse (tree, env, ctplouts, &error)) + { + g_free (ret); + ret = g_strdup (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mouts))); + } + else + { + g_warning ("Error on parsing: %s", error != NULL && error->message != NULL ? error->message : "no details"); + } + + /* and finally, free the built tree */ + ctpl_token_free (tree); + } + else + { + g_warning ("Error opening lexer: %s", error != NULL && error->message != NULL ? error->message : "no details"); + } + + return ret; +} + +/* PRIVATE */ +static void +solipa_ctpl_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + SolipaCtpl *solipa = (SolipaCtpl *)object; + SolipaCtplPrivate *priv = SOLIPA_CTPL_GET_PRIVATE (solipa); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +solipa_ctpl_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + SolipaCtpl *solipa = (SolipaCtpl *)object; + SolipaCtplPrivate *priv = SOLIPA_CTPL_GET_PRIVATE (solipa); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +solipa_ctpl_dispose (GObject *gobject) +{ + SolipaCtpl *solipa = (SolipaCtpl *)gobject; + SolipaCtplPrivate *priv = SOLIPA_CTPL_GET_PRIVATE (solipa); + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +solipa_ctpl_finalize (GObject *gobject) +{ + SolipaCtpl *solipa = (SolipaCtpl *)gobject; + SolipaCtplPrivate *priv = SOLIPA_CTPL_GET_PRIVATE (solipa); + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/ctpl.h b/src/ctpl.h new file mode 100644 index 0000000..d76518e --- /dev/null +++ b/src/ctpl.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2017 Andrea Zagli + * + * This library 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.1 of the License, or (at your option) any later version. + * + * This library 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 Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __SOLIPA_CTPL_H__ +#define __SOLIPA_CTPL_H__ + +#include +#include + +#include + + +G_BEGIN_DECLS + + +#define SOLIPA_TYPE_CTPL (solipa_ctpl_get_type ()) +#define SOLIPA_CTPL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SOLIPA_TYPE_CTPL, SolipaCtpl)) +#define SOLIPA_CTPL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SOLIPA_TYPE_CTPL, SolipaCtplClass)) +#define IS_SOLIPA_CTPL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SOLIPA_TYPE_CTPL)) +#define IS_SOLIPA_CTPL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SOLIPA_TYPE_CTPL)) +#define SOLIPA_CTPL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SOLIPA_TYPE_CTPL, SolipaCtplClass)) + +typedef struct _SolipaCtpl SolipaCtpl; +typedef struct _SolipaCtplClass SolipaCtplClass; + +struct _SolipaCtpl + { + GObject parent; + }; + +struct _SolipaCtplClass + { + GObjectClass parent_class; + }; + +GType solipa_ctpl_get_type (void) G_GNUC_CONST; + + +SolipaCtpl *solipa_ctpl_new (void); + +gchar *solipa_ctpl_get_filled (SolipaCtpl *solipa_ctpl, const gchar *ctpl_file, CtplEnviron *env); + + +G_END_DECLS + + +#endif /* __SOLIPA_CTPL_H__ */ -- 2.49.0