Commit 661da0e0 authored by Jody Goldberg's avatar Jody Goldberg Committed by Jody Goldberg

Have the file loaders return error messages.

1999-12-30  Jody Goldberg <jgoldberg@home.com>

	* src/file.c (workbook_load_from) : The 'open' functions return error
	  strings now.  Success == NULL, standard error == "", custom error
	  which needs to be freed is everything else.
	(workbook_try_read) : An optional argument in which to store error
	  messages returned by the loaders.
	(file_error_message) : Support the custom error messages.
	(workbook_read) : Ditto.

	* src/xml-io.c (xml_workbook_read) : Return simple error strings.
parent 9fa90372
1999-12-30 Jody Goldberg <jgoldberg@home.com>
* src/file.c (workbook_load_from) : The 'open' functions return error
strings now. Success == NULL, standard error == "", custom error
which needs to be freed is everything else.
(workbook_try_read) : An optional argument in which to store error
messages returned by the loaders.
(file_error_message) : Support the custom error messages.
(workbook_read) : Ditto.
* src/xml-io.c (xml_workbook_read) : Return simple error strings.
1999-12-29 Ariel Rios <jarios@usa.net>
* doc/guile-gnumeric.txt: Documentation for the guile plugin was added.
......
......@@ -23,6 +23,8 @@ Frank:
Jody:
* Begin exception handling for corba vs gui.
* Add some simple version protection for plugins.
* Improve error reporting for file loading.
* Explicitly disable passord protected XL spreadsheets.
* Bug fixing.
Cort:
......
1999-12-30 Jody Goldberg <jgoldberg@home.com>
* src/file.c (workbook_load_from) : The 'open' functions return error
strings now. Success == NULL, standard error == "", custom error
which needs to be freed is everything else.
(workbook_try_read) : An optional argument in which to store error
messages returned by the loaders.
(file_error_message) : Support the custom error messages.
(workbook_read) : Ditto.
* src/xml-io.c (xml_workbook_read) : Return simple error strings.
1999-12-29 Ariel Rios <jarios@usa.net>
* doc/guile-gnumeric.txt: Documentation for the guile plugin was added.
......
1999-12-30 Jody Goldberg <jgoldberg@home.com>
* dif.c (dif_read_workbook) : Preliminary error reporting support.
1999-11-21 Jeff Garzik <jgarzik@mandrakesoft.com>
* dif.c (dif_cleanup_plugin): free pd->title
......
......@@ -236,23 +236,20 @@ g_warning("DIF SUCCESS");
# define MAP_FAILED -1
#endif
static gboolean
static char *
dif_read_workbook (Workbook *book, char const *filename)
{
/* TODO : When there is a reasonable error reporting
* mechanism use it and put all the error code back
*/
gboolean result = FALSE;
char *result = NULL;
int len;
struct stat sbuf;
char const *data;
int const fd = open(filename, O_RDONLY);
if (fd < 0)
return FALSE;
return g_strdup (g_strerror(errno));
if (fstat(fd, &sbuf) < 0) {
close (fd);
return FALSE;
return g_strdup (g_strerror(errno));
}
len = sbuf.st_size;
......@@ -269,16 +266,14 @@ dif_read_workbook (Workbook *book, char const *filename)
workbook_attach_sheet (book, src.sheet);
g_free (name);
result = dif_parse_sheet (&src);
if (!result)
{
g_warning("DIF : Failed to load sheet");
if (!dif_parse_sheet (&src)) {
workbook_detach_sheet (book, src.sheet, TRUE);
result = g_strdup(_(("DIF : Failed to load sheet")));
}
munmap((char *)data, len);
}
} else
result = g_strdup (_("Unable to mmap the file"));
close(fd);
return result;
......
1999-12-30 Jody Goldberg <jgoldberg@home.com>
* ms-excel-read.c (ms_excel_workbook_new) : Return error messages.
(ms_excel_read_workbook) : If a file is password protected do NOT
attempt to read it.
* boot.c (excel_load) : return error messages.
1999-12-23 Jon K Hellan <hellan@acm.org>
* ms-excel-write.c (write_bits): Use "The Free Software
......
......@@ -47,22 +47,24 @@ excel_probe (const char *filename)
return FALSE;
}
static gboolean
static char *
excel_load (Workbook *wb, const char *filename)
{
MsOle *f;
gboolean ret;
MsOleErr result;
MsOleErr ole_error;
MsOle *f;
char *workbook_error;
result = ms_ole_open (&f, filename);
if (result != MS_OLE_ERR_OK) {
ole_error = ms_ole_open (&f, filename);
if (ole_error != MS_OLE_ERR_OK) {
ms_ole_destroy (&f);
return FALSE;
/* FIXME : The null string indicates using the default message
* We need a more detailed message from ole_open */
return "";
}
printf ("Opening '%s' ", filename);
ret = ms_excel_read_workbook (wb, f);
if (ret) {
workbook_error = ms_excel_read_workbook (wb, f);
if (workbook_error == NULL) {
char *name = g_strconcat (filename, ".gnumeric", NULL);
ms_summary_read (f, wb->summary_info);
......@@ -75,7 +77,7 @@ excel_load (Workbook *wb, const char *filename)
ms_ole_destroy (&f);
return ret;
return workbook_error;
}
......
......@@ -19,7 +19,7 @@ typedef enum _eBiff_version { eBiffV2=2, eBiffV3=3,
eBiffV8=8, /* Excel 97 */
eBiffVUnknown=0} eBiff_version ;
extern gboolean ms_excel_read_workbook (Workbook *wb, MsOle *file);
extern char * ms_excel_read_workbook (Workbook *wb, MsOle *file);
extern int ms_excel_write_workbook (MsOle *file, Workbook *wb,
eBiff_version ver);
......
......@@ -3425,7 +3425,7 @@ ms_excel_read_supporting_wb (BIFF_BOF_DATA *ver, BiffQuery *q)
#endif
}
gboolean
char *
ms_excel_read_workbook (Workbook *workbook, MsOle *file)
{
ExcelWorkbook *wb = NULL;
......@@ -3434,10 +3434,14 @@ ms_excel_read_workbook (Workbook *workbook, MsOle *file)
BiffQuery *q;
BIFF_BOF_DATA *ver = 0;
int current_sheet = 0;
char *problem_loading = NULL;
cell_deep_freeze_redraws ();
/* Find that book file */
/* Look for workbook before book so that we load the office97 format rather than
* office5 when there are multiple streams.
*/
result = ms_ole_stream_open (&stream, file, "/", "workbook", 'r');
if (result != MS_OLE_ERR_OK) {
ms_ole_stream_close (&stream);
......@@ -3445,14 +3449,13 @@ ms_excel_read_workbook (Workbook *workbook, MsOle *file)
result = ms_ole_stream_open (&stream, file, "/", "book", 'r');
if (result != MS_OLE_ERR_OK) {
ms_ole_stream_close (&stream);
g_warning ("No Excel stream found: wierd");
return FALSE;
return g_strdup (_("No book or workbook streams found."));
}
}
q = ms_biff_query_new (stream);
while (ms_biff_query_next (q)) {
while (problem_loading == NULL && ms_biff_query_next (q)) {
#ifndef NO_DEBUG_EXCEL
if (ms_excel_read_debug > 5) {
printf ("Opcode : 0x%x\n", q->opcode);
......@@ -3782,6 +3785,11 @@ ms_excel_read_workbook (Workbook *workbook, MsOle *file)
case BIFF_PASSWORD :
break;
case BIFF_FILEPASS :
/* All records after this are encrypted */
problem_loading = g_strdup (_("Password protected workbooks are not supported yet."));
break;
case (BIFF_STYLE & 0xff) : /* Why here and not as 93 */
break;
......@@ -3904,9 +3912,12 @@ ms_excel_read_workbook (Workbook *workbook, MsOle *file)
cell_deep_thaw_redraws ();
if (wb) {
workbook_recalc (wb->gnum_wb);
/* If we were forced to stop then the load failed */
if (problem_loading == NULL)
workbook_recalc (wb->gnum_wb);
ms_excel_workbook_destroy (wb);
return TRUE;
return problem_loading;
}
return FALSE;
return "";
}
......@@ -30,9 +30,10 @@ typedef struct {
static char *
csv_parse_field (FileSource_t *src)
csv_parse_field (FileSource_t *src, Cell *cell)
{
GString *res = NULL;
char *field;
char const *cur = src->cur;
char const delim =
(*cur != '"' && *cur != '\'')
......@@ -49,10 +50,9 @@ csv_parse_field (FileSource_t *src)
} else
res = g_string_new ("");
if (!cur[1]) {
g_warning ("CSV : Unexpected end of line at line %d", src->line);
return NULL;
}
if (!cur[1])
return g_strdup_printf (_("Invalid CSV file unexpected end of line at line %d"),
src->line);
/* \r\n is a single embedded newline, ignore the \r */
if (cur [1] == '\r' && cur [2] == '\n')
......@@ -77,77 +77,71 @@ csv_parse_field (FileSource_t *src)
}
if (res != NULL) {
char *tmp = res->str;
field = res->str;
g_string_free (res, FALSE);
return tmp;
}
} else
field = g_strndup (start, cur-start);
cell_set_text_simple (cell, field);
g_free (field);
return g_strndup (start, cur-start);
return NULL;
}
static gboolean
static char *
csv_parse_sheet (FileSource_t *src)
{
int row, col;
char *field;
for (row = 0 ; *src->cur ; ++row, ++src->line, ++(src->cur)) {
if (row >= SHEET_MAX_ROWS) {
g_warning ("CSV : Invalid CSV file has more than the maximum number of rows %d",
SHEET_MAX_ROWS);
return FALSE;
}
if (row >= SHEET_MAX_ROWS)
return g_strdup_printf (_("Invalid CSV file has more than the maximum number of rows %d"),
SHEET_MAX_ROWS);
for (col = 0 ; *src->cur && *src->cur != '\n' && *src->cur != '\r' ; ++col) {
if (col >= SHEET_MAX_COLS) {
g_warning ("CSV : Invalid CSV file has more than the maximum number of columns %d",
SHEET_MAX_COLS);
return FALSE;
}
if (NULL != (field = csv_parse_field (src))) {
Cell *cell = sheet_cell_new (src->sheet, col, row);
cell_set_text_simple (cell, field);
g_free (field);
} else
return FALSE;
char *error = NULL;
Cell *cell;
if (col >= SHEET_MAX_COLS)
return g_strdup_printf (_("Invalid CSV file has more than the maximum number of columns %d"),
SHEET_MAX_COLS);
cell = sheet_cell_new (src->sheet, col, row);
if (NULL != (error = csv_parse_field (src, cell)))
return error;
}
/* \r\n is a single end of line, ignore the \r */
if (src->cur [0] == '\r' && src->cur [1] == '\n')
src->cur++;
}
return TRUE;
return NULL;
}
#ifndef MAP_FAILED
# define MAP_FAILED -1
#endif
static gboolean
static char *
csv_read_workbook (Workbook *book, char const *filename)
{
/* TODO : When there is a reasonable error reporting
* mechanism use it and put all the error code back
*/
gboolean result = FALSE;
char *result = NULL;
int len;
struct stat sbuf;
char const *data;
int const fd = open(filename, O_RDONLY);
int const fd = open(filename, O_RDONLY);
if (fd < 0)
return FALSE;
return g_strdup (g_strerror(errno));
if (fstat(fd, &sbuf) < 0) {
close (fd);
return FALSE;
return g_strdup (g_strerror(errno));
}
len = sbuf.st_size;
if (MAP_FAILED != (data = (char const *) (mmap(0, len, PROT_READ,
MAP_PRIVATE, fd, 0)))) {
FileSource_t src;
char * name = g_strdup_printf (_("Imported %s"), g_basename (filename));
char *name = g_strdup_printf (_("Imported %s"), g_basename (filename));
src.data = data;
src.cur = data;
......@@ -159,17 +153,17 @@ csv_read_workbook (Workbook *book, char const *filename)
result = csv_parse_sheet (&src);
if (!result)
if (result != NULL)
workbook_detach_sheet (book, src.sheet, TRUE);
munmap((char *)data, len);
}
} else
result = g_strdup (_("Unable to mmap the file"));
close(fd);
return result;
}
static int
csv_write_cell (FILE *f, Cell *cell, int col, int row)
{
......
......@@ -35,209 +35,196 @@
char* help_execSQL = "execSQL: Execute SQL statement and display values";
static gchar*
display_recordset(Gda_Recordset* rs, Sheet* sheet, gint col, gint row)
display_recordset (Gda_Recordset* rs, Sheet* sheet, gint col, gint row)
{
gint i;
Cell* cell;
gchar* retval;
Gda_Field* field;
sheet = workbook_get_current_sheet(current_workbook);
field = gda_recordset_field_idx(rs, 0);
retval = gda_field_name(field);
for ( i = 1; i < gda_recordset_rowsize(rs); i++)
{
gchar* field_name;
field = gda_recordset_field_idx(rs, i);
field_name = gda_field_name(field);
cell = sheet_cell_fetch(sheet, col+i, row);
cell_set_text(cell, field_name);
}
while(1)
{
row++;
gda_recordset_move(rs, 1, 0);
if (gda_recordset_eof(rs))
break;
for (i = 0; i < gda_recordset_rowsize(rs); i++)
{
gchar value_bfr[128];
field = gda_recordset_field_idx(rs, i);
if (!gda_field_isnull(field))
{
gda_stringify_value(value_bfr, sizeof(value_bfr), field);
cell = sheet_cell_fetch(sheet, col+i, row);
g_print("Setting cell (%d/%d) to '%s'\n", col+i, row, value_bfr);
cell_set_text(cell, value_bfr);
}
gint i;
Cell* cell;
gchar* retval;
Gda_Field* field;
sheet = workbook_get_current_sheet(current_workbook);
field = gda_recordset_field_idx(rs, 0);
retval = gda_field_name(field);
for ( i = 1; i < gda_recordset_rowsize(rs); i++) {
gchar* field_name;
field = gda_recordset_field_idx(rs, i);
field_name = gda_field_name(field);
cell = sheet_cell_fetch(sheet, col+i, row);
cell_set_text(cell, field_name);
}
}
return retval;
while(1) {
row++;
gda_recordset_move(rs, 1, 0);
if (gda_recordset_eof(rs))
break;
for (i = 0; i < gda_recordset_rowsize(rs); i++) {
gchar value_bfr[128];
field = gda_recordset_field_idx(rs, i);
if (!gda_field_isnull(field)) {
gda_stringify_value(value_bfr, sizeof(value_bfr), field);
cell = sheet_cell_fetch(sheet, col+i, row);
g_print("Setting cell (%d/%d) to '%s'\n", col+i, row, value_bfr);
cell_set_text(cell, value_bfr);
}
}
}
return retval;
}
static Value*
execSQL(void* sheet, GList* expr_node_list, int eval_col, int eval_row, char **error_string)
execSQL (void* sheet, GList* expr_node_list, int eval_col, int eval_row, char **error_string)
{
Value* result;
Gda_Connection* cnc;
Gda_Command* cmd;
Gda_Recordset* rs;
gulong reccount;
ExprTree* node;
gint parm_idx;
gchar* db_name;
gchar* dsn;
gchar* user;
gchar* password;
GString* stmt;
gint rc;
gchar bfr[128];
gchar* provider;
if (g_list_length(expr_node_list) < 4)
{
*error_string = "Format: Databasource, Username, Password, Statement with cell references";
return NULL;
}
stmt = g_string_new("");
node = expr_node_list->data;
g_print("1. node->oper = %d\n", node->oper);
db_name = expr_decode_tree(node, sheet, eval_col, eval_row);
db_name[strlen(db_name)-1] = '\0';
g_print("1. node: value = '%s'\n", &db_name[1]);
expr_node_list = g_list_next(expr_node_list);
node = expr_node_list->data;
g_print("2. node->oper = %d\n", node->oper);
user = expr_decode_tree(node, sheet, eval_col, eval_row);
user[strlen(user)-1] = '\0';
g_print("2. node: value = '%s'\n", &user[1]);
expr_node_list = g_list_next(expr_node_list);
node = expr_node_list->data;
g_print("3.node->oper = %d\n", node->oper);
password = expr_decode_tree(node, sheet, eval_col, eval_row);
password[strlen(password)-1] = '\0';
g_print("3. node: value = '%s'\n", &password[1]);
expr_node_list = g_list_next(expr_node_list);
parm_idx = 0;
cmd = gda_command_new();
while (expr_node_list)
{
node = expr_node_list->data;
g_print("parameter_node %d: oper = %d\n", parm_idx, node->oper);
if (node->oper == OPER_CONSTANT)
{
if (node->u.constant->type != VALUE_STRING)
{
g_free(user);
g_free(password);
g_free(db_name);
g_string_free(stmt, 1);
*error_string = "Statement is no string\n";
return NULL;
}
g_string_append(stmt, node->u.constant->v.str->str);
Value* result;
Gda_Connection* cnc;
Gda_Command* cmd;
Gda_Recordset* rs;
gulong reccount;
ExprTree* node;
gint parm_idx;
gchar* db_name;
gchar* dsn;
gchar* user;
gchar* password;
GString* stmt;
gint rc;
gchar bfr[128];
gchar* provider;
if (g_list_length(expr_node_list) < 4) {
*error_string = "Format: Databasource, Username, Password, Statement with cell references";
return NULL;
}
if (node->oper == OPER_VAR)
{
GDA_Value* gda_value;
Cell* parameter_cell;
gint cell_row;
gint cell_col;
g_string_append(stmt, " ? ");
g_print("cellref->row = %d, relative = %d\n",
node->u.ref.row, node->u.ref.row_relative);
g_print("cellreg->col = %d, relative = %d\n",
node->u.ref.col, node->u.ref.col_relative);
if (node->u.ref.row_relative)
cell_row = eval_row + node->u.ref.row;
else
cell_row = node->u.ref.row;
if (node->u.ref.col_relative)
cell_col = eval_col + node->u.ref.col;
else
cell_col = node->u.ref.col;
parameter_cell = sheet_cell_get(sheet, cell_col, cell_row);
gda_value = GDA_Value__alloc();
gda_value->_d = GDA_TypeVarchar;
gda_value->_u.lvc = parameter_cell->text->str;
gda_command_create_parameter(cmd, "param from gnumeric",
GDA_PARAM_IN, gda_value);
stmt = g_string_new("");
node = expr_node_list->data;
g_print("1. node->oper = %d\n", node->oper);
db_name = expr_decode_tree(node, sheet, eval_col, eval_row);
db_name[strlen(db_name)-1] = '\0';
g_print("1. node: value = '%s'\n", &db_name[1]);
expr_node_list = g_list_next(expr_node_list);
node = expr_node_list->data;
g_print("2. node->oper = %d\n", node->oper);
user = expr_decode_tree(node, sheet, eval_col, eval_row);
user[strlen(user)-1] = '\0';
g_print("2. node: value = '%s'\n", &user[1]);
expr_node_list = g_list_next(expr_node_list);
node = expr_node_list->data;
g_print("3.node->oper = %d\n", node->oper);
password = expr_decode_tree(node, sheet, eval_col, eval_row);
password[strlen(password)-1] = '\0';
g_print("3. node: value = '%s'\n", &password[1]);
expr_node_list = g_list_next(expr_node_list);
parm_idx = 0;
cmd = gda_command_new();
while (expr_node_list) {
node = expr_node_list->data;
g_print("parameter_node %d: oper = %d\n", parm_idx, node->oper);
if (node->oper == OPER_CONSTANT) {
if (node->u.constant->type != VALUE_STRING) {
g_free(user);
g_free(password);
g_free(db_name);
g_string_free(stmt, 1);
*error_string = "Statement is no string\n";
return NULL;
}
g_string_append(stmt, node->u.constant->v.str->str);
}
if (node->oper == OPER_VAR) {
GDA_Value* gda_value;
Cell* parameter_cell;
gint cell_row;
gint cell_col;