gnumeric r16774 - in trunk: . plugins/excel po-functions src



Author: jody
Date: Tue Sep  2 20:30:29 2008
New Revision: 16774
URL: http://svn.gnome.org/viewvc/gnumeric?rev=16774&view=rev

Log:
2008-09-02  Jody Goldberg <jody gnome org>

	* src/search.c (gnm_search_replace_class_init) : add a property to be
	  used to search scripts.  NOT ENABLED yet.

	* src/ssgrep.c (search_string_table) : restore the old logic, and
	  extend it to agregate multiple matches and to search in VBA code if
	  available.
	(ssgrep) : enable search_string_table if -C/--string-table-count is
	  enabled.

Modified:
   trunk/ChangeLog
   trunk/NEWS
   trunk/configure.in
   trunk/plugins/excel/ChangeLog
   trunk/plugins/excel/boot.c
   trunk/po-functions/POTFILES.skip
   trunk/src/search.c
   trunk/src/search.h
   trunk/src/ssgrep.c

Modified: trunk/NEWS
==============================================================================
--- trunk/NEWS	(original)
+++ trunk/NEWS	Tue Sep  2 20:30:29 2008
@@ -7,6 +7,9 @@
 Jean:
 	* Fix printing of rotated text. [#539734]
 
+Jody:
+	* Extend ssgrep to search VBA too.
+
 J.H.M. Dassen (Ray):
 	* Understand and ignore byte-order markers for CSV/stf probing and
 	  importing. [#549743]

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Tue Sep  2 20:30:29 2008
@@ -709,7 +709,7 @@
 SAVE_LIBS=$LIBS
 CFLAGS="$CFLAGS $GNUMERIC_CFLAGS"
 LIBS="$GNUMERIC_LIBS $LIBS"
-AC_CHECK_FUNCS(g_date_set_time_t g_slice_alloc pango_font_map_create_context gtk_recent_manager_get_default gsf_open_pkg_foreach_rel)
+AC_CHECK_FUNCS(g_date_set_time_t g_slice_alloc pango_font_map_create_context gtk_recent_manager_get_default gsf_open_pkg_foreach_rel gsf_infile_msvba_steal_modules)
 CFLAGS=$SAVE_CFLAGS
 LIBS=$SAVE_LIBS
 

Modified: trunk/plugins/excel/boot.c
==============================================================================
--- trunk/plugins/excel/boot.c	(original)
+++ trunk/plugins/excel/boot.c	Tue Sep  2 20:30:29 2008
@@ -41,6 +41,7 @@
 #include <gsf/gsf-input.h>
 #include <gsf/gsf-infile.h>
 #include <gsf/gsf-infile-msole.h>
+#include <gsf/gsf-infile-msvba.h>
 #include <gsf/gsf-msole-utils.h>
 #include <gsf/gsf-output-stdio.h>
 #include <gsf/gsf-outfile.h>
@@ -138,15 +139,15 @@
 	}
 }
 
-/**
- * excel_file_open
- * @fo:         File opener
- * @context:	IO context
- * @wbv:	Workbook view
- * @input:	Input stream
- *
- * Load en excel workbook.
- **/
+#ifdef SPEW_VBA
+static void
+cb_dump_vba (char const *name, guint8 const *src_code)
+{
+	printf ("<module name=\"%s\">\n<![CDATA[%s]]>\n</module>\n", name, src_code);
+}
+#endif
+
+/* Service entry point */
 void
 excel_file_open (GOFileOpener const *fo, IOContext *context,
                  WorkbookView *wbv, GsfInput *input)
