gnumeric r17294 - in trunk: . plugins/xbase src src/dialogs



Author: mortenw
Date: Fri Apr  3 18:06:28 2009
New Revision: 17294
URL: http://svn.gnome.org/viewvc/gnumeric?rev=17294&view=rev

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

	* src/stf.c (stf_read_workbook_auto_csvtab, stf_read_workbook):
	Create sheet as big as needed.



Modified:
   trunk/ChangeLog
   trunk/NEWS
   trunk/plugins/xbase/boot.c
   trunk/src/dialogs/dialog-stf-format-page.c
   trunk/src/dialogs/dialog-stf-main-page.c
   trunk/src/dialogs/dialog-stf.c
   trunk/src/dialogs/dialog-stf.h
   trunk/src/sheet.c
   trunk/src/sheet.h
   trunk/src/stf-parse.c
   trunk/src/stf.c

Modified: trunk/NEWS
==============================================================================
--- trunk/NEWS	(original)
+++ trunk/NEWS	Fri Apr  3 18:06:28 2009
@@ -1,5 +1,8 @@
 Gnumeric 1.9.6
 
+Jean:
+	* Variable sheet sizes.
+
 Morten:
 	* Handle elapsed time formats better on text export.
 	* Plug leak.
@@ -13,6 +16,8 @@
 	* Fix criticals during Lotus load.
 	* Clean up Sheet object.
 	* Clean up str.h usage.
+	* Load csv and txt files into sheets as big as needed.  [Part of
+	#168875]
 
 --------------------------------------------------------------------------
 Gnumeric 1.9.5

Modified: trunk/plugins/xbase/boot.c
==============================================================================
--- trunk/plugins/xbase/boot.c	(original)
+++ trunk/plugins/xbase/boot.c	Fri Apr  3 18:06:28 2009
@@ -155,7 +155,7 @@
 	XBrecord  *record;
 	Sheet	  *sheet = NULL;
 	ErrorInfo *open_error;
-	guint cols, rows;
+	int rows = GNM_MAX_ROWS;
 	int pass;
 
 	if ((file = xbase_open (input, &open_error)) == NULL) {
@@ -165,17 +165,15 @@
 		return;
 	}
 
-	cols = GNM_DEFAULT_COLS;
-	while (cols < file->fields)
-		cols *= 2;
-
-	rows = GNM_DEFAULT_ROWS;
+	rows = 0;
 	wb = wb_view_get_workbook (wb_view);
 
 	for (pass = 1; pass <= 2; pass++) {
-		unsigned int row = 0;
+		int row = 0;
 
 		if (pass == 2) {
+			int cols = file->fields;
+			gnm_sheet_suggest_size (&cols, &rows);
 			sheet = workbook_sheet_add (wb, -1, cols, rows);
 			create_header (sheet, file);
 		}
@@ -187,17 +185,13 @@
 			if (deleted)
 				continue;
 
-			if (pass == 1) {
-				if (row >= rows) {
-					if (rows == GNM_MAX_ROWS)
-						break;
-					rows *= 2;
-				}
-				row++;
-				continue;
-			}
+			if (row >= rows)
+				break;
 
 			row++;
+			if (pass == 1)
+				continue;
+
 			for (ui = 0; ui < file->fields; ui++) {
 				GnmCell *cell;
 				XBfield *field = record->file->format[ui];
@@ -213,6 +207,7 @@
 			}
 		} while (record_seek (record, SEEK_CUR, 1));
 		record_free (record);
+		rows = row;
 	}
 
 	xbase_close (file);

