gnumeric r17288 - in trunk: . plugins/applix plugins/lotus-123 plugins/sc plugins/sylk plugins/xbase src



Author: mortenw
Date: Thu Apr  2 19:12:23 2009
New Revision: 17288
URL: http://svn.gnome.org/viewvc/gnumeric?rev=17288&view=rev

Log:
2009-04-02  Morten Welinder  <terra gnome org>

	* src/format-template.c (format_template_new): Switch hash key to
	GnmCellPos so we can handle large sheets.


2009-04-02  Morten Welinder  <terra gnome org>

	* boot.c (xbase_file_open): Allocate a sheet as big as needed.



Modified:
   trunk/ChangeLog
   trunk/plugins/applix/applix-read.c
   trunk/plugins/lotus-123/lotus.c
   trunk/plugins/sc/sc.c
   trunk/plugins/sylk/sylk.c
   trunk/plugins/xbase/ChangeLog
   trunk/plugins/xbase/boot.c
   trunk/plugins/xbase/xbase.c
   trunk/src/format-template.c
   trunk/src/sheet-style.c

Modified: trunk/plugins/applix/applix-read.c
==============================================================================
--- trunk/plugins/applix/applix-read.c	(original)
+++ trunk/plugins/applix/applix-read.c	Thu Apr  2 19:12:23 2009
@@ -172,21 +172,23 @@
 #define APPLIX_SHEET_MAX_COLS 702
 
 static gboolean
-valid_col (int c)
+valid_col (Sheet const *sheet, int c)
 {
-	return c >= 0 && c < gnm_sheet_get_max_cols (NULL);
+	return c >= 0 && c < gnm_sheet_get_max_cols (sheet);
 }
 
 static gboolean
-valid_row (int r)
+valid_row (Sheet const *sheet, int r)
 {
-	return r >= 0 && r < gnm_sheet_get_max_rows (NULL);
+	return r >= 0 && r < gnm_sheet_get_max_rows (sheet);
 }
 
 static gboolean
-valid_cellpos (const GnmCellPos *cpos)
+valid_cellpos (Sheet const *sheet, const GnmCellPos *cpos)
 {
-	return valid_col (cpos->col) && valid_row (cpos->row);
+	return (sheet &&
+		valid_col (sheet, cpos->col) &&
+		valid_row (sheet, cpos->row));
 }
 
 static char const *
@@ -910,7 +912,7 @@
 	Sheet *sheet = workbook_sheet_by_name (state->wb, name);
 
 	if (sheet == NULL) {
-	  sheet = sheet_new (state->wb, name, 256, 65536);
+	  sheet = sheet_new (state->wb, name, GNM_DEFAULT_COLS, GNM_DEFAULT_ROWS);
 		workbook_sheet_attach (state->wb, sheet);
 		g_object_set (sheet, "zoom-factor", state->zoom / 100.0, NULL);
 		sheet_flag_recompute_spans (sheet);
@@ -1007,13 +1009,13 @@
 		if (!a_strncmp (buffer, "View Top Left: ")) {
 			GnmCellPos pos;
 			if (applix_parse_cellref (state, buffer+15, &sheet, &pos, ':') &&
-			    valid_cellpos (&pos))
+			    valid_cellpos (sheet, &pos))
 				sv_set_initial_top_left (sheet_get_view (sheet, state->wb_view),
 							 pos.col, pos.row);
 		} else if (!a_strncmp (buffer, "View Open Cell: ")) {
 			GnmCellPos pos;
 			if (applix_parse_cellref (state, buffer+16, &sheet, &pos, ':') &&
-			    valid_cellpos (&pos))
+			    valid_cellpos (sheet, &pos))
 				sv_selection_set (sheet_get_view (sheet, state->wb_view),
 						  &pos, pos.col, pos.row, pos.col, pos.row);
 		} else if (!a_strncmp (buffer, "View Default Column Width ")) {
@@ -1117,7 +1119,7 @@
 			return applix_parse_error (state, "Expression did not specify target cell");
 		}
 
