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 214 215 216 217 218 219 220 221
|
/* 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 <glib/gi18n.h>
#include <gnumeric.h>
#include "xbase.h"
#include <workbook-view.h>
#include <workbook.h>
#include <cell.h>
#include <value.h>
#include <plugin.h>
#include <plugin-util.h>
#include <module-plugin-defs.h>
#include <sheet.h>
#include <datetime.h>
#include <ranges.h>
#include <mstyle.h>
#include <sheet-style.h>
#include <io-context.h>
#include <stdlib.h>
GNUMERIC_MODULE_PLUGIN_INFO_DECL;
void xbase_file_open (GnmFileOpener const *fo, IOContext *io_context,
WorkbookView *wb_view, GsfInput *input);
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
# define XB_GETDOUBLE(p) (*((double*)(p)))
#if 0
# define XB_SETDOUBLE(p,q) (*((double*)(p))=(q))
#endif
#else
# define XB_GETDOUBLE(p) (xb_getdouble(p))
#if 0
# define XB_SETDOUBLE(p,q) (xb_setdouble(p,q))
#endif
static double
xb_getdouble (guint8 const *p)
{
double d;
int i;
guint8 *t = (guint8 *)&d;
int sd = sizeof (d);
for (i = 0; i < sd; i++)
t[i] = p[sd - 1 - i];
return d;
}
#if 0
static void
xb_setdouble (guint8 *p, double d)
{
int i;
guint8 *t = (guint8 *)&d;
int sd = sizeof (d);
for (i = 0; i < sd; i++)
p[sd - 1 - i] = t[i];
}
#endif
#endif
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': {
if (file->char_map != (GIConv)-1)
val = value_new_string_nocopy (
g_convert_with_iconv (g_strchomp (s), -1,
file->char_map, NULL, NULL, NULL));
else
val = value_new_string_nocopy (g_strchomp (s));
return val;
}
case 'N':
val = value_new_float (strtognum (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 value_new_string ("Uninitialised boolean");
default: {
char str[20];
snprintf (str, 20, "Invalid logical '%c'", s[0]);
g_free (s);
return value_new_string (str);
}
}
case 'D': {
/* double check that the date is stored according to spec */
int year, month, day;
if (sscanf (s, "%4d%2d%2d", &year, &month, &day) == 3) {
GDate *date = g_date_new_dmy (day, month, year);
/* Use default date convention */
val = value_new_int (datetime_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 (GINT32_FROM_LE (*(gint32 *)s));
g_free (s);
return val;
case 'F':
g_assert (sizeof (double) == field->len);
val = value_new_float (XB_GETDOUBLE (s));
g_free (s);
return val;
case 'B': {
gint64 tmp = GINT32_FROM_LE (*(gint64 *)s);
g_free (s);
g_warning ("FIXME: \"BINARY\" field type doesn't work");
g_assert (sizeof(tmp) == field->len);
return value_new_int (tmp);
}
default: {
char str[27];
snprintf (str, 27, "Field type '%c' unsupported",
field->type);
g_free (s);
return value_new_string (str);
}
}
}
void
xbase_file_open (GnmFileOpener const *fo, IOContext *io_context,
WorkbookView *wb_view, GsfInput *input)
{
Workbook *wb;
XBfile *file;
XBrecord *record;
char *name;
Sheet *sheet = NULL;
GnmCell *cell;
GnmValue *val;
XBfield *field;
ErrorInfo *open_error;
guint row, i;
if ((file = xbase_open (input, &open_error)) == NULL) {
gnumeric_io_error_info_set (io_context, error_info_new_str_with_details (
_("Error while opening xbase file."),
open_error));
return;
}
wb = wb_view_workbook (wb_view);
name = workbook_sheet_get_free_name (wb, _("Sheet"), FALSE, TRUE);
sheet = sheet_new (wb, name);
g_free (name);
workbook_sheet_attach (wb, sheet, NULL);
i = 0;
for (i = 0 ; i < file->fields ; i++) {
cell = sheet_cell_fetch (sheet, i, 0);
cell_set_text (cell, file->format [i]->name);
}
{
GnmRange r;
GnmStyle *bold = mstyle_new ();
mstyle_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 {
if (row >= SHEET_MAX_ROWS) {
g_warning (_("This build of Gnumeric can only hold %d "
"rows. Ignoring the rest of this file. You "
"will need a custom build with SHEET_MAX_ROWS "
"increased to read this file."),
SHEET_MAX_ROWS);
break;
}
for (i = 0; i < file->fields ; i++) {
field = record->file->format [i];
val = xbase_field_as_value (
record_get_field (record, i), field, file);
cell = sheet_cell_fetch (sheet, i, row);
value_set_fmt (val, field->fmt);
cell_set_value (cell, val);
}
row++;
} while (record_seek (record, SEEK_CUR, 1));
record_free (record);
xbase_close (file);
sheet_flag_recompute_spans (sheet);
}
|