Modified: trunk/src/dialogs/dialog-stf-format-page.c
==============================================================================
--- trunk/src/dialogs/dialog-stf-format-page.c	(original)
+++ trunk/src/dialogs/dialog-stf-format-page.c	Fri Apr  3 18:06:28 2009
@@ -177,7 +177,7 @@
 		pagedata->format.col_import_count--;
 		format_page_update_column_selection (pagedata);
 	} else {
-		if (pagedata->format.col_import_count < gnm_sheet_get_max_cols (NULL)) {
+		if (pagedata->format.col_import_count < GNM_MAX_COLS) {
 			pagedata->format.col_import_array[i] = TRUE;
 			pagedata->format.col_import_count++;
 			format_page_update_column_selection (pagedata);
@@ -185,8 +185,8 @@
 			char *msg = g_strdup_printf( 
 				ngettext("A maximum of %d column can be imported.",
 					 "A maximum of %d columns can be imported.",
-					 gnm_sheet_get_max_cols (NULL)), 
-				gnm_sheet_get_max_cols (NULL));
+					 GNM_MAX_COLS), 
+				GNM_MAX_COLS);
 			gtk_toggle_button_set_active (togglebutton, FALSE);
 			go_gtk_notice_dialog (GTK_WINDOW (pagedata->dialog),
 					 GTK_MESSAGE_WARNING, msg);
@@ -210,8 +210,8 @@
 		if (!pagedata->format.col_import_array[i]) {
 			GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
 			GtkWidget *w = g_object_get_data (G_OBJECT (column), "checkbox");
-			if (!(pagedata->format.col_import_count < gnm_sheet_get_max_cols (NULL)))
-				return;
+			if (pagedata->format.col_import_count >= GNM_MAX_COLS)
+				break;
 			gtk_widget_hide (w);
 			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
 			/* Note this caused a signal to be send that sets the */
@@ -498,7 +498,7 @@
 			pagedata->format.col_import_count++;
 	for (i = old_part; 
 	     i < pagedata->format.col_import_array_len; i++) 
-		if (pagedata->format.col_import_count < gnm_sheet_get_max_cols (NULL)) {
+		if (pagedata->format.col_import_count < GNM_MAX_COLS) {
 			pagedata->format.col_import_array[i] = TRUE;
 			pagedata->format.col_import_count++;
 		} else {
@@ -510,7 +510,7 @@
 	if (old_part < renderdata->colcount)
 		msg = g_strdup_printf 
 			(_("A maximum of %d columns can be imported."), 
-			 gnm_sheet_get_max_cols (NULL));
+			 GNM_MAX_COLS);
 
 	for (i = old_part; i < renderdata->colcount; i++) {
 		GtkTreeViewColumn *column =

Modified: trunk/src/dialogs/dialog-stf-main-page.c
==============================================================================
--- trunk/src/dialogs/dialog-stf-main-page.c	(original)
+++ trunk/src/dialogs/dialog-stf-main-page.c	Fri Apr  3 18:06:28 2009
@@ -149,7 +149,7 @@
 	}
 
 	stoplimit = MIN ((int)renderdata->lines->len,
-			 startrow + (gnm_sheet_get_max_rows (NULL) - 1));
+			 startrow + (GNM_MAX_ROWS - 1));
 	if (stoprow > stoplimit) {
 		stoprow = stoplimit;
 		gtk_spin_button_set_value (data->main.main_stoprow, stoprow);

Modified: trunk/src/dialogs/dialog-stf.c
==============================================================================
--- trunk/src/dialogs/dialog-stf.c	(original)
+++ trunk/src/dialogs/dialog-stf.c	Fri Apr  3 18:06:28 2009
@@ -318,6 +318,7 @@
 		dialogresult->encoding = pagedata.encoding;
 		pagedata.encoding = NULL;
 
+		dialogresult->colcount = pagedata.format.col_import_count;
 		dialogresult->rowcount = pagedata.rowcount;
 
 		dialogresult->parseoptions = pagedata.parseoptions;

Modified: trunk/src/dialogs/dialog-stf.h
==============================================================================
--- trunk/src/dialogs/dialog-stf.h	(original)
+++ trunk/src/dialogs/dialog-stf.h	Fri Apr  3 18:06:28 2009
@@ -129,6 +129,7 @@
 
 	char              *text;          /* Decoded text.  */
 	int                rowcount;      /* Number of resulting rows.  */
+	int                colcount;      /* Number of resulting rows.  */
 	StfParseOptions_t *parseoptions;  /* parse options */
 } DialogStfResult_t;
 

Modified: trunk/src/sheet.c
==============================================================================
--- trunk/src/sheet.c	(original)
+++ trunk/src/sheet.c	Fri Apr  3 18:06:28 2009
@@ -1038,6 +1038,25 @@
 }
 
 /* ------------------------------------------------------------------------- */
+
+gboolean
+gnm_sheet_suggest_size (int *cols, int *rows)
+{
+	gboolean bad = (*cols > GNM_MAX_COLS || *rows > GNM_MAX_ROWS);
+	int c = GNM_DEFAULT_COLS;
+	int r = GNM_DEFAULT_ROWS;
+
+	while (c < *cols && c < GNM_MAX_COLS)
+		c *= 2;
+	*cols = c;
+
+	while (r < *rows && r < GNM_MAX_ROWS)
+		r *= 2;
+	*rows = r;
+
+	return bad;
+}
+
 /**
  * sheet_new_with_type :
  * @wb      : #Workbook

Modified: trunk/src/sheet.h
==============================================================================
--- trunk/src/sheet.h	(original)
+++ trunk/src/sheet.h	Fri Apr  3 18:06:28 2009
@@ -125,6 +125,8 @@
 Sheet    *sheet_dup		 (Sheet const *source_sheet);
 void      sheet_destroy_contents (Sheet *sheet);
 
+gboolean  gnm_sheet_suggest_size (int *cols, int *rows);
+
 int gnm_sheet_get_max_rows (Sheet const *sheet);
 int gnm_sheet_get_max_cols (Sheet const *sheet);
 #define gnm_sheet_get_last_col(sheet) (gnm_sheet_get_max_cols(sheet) - 1)

Modified: trunk/src/stf-parse.c
==============================================================================
--- trunk/src/stf-parse.c	(original)
+++ trunk/src/stf-parse.c	Fri Apr  3 18:06:28 2009
@@ -88,12 +88,6 @@
 	return (int)a->len;
 }
 
-static inline int
-my_gptrarray_len (GPtrArray const *a)
-{
-	return (int)a->len;
-}
-
 static int
 compare_terminator (char const *s, StfParseOptions_t *parseoptions)
 {
@@ -840,7 +834,7 @@
 	while (*src.position != '\0' && src.position < data_end) {
 		GPtrArray *line;
 
-		if (row == gnm_sheet_get_max_rows (NULL)) {
+		if (row == GNM_MAX_ROWS) {
 			parseoptions->rows_exceeded = TRUE;
 			break;
 		}
@@ -1224,11 +1218,12 @@
 		 char const *data, char const *data_end,
 		 Sheet *sheet, int start_col, int start_row)
 {
-	int row, col;
-	unsigned int lrow, lcol;
+	int row;
+	unsigned int lrow;
 	GODateConventions const *date_conv;
 	GStringChunk *lines_chunk;
-	GPtrArray *lines, *line;
+	GPtrArray *lines;
+	gboolean result = TRUE;
 
 	SETUP_LOCALE_SWITCH;
 
@@ -1236,46 +1231,71 @@
 	g_return_val_if_fail (data != NULL, FALSE);
 	g_return_val_if_fail (IS_SHEET (sheet), FALSE);
 
-	START_LOCALE_SWITCH;
-
 	date_conv = workbook_date_conv (sheet->workbook);
 
 	if (!data_end)
 		data_end = data + strlen (data);
+
 	lines_chunk = g_string_chunk_new (100 * 1024);
 	lines = stf_parse_general (parseoptions, lines_chunk, data, data_end);
 	if (lines == NULL)
-		return FALSE;
-	for (row = start_row, lrow = 0; lrow < lines->len ; row++, lrow++) {
+		result = FALSE;
+
+	START_LOCALE_SWITCH;
+	for (row = start_row, lrow = 0;
+	     result && lrow < lines->len;
+	     row++, lrow++) {
+		GPtrArray *line;
+		int col;
+		unsigned int lcol;
+
+		if (row >= gnm_sheet_get_max_rows (sheet)) {
+			if (!parseoptions->rows_exceeded) {
+				/* FIXME: What locale?  */
+				g_warning (_("There are more rows of data than "
+					     "there is room for in the sheet.  Extra "
+					     "rows will be ignored."));
+				parseoptions->rows_exceeded = TRUE;
+			}
+			break;
+		}
+
 		col = start_col;
 		line = g_ptr_array_index (lines, lrow);
 