@@ -200,8 +201,24 @@
 	/* See if there are any macros to keep around */
 	stream = gsf_infile_child_by_name (ole, "\01CompObj");
 	if (stream != NULL) {
-		GsfInput *macros = gsf_infile_child_by_name (ole, "_VBA_PROJECT_CUR");
+		GsfInput *macros = gsf_infile_child_by_vname (ole, "_VBA_PROJECT_CUR", "VBA", NULL);
 		if (macros != NULL) {
+			GsfInfile *vba = gsf_infile_msvba_new (GSF_INFILE (macros), NULL);
+			if (NULL != vba) {
+				GHashTable *modules =
+					gsf_infile_msvba_steal_modules (GSF_INFILE_MSVBA (vba));
+				if (NULL != modules) {
+#ifdef SPEW_VBA
+					g_hash_table_foreach (modules,
+						(GHFunc) cb_dump_vba, NULL);
+#endif
+					g_object_set_data_full (G_OBJECT (wb), "VBA",
+						modules, (GDestroyNotify) g_hash_table_destroy);
+				}
+				g_object_unref (G_OBJECT (vba));
+			}
+
+			/* LOOKS BROKEN */
 			g_object_set_data_full (G_OBJECT (wb), "MS_EXCEL_COMPOBJ",
 				gsf_structured_blob_read (stream), g_object_unref);
 			g_object_set_data_full (G_OBJECT (wb), "MS_EXCEL_MACROS",

Modified: trunk/po-functions/POTFILES.skip
==============================================================================
--- trunk/po-functions/POTFILES.skip	(original)
+++ trunk/po-functions/POTFILES.skip	Tue Sep  2 20:30:29 2008
@@ -33,7 +33,7 @@
 plugins/fn-tsa/plugin.xml.in
 plugins/gda/plugin.xml.in
 plugins/gnome-db/plugin.xml.in
-plugins/gnome-db/ui.xml
+plugins/gnome-db/ui.xml.in
 plugins/gnome-glossary/plugin.xml.in
 plugins/html/plugin.xml.in
 plugins/lotus-123/plugin.xml.in
@@ -48,12 +48,12 @@
 plugins/psiconv/plugin.xml.in
 plugins/py-func/plugin.xml.in
 plugins/python-loader/plugin.xml.in
-plugins/python-loader/ui-console-menu.xml
+plugins/python-loader/ui-console-menu.xml.in
 plugins/qpro/plugin.xml.in
 plugins/sample_datasource/plugin.xml.in
 plugins/sc/plugin.xml.in
 plugins/sylk/plugin.xml.in
-plugins/uihello/hello.xml
+plugins/uihello/hello.xml.in
 plugins/uihello/plugin.xml.in
 plugins/xbase/plugin.xml.in
 schemas/gnumeric-dialogs.schemas.in

Modified: trunk/src/search.c
==============================================================================
--- trunk/src/search.c	(original)
+++ trunk/src/search.c	Tue Sep  2 20:30:29 2008
@@ -36,6 +36,7 @@
 	PROP_SEARCH_EXPRESSIONS,
 	PROP_SEARCH_EXPRESSION_RESULTS,
 	PROP_SEARCH_COMMENTS,
+	PROP_SEARCH_SCRIPTS,
 	PROP_INVERT,
 	PROP_BY_ROW,
 	PROP_QUERY,
@@ -429,6 +430,9 @@
 	case PROP_SEARCH_COMMENTS:
 		g_value_set_boolean (value, sr->search_comments);
 		break;
+	case PROP_SEARCH_SCRIPTS:
+		g_value_set_boolean (value, sr->search_scripts);
+		break;
 	case PROP_INVERT:
 		g_value_set_boolean (value, sr->invert);
 		break;
@@ -500,6 +504,9 @@
 	case PROP_SEARCH_COMMENTS:
 		sr->search_comments = g_value_get_boolean (value);
 		break;
+	case PROP_SEARCH_SCRIPTS:
+		sr->search_scripts = g_value_get_boolean (value);
+		break;
 	case PROP_INVERT:
 		sr->invert = g_value_get_boolean (value);
 		break;
@@ -598,6 +605,15 @@
 				       G_PARAM_READWRITE));
 	g_object_class_install_property
 		(gobject_class,
+		 PROP_SEARCH_SCRIPTS,
+		 g_param_spec_boolean ("search-scripts",
+				       _("Search Scripts"),
+				       _("Should scrips (workbook, and worksheet) be searched?"),
+				       FALSE,
+				       GSF_PARAM_STATIC |
+				       G_PARAM_READWRITE));
+	g_object_class_install_property
+		(gobject_class,
 		 PROP_INVERT,
 		 g_param_spec_boolean ("invert",
 				       _("Invert"),

Modified: trunk/src/search.h
==============================================================================
--- trunk/src/search.h	(original)
+++ trunk/src/search.h	Tue Sep  2 20:30:29 2008
@@ -62,6 +62,7 @@
 	gboolean search_expressions;
 	gboolean search_expression_results;
 	gboolean search_comments;
+	gboolean search_scripts;
 	gboolean invert;
 
 	GnmSearchReplaceError error_behaviour;

Modified: trunk/src/ssgrep.c
==============================================================================
--- trunk/src/ssgrep.c	(original)
+++ trunk/src/ssgrep.c	Tue Sep  2 20:30:29 2008
@@ -21,6 +21,8 @@
 #include "sheet.h"
 #include "cell.h"
 #include "value.h"
+#include "str.h"
+#include "func.h"
 #include "parse-util.h"
 #include "sheet-object-cell-comment.h"
 
@@ -35,6 +37,7 @@
 static gboolean ssgrep_locus_expressions = TRUE;
 static gboolean ssgrep_locus_results = FALSE;
 static gboolean ssgrep_locus_comments = TRUE;
+static gboolean ssgrep_locus_scripts = TRUE;
 static gboolean ssgrep_ignore_case = FALSE;
 static gboolean ssgrep_match_words = FALSE;
 static gboolean ssgrep_quiet = FALSE;
@@ -48,6 +51,7 @@
 static gboolean ssgrep_fixed_strings = FALSE;
 static gboolean ssgrep_recalc = FALSE;
 static gboolean ssgrep_invert_match = FALSE;
+static gboolean ssgrep_string_table = FALSE;
 
 static gboolean ssgrep_show_version = FALSE;
 static char *ssgrep_pattern_file = NULL;
@@ -62,6 +66,12 @@
 		N_("Only print a count of matches per file"),
 		NULL
 	},
+	{
+		"string-table-count", 'C',
+		0, G_OPTION_ARG_NONE, &ssgrep_string_table,
+		N_("optimization to search only via the string table, a display a count of the references."),
+		NULL
+	},
 
 	{
 		"pattern-file", 'f',
@@ -173,8 +183,100 @@
 	{ NULL }
 };
 
+typedef struct {
+	Workbook   *wb;
+	GHashTable *targets;
+	GHashTable *results;
+	char const *lc_code;
+} StringTableSearch;
+
+static void
+add_result (StringTableSearch *state, char const *clean, unsigned int n)
+{
+	gpointer prev;
+
+	if (NULL == state->results)
+		state->results = g_hash_table_new (g_str_hash, g_str_equal);
+	else if (NULL != (prev = g_hash_table_lookup (state->results, clean)))
+		n += GPOINTER_TO_UINT (prev);
+	g_hash_table_replace (state->results, (gpointer) clean, GUINT_TO_POINTER (n));
+}
+
+static void
+cb_check_strings (G_GNUC_UNUSED gpointer key, gpointer str, gpointer user_data)
+{
+	StringTableSearch *state = user_data;
+	char *clean = g_utf8_strdown (key, -1);
+	char const *orig = g_hash_table_lookup (state->targets, clean);
+	if (NULL != orig)
+		add_result (state, clean, ((GnmString *)str)->ref_count);
+	g_free (clean);
+}
+
 static void
-ssgrep (const char *arg, char const *uri, IOContext *ioc)
+cb_check_func (gpointer clean, gpointer orig, gpointer user_data)
+{
+	StringTableSearch *state = user_data;
+	GnmFunc	*func = gnm_func_lookup (clean, state->wb);
+	if (NULL != func)
+		add_result (state, clean, func->ref_count);
+}
+
+static void
+cb_find_target_in_module (gpointer clean, gpointer orig, gpointer user_data)
+{
+	StringTableSearch *state = user_data;
+	unsigned n = 0;
+	char const *ptr = state->lc_code;
+
+	while (NULL != (ptr = strstr (ptr, clean))) {
+		n++;
+		ptr++;
+	}
+
+	if (n > 0)
+		add_result (state, clean, n);
+}
+
+static void
+cb_check_module (gpointer name, gpointer code, gpointer user_data)
+{
+	StringTableSearch *state = user_data;
+	state->lc_code = g_utf8_strdown (code, -1);
+	g_hash_table_foreach (state->targets, &cb_find_target_in_module, state);
+	g_free ((gpointer)state->lc_code);
+	state->lc_code = NULL;
+}
+
+static void
+cb_dump_results (gpointer name, gpointer count)
+{
+	g_print ("\t%s : %u\n", (char const *)name, GPOINTER_TO_UINT (count));
+}
+
+static void
+search_string_table (Workbook *wb, char const *file_name, GHashTable *targets)
+{
+	StringTableSearch	 state;
+	GHashTable *modules;
+
+	state.wb	= wb;
+	state.targets	= targets;
+	state.results	= NULL;
+	gnm_string_foreach (&cb_check_strings, &state);
+	g_hash_table_foreach (targets, &cb_check_func, &state);
+
+	if (NULL != (modules = g_object_get_data (G_OBJECT (wb), "VBA")))
+		g_hash_table_foreach (modules, &cb_check_module, &state);
+	if (NULL != state.results) {
+		g_print ("%s\n", file_name);
+		g_hash_table_foreach (state.results, (GHFunc)&cb_dump_results, NULL);
+		g_hash_table_destroy (state.results);
+	}
+}
+
+static void
+ssgrep (const char *arg, char const *uri, IOContext *ioc, GHashTable *targets, char const *pattern)
 {
 	WorkbookView *wbv;
 	Workbook *wb;
@@ -197,6 +299,12 @@
 			workbook_recalc (wb);
 	}
 
+	if (ssgrep_string_table) {
+		search_string_table (wb, arg, targets);
+		g_object_unref (wb);
+		return;
+	}
+
 	search = (GnmSearchReplace*)
 		g_object_new (GNM_SEARCH_REPLACE_TYPE,
 			      "search-text", ssgrep_pattern,
@@ -209,6 +317,7 @@
 			      "search-expressions", ssgrep_locus_expressions,
 			      "search-expression-results", ssgrep_locus_results,
 			      "search-comments", ssgrep_locus_comments,
+			      "search-scripts", ssgrep_locus_scripts,
 			      "sheet", workbook_sheet_by_index (wb, 0),
 			      "scope", GNM_SRS_WORKBOOK,
 			      NULL);
@@ -296,9 +405,20 @@
 	g_object_unref (wb);
 }
 
+/* simple stripped down hash of lower case target, only used for string table
+ * searches */
+static void
+add_target (GHashTable *ssgrep_targets, char const *target)
+{
+	char *orig = g_strstrip (g_strdup (target));
+	char *clean = g_utf8_strdown (orig, -1);
+	g_hash_table_insert (ssgrep_targets, clean, orig);
+}
+
 int
 main (int argc, char const **argv)
 {
+	GHashTable	*ssgrep_targets;
 	ErrorInfo	*plugin_errs;
 	IOContext	*ioc;
 	GOCmdContext	*cc;
@@ -329,6 +449,9 @@
 		return 0;
 	} 
 
+	gnm_init (FALSE);
+
+	ssgrep_targets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 	if (ssgrep_pattern_file) {
 		char *uri = go_shell_arg_to_uri (ssgrep_pattern_file);
 		GsfInput     	 *input;
@@ -359,6 +482,8 @@
 				go_regexp_quote (pat, line);
 			else
 				g_string_append (pat, line);
+
+			add_target (ssgrep_targets, line);
 		}
 
 		ssgrep_pattern = g_string_free (pat, FALSE);
@@ -379,6 +504,8 @@
 			ssgrep_pattern = g_string_free (pat, FALSE);
 		} else
 			ssgrep_pattern = g_strdup (argv[1]);
+		add_target (ssgrep_targets, argv[1]);
+
 		i = 2;
 		N = argc - i;
 	}
@@ -389,8 +516,6 @@
 		N = 1;
 	}
 
-	gnm_init (FALSE);
-
 	cc = cmd_context_stderr_new ();
 	gnm_plugins_init (GO_CMD_CONTEXT (cc));
 	go_plugin_db_activate_plugin_list (
@@ -406,10 +531,12 @@
 		const char *arg = argv[i];
 		char *uri = go_shell_arg_to_uri (arg);
 		gnm_io_context_processing_file (ioc, uri);
-		ssgrep (arg, uri, ioc);
+		ssgrep (arg, uri, ioc, ssgrep_targets, ssgrep_pattern);
 		g_free (uri);
 	}
 
+	g_hash_table_destroy (ssgrep_targets);
+
 	g_object_unref (ioc);
 
 	g_object_unref (cc);



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