From e191bf678c7a1ebbda35f8ddd27dda180c6713b9 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Mon, 10 Dec 2007 00:54:52 +0000 Subject: [PATCH] Refactor scannerwriter to only be tied to a GIdlModule and move it 2007-12-10 Johan Dahlin * src/Makefile.am: * src/gidlwriter.c: (g_writer_write_inline), (g_writer_write), (g_writer_write_indent), (g_writer_write_unindent), (field_generate), (value_generate), (constant_generate), (property_generate), (function_generate), (vfunc_generate), (signal_generate), (interface_generate), (struct_generate), (union_generate), (boxed_generate), (enum_generate), (node_generate), (g_writer_write_module), (g_idl_writer_save_file): * src/gidlwriter.h: * src/scanner.c: (g_igenerator_generate): * src/scanner.h: * src/scannerwriter.c: Refactor scannerwriter to only be tied to a GIdlModule and move it (again!) to gidlwriter.c. Change the writer function to take a filename. svn path=/trunk/; revision=81 --- ChangeLog | 19 +++ src/Makefile.am | 2 +- src/{scannerwriter.c => gidlwriter.c} | 202 ++++++++++++++------------ src/gidlwriter.h | 26 ++++ src/scanner.c | 3 +- src/scanner.h | 5 - 6 files changed, 159 insertions(+), 98 deletions(-) rename src/{scannerwriter.c => gidlwriter.c} (61%) create mode 100644 src/gidlwriter.h diff --git a/ChangeLog b/ChangeLog index ec843f97..5df3128d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2007-12-10 Johan Dahlin + + * src/Makefile.am: + * src/gidlwriter.c: (g_writer_write_inline), (g_writer_write), + (g_writer_write_indent), (g_writer_write_unindent), + (field_generate), (value_generate), (constant_generate), + (property_generate), (function_generate), (vfunc_generate), + (signal_generate), (interface_generate), (struct_generate), + (union_generate), (boxed_generate), (enum_generate), + (node_generate), (g_writer_write_module), (g_idl_writer_save_file): + * src/gidlwriter.h: + * src/scanner.c: (g_igenerator_generate): + * src/scanner.h: + * src/scannerwriter.c: + + Refactor scannerwriter to only be tied to a GIdlModule and move + it (again!) to gidlwriter.c. Change the writer function to take + a filename. + 2007-12-10 Johan Dahlin * src/Makefile.am: diff --git a/src/Makefile.am b/src/Makefile.am index 193d4f31..b1ca2ff0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,7 +34,7 @@ g_idl_scanner_SOURCES = \ cparser.y \ scanner.c \ scanner.h \ - scannerwriter.c + gidlwriter.c g_idl_scanner_CFLAGS = $(GIREPO_CFLAGS) $(SCANNER_CFLAGS) g_idl_scanner_LDADD = $(GIREPO_LIBS) $(SCANNER_LIBS) libgirepository.la libgirepository-parser.la diff --git a/src/scannerwriter.c b/src/gidlwriter.c similarity index 61% rename from src/scannerwriter.c rename to src/gidlwriter.c index 0670e975..2bfeb25c 100644 --- a/src/scannerwriter.c +++ b/src/gidlwriter.c @@ -1,6 +1,7 @@ /* GObject introspection: gen-introspect * * Copyright (C) 2007 Jürg Billeter + * Copyright (C) 2007 Johan Dahlin * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -26,73 +27,78 @@ #include "scanner.h" #include "gidlnode.h" -static void node_generate (GIGenerator * igenerator, GIdlNode * node); +typedef struct { + int indent; + FILE *output; +} GIdlWriter; + +static void node_generate (GIdlWriter * writer, GIdlNode * node); static void -g_igenerator_write_inline (GIGenerator * igenerator, const char *s) +g_writer_write_inline (GIdlWriter * writer, const char *s) { - fprintf (stdout, "%s", s); + fprintf (writer->output, "%s", s); } static void -g_igenerator_write (GIGenerator * igenerator, const char *s) +g_writer_write (GIdlWriter * writer, const char *s) { int i; - for (i = 0; i < igenerator->indent; i++) + for (i = 0; i < writer->indent; i++) { - fprintf (stdout, "\t"); + fprintf (writer->output, "\t"); } - g_igenerator_write_inline (igenerator, s); + g_writer_write_inline (writer, s); } static void -g_igenerator_write_indent (GIGenerator * igenerator, const char *s) +g_writer_write_indent (GIdlWriter * writer, const char *s) { - g_igenerator_write (igenerator, s); - igenerator->indent++; + g_writer_write (writer, s); + writer->indent++; } static void -g_igenerator_write_unindent (GIGenerator * igenerator, const char *s) +g_writer_write_unindent (GIdlWriter * writer, const char *s) { - igenerator->indent--; - g_igenerator_write (igenerator, s); + writer->indent--; + g_writer_write (writer, s); } static void -field_generate (GIGenerator * igenerator, GIdlNodeField * node) +field_generate (GIdlWriter * writer, GIdlNodeField * node) { char *markup = g_markup_printf_escaped ("\n", node->node.name, node->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } static void -value_generate (GIGenerator * igenerator, GIdlNodeValue * node) +value_generate (GIdlWriter * writer, GIdlNodeValue * node) { char *markup = g_markup_printf_escaped ("\n", node->node.name, node->value); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } static void -constant_generate (GIGenerator * igenerator, GIdlNodeConstant * node) +constant_generate (GIdlWriter * writer, GIdlNodeConstant * node) { char *markup = g_markup_printf_escaped ("\n", node->node.name, node->type->unparsed, node->value); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } static void -property_generate (GIGenerator * igenerator, GIdlNodeProperty * node) +property_generate (GIdlWriter * writer, GIdlNodeProperty * node) { char *markup = g_markup_printf_escaped ("writable ? "1" : "0", node->construct ? "1" : "0", node->construct_only ? "1" : "0"); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } static void -function_generate (GIGenerator * igenerator, GIdlNodeFunction * node) +function_generate (GIdlWriter * writer, GIdlNodeFunction * node) { char *markup; const char *tag_name; @@ -144,65 +150,65 @@ function_generate (GIGenerator * igenerator, GIdlNodeFunction * node) node->node.name, node->symbol); } - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); markup = g_markup_printf_escaped ("\n", node->result->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); if (node->parameters != NULL) { GList *l; - g_igenerator_write_indent (igenerator, "\n"); + g_writer_write_indent (writer, "\n"); for (l = node->parameters; l != NULL; l = l->next) { GIdlNodeParam *param = l->data; markup = g_markup_printf_escaped ("\n", param->node.name, param->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } markup = g_strdup_printf ("\n", tag_name); - g_igenerator_write_unindent (igenerator, markup); + g_writer_write_unindent (writer, markup); g_free (markup); } static void -vfunc_generate (GIGenerator * igenerator, GIdlNodeVFunc * node) +vfunc_generate (GIdlWriter * writer, GIdlNodeVFunc * node) { char *markup = g_markup_printf_escaped ("\n", node->node.name); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); markup = g_markup_printf_escaped ("\n", node->result->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); if (node->parameters != NULL) { GList *l; - g_igenerator_write_indent (igenerator, "\n"); + g_writer_write_indent (writer, "\n"); for (l = node->parameters; l != NULL; l = l->next) { GIdlNodeParam *param = l->data; markup = g_markup_printf_escaped ("\n", param->node.name, param->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } static void -signal_generate (GIGenerator * igenerator, GIdlNodeSignal * node) +signal_generate (GIdlWriter * writer, GIdlNodeSignal * node) { char *markup; const char *when = "LAST"; @@ -217,33 +223,33 @@ signal_generate (GIGenerator * igenerator, GIdlNodeSignal * node) markup = g_markup_printf_escaped ("\n", node->node.name, when); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); markup = g_markup_printf_escaped ("\n", node->result->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); if (node->parameters != NULL) { GList *l; - g_igenerator_write_indent (igenerator, "\n"); + g_writer_write_indent (writer, "\n"); for (l = node->parameters; l != NULL; l = l->next) { GIdlNodeParam *param = l->data; markup = g_markup_printf_escaped ("\n", param->node.name, param->type->unparsed); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } static void -interface_generate (GIGenerator * igenerator, GIdlNodeInterface * node) +interface_generate (GIdlWriter * writer, GIdlNodeInterface * node) { GList *l; char *markup; @@ -267,102 +273,102 @@ interface_generate (GIGenerator * igenerator, GIdlNodeInterface * node) node->node.name, node->gtype_name, node->gtype_init); } - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); if (node->node.type == G_IDL_NODE_OBJECT && node->interfaces != NULL) { GList *l; - g_igenerator_write_indent (igenerator, "\n"); + g_writer_write_indent (writer, "\n"); for (l = node->interfaces; l != NULL; l = l->next) { markup = g_markup_printf_escaped ("\n", (char *) l->data); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } else if (node->node.type == G_IDL_NODE_INTERFACE && node->prerequisites != NULL) { GList *l; - g_igenerator_write_indent (igenerator, "\n"); + g_writer_write_indent (writer, "\n"); for (l = node->prerequisites; l != NULL; l = l->next) { markup = g_markup_printf_escaped ("\n", (char *) l->data); - g_igenerator_write (igenerator, markup); + g_writer_write (writer, markup); g_free (markup); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } for (l = node->members; l != NULL; l = l->next) { - node_generate (igenerator, l->data); + node_generate (writer, l->data); } if (node->node.type == G_IDL_NODE_OBJECT) { - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } else if (node->node.type == G_IDL_NODE_INTERFACE) { - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } } static void -struct_generate (GIGenerator * igenerator, GIdlNodeStruct * node) +struct_generate (GIdlWriter * writer, GIdlNodeStruct * node) { GList *l; char *markup = g_markup_printf_escaped ("\n", node->node.name); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); for (l = node->members; l != NULL; l = l->next) { - node_generate (igenerator, l->data); + node_generate (writer, l->data); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } static void -union_generate (GIGenerator * igenerator, GIdlNodeUnion * node) +union_generate (GIdlWriter * writer, GIdlNodeUnion * node) { GList *l; char *markup = g_markup_printf_escaped ("\n", node->node.name); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); for (l = node->members; l != NULL; l = l->next) { - node_generate (igenerator, l->data); + node_generate (writer, l->data); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } static void -boxed_generate (GIGenerator * igenerator, GIdlNodeBoxed * node) +boxed_generate (GIdlWriter * writer, GIdlNodeBoxed * node) { GList *l; char *markup = g_markup_printf_escaped ("\n", node->node.name, node->gtype_name, node->gtype_init); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); for (l = node->members; l != NULL; l = l->next) { - node_generate (igenerator, l->data); + node_generate (writer, l->data); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } static void -enum_generate (GIGenerator * igenerator, GIdlNodeEnum * node) +enum_generate (GIdlWriter * writer, GIdlNodeEnum * node) { GList *l; char *markup; @@ -378,62 +384,62 @@ enum_generate (GIGenerator * igenerator, GIdlNodeEnum * node) } markup = g_markup_printf_escaped ("<%s name=\"%s\">\n", tag_name, node->node.name); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); for (l = node->values; l != NULL; l = l->next) { - node_generate (igenerator, l->data); + node_generate (writer, l->data); } markup = g_strdup_printf ("\n", tag_name); - g_igenerator_write_unindent (igenerator, markup); + g_writer_write_unindent (writer, markup); g_free (markup); } static void -node_generate (GIGenerator * igenerator, GIdlNode * node) +node_generate (GIdlWriter * writer, GIdlNode * node) { switch (node->type) { case G_IDL_NODE_FUNCTION: case G_IDL_NODE_CALLBACK: - function_generate (igenerator, (GIdlNodeFunction *) node); + function_generate (writer, (GIdlNodeFunction *) node); break; case G_IDL_NODE_VFUNC: - vfunc_generate (igenerator, (GIdlNodeVFunc *) node); + vfunc_generate (writer, (GIdlNodeVFunc *) node); break; case G_IDL_NODE_OBJECT: case G_IDL_NODE_INTERFACE: - interface_generate (igenerator, (GIdlNodeInterface *) node); + interface_generate (writer, (GIdlNodeInterface *) node); break; case G_IDL_NODE_STRUCT: - struct_generate (igenerator, (GIdlNodeStruct *) node); + struct_generate (writer, (GIdlNodeStruct *) node); break; case G_IDL_NODE_UNION: - union_generate (igenerator, (GIdlNodeUnion *) node); + union_generate (writer, (GIdlNodeUnion *) node); break; case G_IDL_NODE_BOXED: - boxed_generate (igenerator, (GIdlNodeBoxed *) node); + boxed_generate (writer, (GIdlNodeBoxed *) node); break; case G_IDL_NODE_ENUM: case G_IDL_NODE_FLAGS: - enum_generate (igenerator, (GIdlNodeEnum *) node); + enum_generate (writer, (GIdlNodeEnum *) node); break; case G_IDL_NODE_PROPERTY: - property_generate (igenerator, (GIdlNodeProperty *) node); + property_generate (writer, (GIdlNodeProperty *) node); break; case G_IDL_NODE_FIELD: - field_generate (igenerator, (GIdlNodeField *) node); + field_generate (writer, (GIdlNodeField *) node); break; case G_IDL_NODE_SIGNAL: - signal_generate (igenerator, (GIdlNodeSignal *) node); + signal_generate (writer, (GIdlNodeSignal *) node); break; case G_IDL_NODE_VALUE: - value_generate (igenerator, (GIdlNodeValue *) node); + value_generate (writer, (GIdlNodeValue *) node); break; case G_IDL_NODE_CONSTANT: - constant_generate (igenerator, (GIdlNodeConstant *) node); + constant_generate (writer, (GIdlNodeConstant *) node); break; default: g_assert_not_reached (); @@ -441,25 +447,39 @@ node_generate (GIGenerator * igenerator, GIdlNode * node) } static void -module_generate (GIGenerator * igenerator, GIdlModule * module) +g_writer_write_module (GIdlWriter * writer, GIdlModule * module) { GList *l; char *markup = g_markup_printf_escaped ("\n", module->name); - g_igenerator_write_indent (igenerator, markup); + g_writer_write_indent (writer, markup); g_free (markup); for (l = module->entries; l != NULL; l = l->next) { - node_generate (igenerator, l->data); + node_generate (writer, l->data); } - g_igenerator_write_unindent (igenerator, "\n"); + g_writer_write_unindent (writer, "\n"); } void -g_scanner_write_file (GIGenerator *igenerator) +g_idl_writer_save_file (GIdlModule *module, + const gchar *filename) { - g_igenerator_write (igenerator, "\n"); - g_igenerator_write_indent (igenerator, "\n"); - module_generate (igenerator, igenerator->module); - g_igenerator_write_unindent (igenerator, "\n"); + FILE * file; + GIdlWriter *writer; + + writer = g_new0 (GIdlWriter, 1); + + if (!filename) + writer->output = stdout; + else + writer->output = fopen (filename, "w"); + + g_writer_write (writer, "\n"); + g_writer_write_indent (writer, "\n"); + g_writer_write_module (writer, module); + g_writer_write_unindent (writer, "\n"); + + if (filename) + fclose (writer->output); } diff --git a/src/gidlwriter.h b/src/gidlwriter.h new file mode 100644 index 00000000..5d41a0c6 --- /dev/null +++ b/src/gidlwriter.h @@ -0,0 +1,26 @@ +/* GObject introspection: IDL writer + * + * Copyright (C) 2007 Johan Dahlin + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __G_IDL_WRITER_H__ +#define __G_IDL_WRITER_H__ + +void g_idl_writer_save_file (GIdlModule *module, const gchar *filename); + +#endif /* __G_IDL_WRITER_H__ */ diff --git a/src/scanner.c b/src/scanner.c index c09ec619..ea3ded1d 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -33,6 +33,7 @@ #include "gidlparser.h" #include "gidlmodule.h" #include "gidlnode.h" +#include "gidlwriter.h" typedef GType (*TypeFunction) (void); @@ -1244,7 +1245,7 @@ g_igenerator_generate (GIGenerator * igenerator, GList *libraries) g_igenerator_process_symbols (igenerator); - g_scanner_write_file (igenerator); + g_idl_writer_save_file (igenerator->module, NULL); } static int diff --git a/src/scanner.h b/src/scanner.h index 3e065b74..6c70d89e 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -53,8 +53,6 @@ struct _GIGenerator GHashTable *type_by_lower_case_prefix; GHashTable *symbols; /* typename -> module.name */ - - int indent; }; typedef enum @@ -146,10 +144,7 @@ void g_igenerator_parse (GIGenerator * igenerator, FILE * f); void g_igenerator_add_symbol (GIGenerator * igenerator, CSymbol * symbol); gboolean g_igenerator_is_typedef (GIGenerator * igenerator, const char *name); -void g_scanner_write_file (GIGenerator *generator); - GIGenerator *the_igenerator; - G_END_DECLS #endif -- 2.34.1