-		for (lcol = 0; lcol < line->len; lcol++)
-			if (parseoptions->col_import_array == NULL ||
-			    parseoptions->col_import_array_len <= lcol ||
-			    parseoptions->col_import_array[lcol]) {
-				if (col >= gnm_sheet_get_max_cols (sheet)) {
-					if (!parseoptions->cols_exceeded) {
-						g_warning (_("There are more columns of data than "
-							     "there is room for in the sheet.  Extra "
-							     "columns will be ignored."));
-						parseoptions->cols_exceeded = TRUE;
-					}
-				} else {
-					char const *text = g_ptr_array_index (line, lcol);
-					if (text && *text)
-						stf_cell_set_text (
-							sheet_cell_fetch (sheet, col, row),
-							text);
+		for (lcol = 0; lcol < line->len; lcol++) {
+			gboolean want_col =
+				(parseoptions->col_import_array == NULL ||
+				 parseoptions->col_import_array_len <= lcol ||
+				 parseoptions->col_import_array[lcol]);
+			if (!want_col)
+				continue;
+
+			if (col >= gnm_sheet_get_max_cols (sheet)) {
+				if (!parseoptions->cols_exceeded) {
+					/* FIXME: What locale?  */
+					g_warning (_("There are more columns of data than "
+						     "there is room for in the sheet.  Extra "
+						     "columns will be ignored."));
+					parseoptions->cols_exceeded = TRUE;
+				}
+				break;
+			} else {
+				char const *text = g_ptr_array_index (line, lcol);
+				if (text && *text) {
+					GnmCell *cell = sheet_cell_fetch (sheet, col, row);
+					stf_cell_set_text (cell, text);
 				}
-				col++;
 			}
+			col++;
+		}
 	}
+	END_LOCALE_SWITCH;
 
-	stf_parse_general_free (lines);
+	if (lines)
+		stf_parse_general_free (lines);
 	g_string_chunk_free (lines_chunk);
-	END_LOCALE_SWITCH;
-	return TRUE;
+	return result;
 }
 
 GnmCellRegion *