-		if (!valid_cellpos (&pos)) {
+		if (!valid_cellpos (sheet, &pos)) {
 			gnm_style_unref (style);
 			g_warning ("Ignoring sheet contents beyond allowed range.");
 			continue;
@@ -1179,7 +1181,8 @@
 						continue;
 					}
 
-					if (!valid_cellpos (&r.start) || !valid_cellpos (&r.end)) {
+					if (!valid_cellpos (start_sheet, &r.start) ||
+					    !valid_cellpos (end_sheet, &r.end)) {
 						g_warning ("Ignoring sheet contents beyond allowed range.");
 						continue;
 					}
@@ -1555,7 +1558,7 @@
 
 	/* We only need the sheet, the visible cell, and edit pos are already set */
 	if (applix_parse_cellref (state, cur_cell_addr, &sheet, &pos, ':') &&
-	    valid_cellpos (&pos) && sheet)
+	    valid_cellpos (sheet, &pos))
 		wb_view_sheet_focus (state->wb_view, sheet);
 
 	return 0;

Modified: trunk/plugins/lotus-123/lotus.c
==============================================================================
--- trunk/plugins/lotus-123/lotus.c	(original)
+++ trunk/plugins/lotus-123/lotus.c	Thu Apr  2 19:12:23 2009
@@ -1211,7 +1211,8 @@
 	guint si, cri;
 	LotusRLDB *rldb1, *rldb0;
 	const GString *data;
-	int max = iscol ? gnm_sheet_get_max_cols (NULL) : gnm_sheet_get_max_rows (NULL);
+	Sheet *ref_sheet = workbook_sheet_by_index (state->wb, 0);
+	int max = iscol ? gnm_sheet_get_max_cols (ref_sheet) : gnm_sheet_get_max_rows (ref_sheet);
 	int start, end;
 	Sheet *sheet;
 

Modified: trunk/plugins/sc/sc.c
==============================================================================
--- trunk/plugins/sc/sc.c	(original)
+++ trunk/plugins/sc/sc.c	Thu Apr  2 19:12:23 2009
@@ -273,7 +273,7 @@
 
 
 static char const *
-sc_row_parse (char const *str, int *res, unsigned char *relative)
+sc_row_parse (char const *str, Sheet *sheet, int *res, unsigned char *relative)
 {
 	char const *end, *ptr = str;
 	long int row;
@@ -292,7 +292,7 @@
 	row = strtol (ptr, (char **)&end, 10);
 	if (ptr != end &&
 	    !g_unichar_isalnum (g_utf8_get_char (end)) && *end != '_' &&
-	    0 <= row && row  < gnm_sheet_get_max_rows (NULL)) {
+	    0 <= row && row < gnm_sheet_get_max_rows (sheet)) {
 		*res = row;
 		return end;
 	} else
@@ -313,7 +313,7 @@
 	tmp1 = col_parse (ptr, &res->a.col, &res->a.col_relative);
 	if (!tmp1)
 		return start;
-	tmp2 = sc_row_parse (tmp1, &res->a.row, &res->a.row_relative);
+	tmp2 = sc_row_parse (tmp1, pp->sheet, &res->a.row, &res->a.row_relative);
 	if (!tmp2)
 		return start;
 	if (res->a.col_relative)
@@ -330,7 +330,7 @@
 	tmp1 = col_parse (start+1, &res->b.col, &res->b.col_relative);
 	if (!tmp1)
 		return start;
-	tmp2 = sc_row_parse (tmp1, &res->b.row, &res->b.row_relative);
+	tmp2 = sc_row_parse (tmp1, pp->sheet, &res->b.row, &res->b.row_relative);
 	if (!tmp2)
 		return start;
 	if (res->b.col_relative)

Modified: trunk/plugins/sylk/sylk.c
==============================================================================
--- trunk/plugins/sylk/sylk.c	(original)
+++ trunk/plugins/sylk/sylk.c	Thu Apr  2 19:12:23 2009
@@ -621,7 +621,9 @@
 			int first, last, width;
 			if (3 == sscanf (str+1, "%d %d %d", &first, &last, &width)) {
 				/* width seems to be in characters */
-				if (first <= last && first < gnm_sheet_get_max_cols (NULL) && last < gnm_sheet_get_max_cols (NULL))
+				if (first <= last &&
+				    first < gnm_sheet_get_max_cols (state->pp.sheet) &&
+				    last < gnm_sheet_get_max_cols (state->pp.sheet))
 					while (first <= last)
 						sheet_col_set_size_pts (state->pp.sheet,
 							first++ - 1, width*7.45, TRUE);

Modified: trunk/plugins/xbase/boot.c
==============================================================================
--- trunk/plugins/xbase/boot.c	(original)
+++ trunk/plugins/xbase/boot.c	Thu Apr  2 19:12:23 2009
@@ -129,6 +129,23 @@
 
 #undef CHECK_LENGTH
 
+static void
+create_header (Sheet *sheet, XBfile *file)
+{
+	unsigned ui;
+	GnmRange r;
+	GnmStyle *bold = gnm_style_new ();
+
+	for (ui = 0 ; ui < file->fields ; ui++) {
+		GnmCell *cell = sheet_cell_fetch (sheet, ui, 0);
+		gnm_cell_set_text (cell, file->format[ui]->name);
+	}
+
+	gnm_style_set_font_bold (bold, TRUE);
+	range_init (&r, 0, 0, file->fields - 1, 0);
+	sheet_style_apply_range	(sheet, &r, bold);
+}
+
 void
 xbase_file_open (GOFileOpener const *fo, IOContext *io_context,
                  WorkbookView *wb_view, GsfInput *input)
@@ -136,13 +153,10 @@
 	Workbook  *wb;
 	XBfile	  *file;
 	XBrecord  *record;
-	char	  *name;
 	Sheet	  *sheet = NULL;
-	GnmCell	  *cell;
-	GnmValue	  *val;
-	XBfield	  *field;
 	ErrorInfo *open_error;
-	guint row, i;
+	guint cols, rows;
+	int pass;
 
 	if ((file = xbase_open (input, &open_error)) == NULL) {
 		gnumeric_io_error_info_set (io_context, error_info_new_str_with_details (
@@ -151,50 +165,57 @@
 		return;
 	}
 
+	cols = GNM_DEFAULT_COLS;
+	while (cols < file->fields)
+		cols *= 2;
+
+	rows = GNM_DEFAULT_ROWS;
 	wb = wb_view_get_workbook (wb_view);
-	name = workbook_sheet_get_free_name (wb, _("Sheet"), FALSE, TRUE);
-	sheet = sheet_new (wb, name, 256, 65536);
-	g_free (name);
-	workbook_sheet_attach (wb, sheet);
-
-	i = 0;
-	for (i = 0 ; i < file->fields ; i++) {
-		cell = sheet_cell_fetch (sheet, i, 0);
-		gnm_cell_set_text (cell, file->format [i]->name);
-	}
-	{
-		GnmRange r;
-		GnmStyle *bold = gnm_style_new ();
-		gnm_style_set_font_bold (bold, TRUE);
-		sheet_style_apply_range	(sheet,
-			range_init (&r, 0, 0, file->fields-1, 0), bold);
-	}
 
-	record = record_new (file);
-	row = 1;
-	do {
-		gboolean deleted = record_deleted (record);
-		if (deleted)
-			continue;
-		if (row >= (unsigned)gnm_sheet_get_max_rows (sheet)) {
-			/* FIXME: either we need to add new rows, if posible
-			or create a larger sheet*/
-			break;
+	for (pass = 1; pass <= 2; pass++) {
+		Sheet *sheet = NULL;
+		unsigned int row = 0;
+
+		if (pass == 2) {
+			sheet = workbook_sheet_add (wb, -1, cols, rows);
+			create_header (sheet, file);
 		}
-		for (i = 0; i < file->fields ; i++) {
-			field = record->file->format[i];
-			val = xbase_field_as_value (
-				record_get_field (record, i), field, file);
-			if (!val)
+
+		record = record_new (file);
+		do {
+			unsigned ui;
+			gboolean deleted = record_deleted (record);
+			if (deleted)
 				continue;
-			cell = sheet_cell_fetch (sheet, i, row);
-			value_set_fmt (val, field->fmt);
-			gnm_cell_set_value (cell, val);
-		}
-		row++;
-	} while (record_seek (record, SEEK_CUR, 1));
 
-	record_free (record);
+			if (pass == 1) {
+				if (row >= rows) {
+					if (rows == GNM_MAX_ROWS)
+						break;
+					rows *= 2;
+				}
+				row++;
+				continue;
+			}
+
+			row++;
+			for (ui = 0; ui < file->fields; ui++) {
+				GnmCell *cell;
+				XBfield *field = record->file->format[ui];
+				GnmValue *val = xbase_field_as_value
+					(record_get_field (record, ui),
+					 field, file);
+				if (!val)
+					continue;
+
+				cell = sheet_cell_fetch (sheet, ui, row);
+				value_set_fmt (val, field->fmt);
+				gnm_cell_set_value (cell, val);
+			}
+		} while (record_seek (record, SEEK_CUR, 1));
+		record_free (record);
+	}
+
 	xbase_close (file);
 
 	sheet_flag_recompute_spans (sheet);

Modified: trunk/plugins/xbase/xbase.c
==============================================================================
--- trunk/plugins/xbase/xbase.c	(original)
+++ trunk/plugins/xbase/xbase.c	Thu Apr  2 19:12:23 2009
@@ -300,7 +300,7 @@
 xbase_open (GsfInput *input, ErrorInfo **ret_error)
 {
 	XBfile *ans;
-	XBfield *field;
+	guint allocated = GNM_DEFAULT_COLS;
 
 	*ret_error = NULL;
 
@@ -314,12 +314,18 @@
 	}
 
 	ans->fields = 0;
-	ans->format = NULL;
-	while (ans->fields < (gnm_sheet_get_max_cols (NULL)-1) && (field = xbase_field_new (ans)) != NULL) {
-		ans->format = g_renew (XBfield *, ans->format, ans->fields + 1);
-		/* FIXME: allocate number of field formats from file size? */
+	ans->format = g_new (XBfield *, allocated);
+	while (ans->fields < GNM_MAX_COLS) {
+		XBfield *field = xbase_field_new (ans);
+		if (!field)
+			break;
+		if (ans->fields == allocated) {
+			allocated *= 2;
+			ans->format = g_renew (XBfield *, ans->format, allocated);
+		}
 		ans->format[ans->fields++] = field;
 	}
+
 	return ans;
 }
 

Modified: trunk/src/format-template.c
==============================================================================
--- trunk/src/format-template.c	(original)
+++ trunk/src/format-template.c	Thu Apr  2 19:12:23 2009
@@ -47,7 +47,7 @@
 	return !strcmp (CXML2C (a), s);
 }
 
-#define ROW_COL_KEY(row,col) GINT_TO_POINTER (row * gnm_sheet_get_max_cols (sheet) + col)
+#define ROW_COL_KEY(row,col) GINT_TO_POINTER ((guint)row * (guint)GNM_MAX_COLS + (guint)col)
 
 /******************************************************************************
  * FormatTemplateMember - Getters/setters and creation
@@ -65,9 +65,6 @@
 {
 	TemplateMember *member;
 
-	/* Sanity check for ROW_COL_KEY.  */
-	g_assert (INT_MAX / gnm_sheet_get_max_cols (NULL) > gnm_sheet_get_max_rows (NULL));
-
 	member = g_new (TemplateMember, 1);
 
 	member->col.offset	   = member->row.offset = 0;
@@ -269,7 +266,10 @@
 	ft->edges.top    = TRUE;
 	ft->edges.bottom = TRUE;
 
-	ft->table     = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)gnm_style_unref);
+	ft->table = g_hash_table_new_full ((GHashFunc)gnm_cellpos_hash,
+					   (GEqualFunc)gnm_cellpos_equal,
+					   (GDestroyNotify)g_free,
+					   (GDestroyNotify)gnm_style_unref);
 	ft->invalidate_hash = TRUE;
 
 	range_init (&ft->dimension, 0,0,0,0);
@@ -891,7 +891,8 @@
  *
  **/
 static void
-format_template_calculate (GnmFormatTemplate *origft, GnmRange const *r, PCalcCallback pc, gpointer cb_data)
+format_template_calculate (GnmFormatTemplate *origft, GnmRange const *r,
+			   PCalcCallback pc, gpointer cb_data)
 {
 	GnmFormatTemplate *ft = origft;
 	GSList *ptr;
@@ -963,10 +964,10 @@
  ******************************************************************************/
 
 static void
-cb_format_hash_style (GnmFormatTemplate *ft, GnmRange *r, GnmStyle *mstyle, GHashTable *table)
+cb_format_hash_style (GnmFormatTemplate *ft, GnmRange *r, GnmStyle *mstyle, gpointer user)
 {
+	GHashTable *table = user;
 	int row, col;
-	Sheet *sheet = NULL;
 
 	/*
 	 * Filter out undesired elements
@@ -974,9 +975,14 @@
 	mstyle = format_template_filter_style (ft, mstyle, TRUE);
 
 	for (row = r->start.row; row <= r->end.row; row++)
-		for (col = r->start.col; col <= r->end.col; col++)
-			g_hash_table_insert (table, ROW_COL_KEY (row, col),
-				gnm_style_dup (mstyle));
+		for (col = r->start.col; col <= r->end.col; col++) {
+			GnmCellPos key;
+			key.col = col;
+			key.row = row;
+			g_hash_table_insert (table,
+					     g_memdup (&key, sizeof (key)),
+					     gnm_style_dup (mstyle));
+		}
 
 	/*
 	 * Unref here, the hashtable will take care of its own
@@ -998,7 +1004,7 @@
 
 	g_return_if_fail (ft != NULL);
 
-	g_hash_table_foreach_remove (ft->table, (GHRFunc)g_direct_hash /* :-) */, NULL);
+	g_hash_table_remove_all (ft->table);
 
 	r = ft->dimension;
 
@@ -1010,7 +1016,7 @@
 		return;
 	}
 
-	format_template_calculate (ft, &r, (PCalcCallback) cb_format_hash_style, ft->table);
+	format_template_calculate (ft, &r, cb_format_hash_style, ft->table);
 }
 
 /**
@@ -1030,7 +1036,8 @@
 GnmStyle *
 format_template_get_style (GnmFormatTemplate *ft, int row, int col)
 {
-	Sheet *sheet = NULL;
+	GnmCellPos key;
+
 	g_return_val_if_fail (ft != NULL, NULL);
 	g_return_val_if_fail (ft->table != NULL, NULL);
 
@@ -1043,7 +1050,9 @@
 		format_template_recalc_hash (ft);
 	}
 
-	return g_hash_table_lookup (ft->table, ROW_COL_KEY (row, col));
+	key.col = col;
+	key.row = row;
+	return g_hash_table_lookup (ft->table, &key);
 }
 
 
@@ -1053,8 +1062,10 @@
  ******************************************************************************/
 
 static void
-cb_format_sheet_style (GnmFormatTemplate *ft, GnmRange *r, GnmStyle *mstyle, Sheet *sheet)
+cb_format_sheet_style (GnmFormatTemplate *ft, GnmRange *r, GnmStyle *mstyle, gpointer user)
 {
+	Sheet *sheet = user;
+
 	g_return_if_fail (ft != NULL);
 	g_return_if_fail (r != NULL);
 	g_return_if_fail (mstyle != NULL);
@@ -1103,7 +1114,7 @@
 {
 	for (; regions != NULL ; regions = regions->next)
 		format_template_calculate (ft, regions->data,
-			(PCalcCallback) cb_format_sheet_style, sheet);
+					   cb_format_sheet_style, sheet);
 }
 
 /******************************************************************************

Modified: trunk/src/sheet-style.c
==============================================================================
--- trunk/src/sheet-style.c	(original)
+++ trunk/src/sheet-style.c	Thu Apr  2 19:12:23 2009
@@ -1006,9 +1006,9 @@
 	CellTileType type;
 
 	g_return_if_fail (col >= 0);
-	g_return_if_fail (col < gnm_sheet_get_max_cols (NULL));
+	g_return_if_fail (col < gnm_sheet_get_max_cols (rs->sheet));
 	g_return_if_fail (row >= 0);
-	g_return_if_fail (row < gnm_sheet_get_max_rows (NULL));
+	g_return_if_fail (row < gnm_sheet_get_max_rows (rs->sheet));
 
 tail_recursion :
 	g_return_if_fail (TILE_TOP_LEVEL >= level && level >= 0);



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