[gegl/operation-source: 1/3] Allow operations to include their source as class metadata



commit 89ff4104304a5165de69f92b16ea4405863a0c47
Author: Jon Nordby <jononor gmail com>
Date:   Fri Nov 7 20:42:47 2014 +0100

    Allow operations to include their source as class metadata
    
    Allows GEGL applications to show the code, so users can
    study them or create new operations based on them easily.

 gegl/gegl-op.h                |   11 +++++++++++
 operations/.gitignore         |    1 +
 operations/Makefile-common.am |    3 +++
 operations/common/Makefile.am |    2 ++
 operations/core/Makefile.am   |    1 +
 tools/csourcetostring.py      |   33 +++++++++++++++++++++++++++++++++
 6 files changed, 51 insertions(+), 0 deletions(-)
---
diff --git a/gegl/gegl-op.h b/gegl/gegl-op.h
index e1421b9..fe4cd63 100644
--- a/gegl/gegl-op.h
+++ b/gegl/gegl-op.h
@@ -267,6 +267,10 @@ gegl_module_register (GTypeModule *module)
 #undef enum_value
 #undef enum_end
 
+#ifdef GEGL_OP_C_FILE_SOURCE
+#include GEGL_OP_C_FILE_SOURCE
+#endif
+
 #ifdef GETTEXT_PACKAGE
 static const gchar *gegl_op_gettext_package G_GNUC_UNUSED = GETTEXT_PACKAGE;
 #else
@@ -815,6 +819,13 @@ gegl_op_class_intern_init (gpointer klass)
   gboolean G_GNUC_UNUSED ui_digits_set = FALSE;
   GParamFlags flags G_GNUC_UNUSED = (GParamFlags)(G_PARAM_READWRITE | G_PARAM_CONSTRUCT | 
GEGL_PARAM_PAD_INPUT);
 
+#ifdef GEGL_OP_C_FILE_SOURCE
+  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);
+  gegl_operation_class_set_keys (operation_class,
+    "source", op_c_source,
+    NULL);
+#endif
+
   object_class->set_property = set_property;
   object_class->get_property = get_property;
   object_class->constructor  = gegl_op_constructor;
diff --git a/operations/.gitignore b/operations/.gitignore
index b336cc7..3bab32d 100644
--- a/operations/.gitignore
+++ b/operations/.gitignore
@@ -1,2 +1,3 @@
 /Makefile
 /Makefile.in
+*/*.c.h
diff --git a/operations/Makefile-common.am b/operations/Makefile-common.am
index ea7b0dc..cf14ae0 100644
--- a/operations/Makefile-common.am
+++ b/operations/Makefile-common.am
@@ -31,3 +31,6 @@ AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS)
 AM_LDFLAGS = -avoid-version -export-dynamic -module $(no_undefined)
 
 ext_dir = $(libdir)/gegl- GEGL_API_VERSION@
+
+%.c.h: %.c $(top_builddir)/tools/csourcetostring.py
+       $(PYTHON) $(top_builddir)/tools/csourcetostring.py $<
diff --git a/operations/common/Makefile.am b/operations/common/Makefile.am
index 76839cb..f649b3e 100644
--- a/operations/common/Makefile.am
+++ b/operations/common/Makefile.am
@@ -4,6 +4,8 @@ include $(top_srcdir)/operations/Makefile-common.am
 
 EXTRA_DIST = $(wildcard $(srcdir)/*.h)
 
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
+
 AM_CPPFLAGS += -I$(srcdir)
 
 LIBS = $(op_libs)
diff --git a/operations/core/Makefile.am b/operations/core/Makefile.am
index 88741cc..a5dcbeb 100644
--- a/operations/core/Makefile.am
+++ b/operations/core/Makefile.am
@@ -1,6 +1,7 @@
 include $(top_srcdir)/operations/Makefile-common.am
 
 EXTRA_DIST = $(wildcard $(srcdir)/*.h)
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
 
 AM_CPPFLAGS += -I$(srcdir)
 
diff --git a/tools/csourcetostring.py b/tools/csourcetostring.py
new file mode 100755
index 0000000..62a0b1b
--- /dev/null
+++ b/tools/csourcetostring.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+from __future__ import print_function
+
+import os
+import sys
+
+if len(sys.argv) != 2:
+  print("Usage: %s file.c" % sys.argv[0])
+  sys.exit(1)
+
+# From http://stackoverflow.com/questions/14945095/how-to-escape-string-for-generated-c
+def escape_string(s):
+  return s.replace('\\', r'\\').replace('"', r'\"')
+
+
+infile  = open(sys.argv[1], "r")
+outfile = open(sys.argv[1] + ".h", "w")
+
+cl_source = infile.read()
+infile.close()
+
+string_var_name = os.path.basename(sys.argv[1]).replace("-", "_").replace(":", "_")
+if string_var_name.endswith(".c"):
+  string_var_name = string_var_name.rstrip(".c")
+
+outfile.write("static const char* %s_c_source =\n" % "op")
+for line in cl_source.rstrip().split("\n"):
+  line = line.rstrip()
+  line = escape_string(line)
+  line = '"%-78s\\n"\n' % line
+  outfile.write(line)
+outfile.write(";\n")
+outfile.close()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]