Modified: trunk/src/stf.c
==============================================================================
--- trunk/src/stf.c	(original)
+++ trunk/src/stf.c	Fri Apr  3 18:06:28 2009
@@ -178,11 +178,9 @@
 		   IOContext *context, gpointer wbv, GsfInput *input)
 {
 	DialogStfResult_t *dialogresult = NULL;
-	char *name, *nameutf8;
-	char *data;
+	char *name, *nameutf8 = NULL;
+	char *data = NULL;
 	size_t data_len;
-	Sheet *sheet, *old_sheet;
-	Workbook *book;
 
 	/* FIXME : how to do this cleanly ? */
 	if (!IS_WBC_GTK (context->impl))
@@ -193,37 +191,37 @@
 	g_free (name);
 	if (!nameutf8) {
 		g_warning ("Failed to convert filename to UTF-8.  This shouldn't happen here.");
-		return;
+		goto out;
 	}
 
 	data = stf_preparse (GO_CMD_CONTEXT (context), input, &data_len);
-	if (!data) {
-		g_free (nameutf8);
-		return;
-	}
-
-	/* Add Sheet */
-	book = wb_view_get_workbook (wbv);
-	old_sheet = wb_view_cur_sheet (wbv);
-	sheet = sheet_new (book, nameutf8,
-			   gnm_sheet_get_max_cols (old_sheet),
-			   gnm_sheet_get_max_rows (old_sheet));
-	workbook_sheet_attach (book, sheet);
+	if (!data)
+		goto out;
 
 	dialogresult = stf_dialog (WBC_GTK (context->impl),
 				   enc, FALSE, NULL, FALSE,
 				   nameutf8, data, data_len);
-	if (dialogresult != NULL && stf_store_results (dialogresult, sheet, 0, 0)) {
-		workbook_recalc_all (book);
-		sheet_queue_respan (sheet, 0, gnm_sheet_get_max_rows (sheet)-1);
-	} else {
-		/* the user has cancelled */
-                /* the caller should notice that we have no sheets */
-		workbook_sheet_delete (sheet);
+	if (dialogresult != NULL) {
+		Workbook *book = wb_view_get_workbook (wbv);
+		int cols = dialogresult->colcount, rows = dialogresult->rowcount;
+		Sheet *sheet;
+
+		gnm_sheet_suggest_size (&cols, &rows);
+		sheet = sheet_new (book, nameutf8, cols, rows);
+		workbook_sheet_attach (book, sheet);
+		if (stf_store_results (dialogresult, sheet, 0, 0)) {
+			workbook_recalc_all (book);
+			sheet_queue_respan (sheet, 0, gnm_sheet_get_last_row (sheet));
+		} else {
+			/* the user has cancelled */
+			/* the caller should notice that we have no sheets */
+			workbook_sheet_delete (sheet);
+		}
 	}
 
-	g_free (data);
+ out:
 	g_free (nameutf8);
+	g_free (data);
 	if (dialogresult != NULL)
 		stf_dialog_result_free (dialogresult);
 }
@@ -352,6 +350,9 @@
 	size_t data_len;
 	StfParseOptions_t *po;
 	const char *gsfname;
+	int cols, rows, i;
+	GStringChunk *lines_chunk;
+	GPtrArray *lines;
 
 	g_return_if_fail (context != NULL);
 	g_return_if_fail (wbv != NULL);
@@ -387,17 +388,27 @@
 			po = stf_parse_options_guess (utf8data);
 	}
 
