1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
* boot.c: XBase support for Gnumeric
*
* Author:
* Sean Atkinson <sca20@cam.ac.uk>
**/
#include <gnumeric-config.h>
#include "xbase.h"
#include <gnumeric.h>
#include <workbook-view.h>
#include <workbook.h>
#include <cell.h>
#include <value.h>
#include <sheet.h>
#include <ranges.h>
#include <mstyle.h>
#include <sheet-style.h>
#include <goffice/goffice.h>
#include <gsf/gsf-utils.h>
#include <gnm-plugin.h>
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <string.h>
GNM_PLUGIN_MODULE_HEADER;
void xbase_file_open (GOFileOpener const *fo, GOIOContext *io_context,
WorkbookView *wb_view, GsfInput *input);
#define CHECK_LENGTH(_l) do { \
if (field->len != (_l)) { \
g_warning ("Invalid field length. File is probably corrupted."); \
g_free (s); \
return NULL; \
} \
} while (0)
static GnmValue *
xbase_field_as_value (gchar *content, XBfield *field, XBfile *file)
{
gchar *s = g_strndup (content, field->len);
GnmValue *val;
switch (field->type) {
case 'C': {
char *sutf8 = g_convert_with_iconv (g_strchomp (s), -1,
file->char_map, NULL, NULL, NULL);
if (!sutf8) {
char *t;
for (t = s; *t; t++)
if ((guchar)*t >= 0x7f)
*t = '?';
sutf8 = s;
s = NULL;
g_warning ("Unrepresentable characters replaced by '?'");
}
if (sutf8)
val = value_new_string_nocopy (sutf8);
else
val = value_new_string ("???");
g_free (s);
return val;
}
case 'N':
val = value_new_float (gnm_strto (s, NULL));
g_free (s);
return val;
case 'L':
switch (s[0]) {
case 'Y': case 'y': case 'T': case 't':
g_free (s);
return value_new_bool (TRUE);
case 'N': case 'n': case 'F': case 'f':
g_free (s);
return value_new_bool (FALSE);
case '?': case ' ':
g_free (s);
return NULL;
default:
g_warning ("Invalid logical value. File is probably corrupted.");
g_free (s);
return NULL;
}
case 'D': {
/* double check that the date is stored according to spec */
int year, month, day;
if (strcmp (s, "00000000") == 0)
val = NULL;
else if (sscanf (s, "%4d%2d%2d", &year, &month, &day) == 3 &&
g_date_valid_dmy (day, month, year)) {
GDate *date = g_date_new_dmy (day, month, year);
/* Use default date convention */
val = value_new_int (go_date_g_to_serial (date, NULL));
g_date_free (date);
} else
val = value_new_string (s);
g_free (s);
return val;
}
case 'I':
val = value_new_int (GSF_LE_GET_GINT32 (s));
g_free (s);
return val;
case 'F':
CHECK_LENGTH (sizeof (double));
val = value_new_float (GSF_LE_GET_DOUBLE (s));
g_free (s);
return val;
case 'B': {
gint64 tmp = GSF_LE_GET_GINT64 (s);
g_warning ("FIXME: \"BINARY\" field type doesn't work");
CHECK_LENGTH (sizeof (tmp));
g_free (s);
return value_new_float (tmp);
}
default: {
char *s = g_strdup_printf ("Field type '0x%02x' unsupported",
field->type);
return value_new_string_nocopy (s);
}
}
}
#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, GOIOContext *io_context,
WorkbookView *wb_view, GsfInput *input)
{
Workbook *wb;
XBfile *file;
XBrecord *record;
Sheet *sheet = NULL;
GOErrorInfo *open_error;
int rows = GNM_MAX_ROWS;
int pass;
if ((file = xbase_open (input, &open_error)) == NULL) {
go_io_error_info_set (io_context, go_error_info_new_str_with_details (
_("Error while opening xbase file."),
open_error));
return;
}
rows = 0;
wb = wb_view_get_workbook (wb_view);
for (pass = 1; pass <= 2; pass++) {
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);
}
record = record_new (file);
do {
unsigned ui;
gboolean deleted = record_deleted (record);
if (deleted)
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];
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);
rows = row;
}
xbase_close (file);
sheet_flag_recompute_spans (sheet);
}
|