+	lines_chunk = g_string_chunk_new (100 * 1024);
+	lines = stf_parse_general (po, lines_chunk,
+				   utf8data, utf8data + strlen (utf8data));
+	rows = lines->len;
+	cols = 0;
+	for (i = 0; i < rows; i++) {
+		GPtrArray *line = g_ptr_array_index (lines, i);
+		cols = MAX (cols, (int)line->len);
+	}
+	gnm_sheet_suggest_size (&cols, &rows);
+	stf_parse_general_free (lines);
+	g_string_chunk_free (lines_chunk);
+
 	name = g_path_get_basename (gsfname);
-	sheet = sheet_new (book, name,
-			   gnm_sheet_get_max_cols (old_sheet),
-			   gnm_sheet_get_max_rows (old_sheet));
+	sheet = sheet_new (book, name, cols, rows);
 	g_free (name);
 	workbook_sheet_attach (book, sheet);
 
-
 	if (stf_parse_sheet (po, utf8data, NULL, sheet, 0, 0)) {
 		workbook_recalc_all (book);
-		sheet_queue_respan (sheet, 0, gnm_sheet_get_max_rows (sheet)-1);
+		sheet_queue_respan (sheet, 0, gnm_sheet_get_last_row (sheet));
 		if (po->cols_exceeded || po->rows_exceeded) {
 			const char *msg =
 				_("Some data did not fit on the sheet and was dropped.");



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