gnm_float datetime_value_to_serial_raw (GnmValue const *v, GODateConventions const *conv) { gnm_float serial; if (VALUE_IS_NUMBER (v)) serial = value_get_as_float (v); else { char const *str = value_peek_string (v); GnmValue *conversion = format_match_number (str, go_format_default_date (), conv); if (conversion) { serial = value_get_as_float (conversion); value_release (conversion); } else serial = G_MAXINT; } if (serial < 0 && !gnm_datetime_allow_negative ()) serial = G_MAXINT; return serial; }
G_MODULE_EXPORT void paradox_file_open (GOFileOpener const *fo, GOIOContext *io_context, WorkbookView *wb_view, GsfInput *input) { Workbook *wb; pxdoc_t *pxdoc; pxhead_t *pxh; pxfield_t *pxf; char *data; char *name; Sheet *sheet; GnmCell *cell; GnmValue *val = NULL; GOErrorInfo *open_error = NULL; guint row, i, j, offset; #ifdef PX_MEMORY_DEBUGGING PX_mp_init (); #endif #ifdef PX_MEMORY_DEBUGGING pxdoc = PX_new2 (gn_errorhandler, PX_mp_malloc, PX_mp_realloc, PX_mp_free); #else pxdoc = PX_new2 (gn_errorhandler, gn_malloc, gn_realloc, gn_free); #endif if (PX_open_gsf (pxdoc, input) < 0) { go_io_error_info_set (io_context, go_error_info_new_str_with_details ( _("Error while opening Paradox file."), open_error)); return; } pxh = pxdoc->px_head; PX_set_targetencoding (pxdoc, "UTF-8"); wb = wb_view_get_workbook (wb_view); name = workbook_sheet_get_free_name (wb, pxh->px_tablename, FALSE, TRUE); sheet = sheet_new (wb, name, 256, 65536); g_free (name); workbook_sheet_attach (wb, sheet); pxf = pxh->px_fields; for (i = 0 ; i < (guint) pxh->px_numfields; i++) { char str[30], *str2; char ctypes[26] = {'?', 'A', 'D', 'S', 'I', '$', 'N', '?', '?', 'L', '?', '?', 'M', 'B', 'F', 'O', 'G', '?', '?', '?', 'T', '@', '+', '#', 'Y', }; cell = sheet_cell_fetch (sheet, i, 0); if (pxf->px_ftype == pxfBCD) snprintf (str, 30, "%s,%c,%d", pxf->px_fname, ctypes[(int)pxf->px_ftype], pxf->px_fdc); else snprintf (str, 30, "%s,%c,%d", pxf->px_fname, ctypes[(int)pxf->px_ftype], pxf->px_flen); #if PXLIB_MAJOR_VERSION == 0 && (PXLIB_MINOR_VERION < 3 || (PXLIB_MAJOR_VERSION == 3 && PXLIB_MICRO_VERSION == 0)) /* Convert the field names to utf-8. This is actually in pxlib * responsibility, but hasn't been implemented yet. For the mean time * we *misuse* PX_get_data_alpha() */ PX_get_data_alpha (pxdoc, str, strlen (str), &str2); gnm_cell_set_text (cell, str2); pxdoc->free (pxdoc, str2); #else gnm_cell_set_text (cell, str); #endif pxf++; } { GnmRange r; GnmStyle *bold = gnm_style_new (); gnm_style_set_font_bold (bold, TRUE); sheet_style_apply_range (sheet, range_init (&r, 0, 0, pxh->px_numfields-1, 0), bold); } if ((data = (char *) pxdoc->malloc (pxdoc, pxh->px_recordsize, _("Could not allocate memory for record."))) == NULL) { go_io_error_info_set (io_context, go_error_info_new_str_with_details ( _("Error while opening Paradox file."), open_error)); return; } row = 1; for (j = 0; j < (guint)pxh->px_numrecords; j++) { pxdatablockinfo_t pxdbinfo; int isdeleted = 0; if (NULL != PX_get_record2 (pxdoc, j, data, &isdeleted, &pxdbinfo)) { offset = 0; pxf = pxh->px_fields; for (i = 0; i < (guint) pxh->px_numfields ; i++) { cell = sheet_cell_fetch (sheet, i, row); val = NULL; switch (pxf->px_ftype) { case pxfAlpha: { char *value; if (0 < PX_get_data_alpha (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_string_nocopy (value); /* value_set_fmt (val, field->fmt); */ } break; } case pxfShort: { short int value; if (0 < PX_get_data_short (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_int (value); } break; } case pxfAutoInc: case pxfLong: { long value; if (0 < PX_get_data_long (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_int (value); } break; } case pxfCurrency: case pxfNumber: { double value; if (0 < PX_get_data_double (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_float (value); if (pxf->px_ftype == pxfCurrency) value_set_fmt (val, go_format_default_money ()); } break; } case pxfTimestamp: { double value; if (0 < PX_get_data_double (pxdoc, &data[offset], pxf->px_flen, &value)) { value = value / 86400000.0; /* 693594 = number of days up to 31.12.1899 */ value -= 693594; val = value_new_float (value); value_set_fmt (val, go_format_default_date_time ()); } break; } case pxfLogical: { char value; if (0 < PX_get_data_byte (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_bool (value ? TRUE : FALSE); } break; } case pxfDate: { long value; int year, month, day; GDate *date; if (0 < PX_get_data_long (pxdoc, &data[offset], pxf->px_flen, &value)) { PX_SdnToGregorian (value+1721425, &year, &month, &day); date = g_date_new_dmy (day, month, year); val = value_new_int (go_date_g_to_serial (date, NULL)); value_set_fmt (val, go_format_default_date ()); g_date_free (date); } break; } case pxfTime: { long value; if (0 < PX_get_data_long (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_float (value/86400000.0); value_set_fmt (val, go_format_default_time ()); } break; } case pxfBCD: { char *value; if (0 < PX_get_data_bcd (pxdoc, &data[offset], pxf->px_fdc, &value)) { val = value_new_string_nocopy (value); } break; } case pxfMemoBLOb: { char *value; int size, mod_nr; if (0 < PX_get_data_blob (pxdoc, &data[offset], pxf->px_flen, &mod_nr, &size, &value)) { val = value_new_string_nocopy (value); } break; } default: val = value_new_string_nocopy ( g_strdup_printf (_("Field type %d is not supported."), pxf->px_ftype)); } if (val) gnm_cell_set_value (cell, val); offset += pxf->px_flen; pxf++; } if (pxh->px_filetype == pxfFileTypPrimIndex) { short int value; cell = sheet_cell_fetch (sheet, i++, row); if (0 < PX_get_data_short (pxdoc, &data[offset], 2, &value)) { val = value_new_int (value); gnm_cell_set_value (cell, val); } offset += 2; cell = sheet_cell_fetch (sheet, i++, row); if (0 < PX_get_data_short (pxdoc, &data[offset], 2, &value)) { val = value_new_int (value); gnm_cell_set_value (cell, val); } offset += 2; cell = sheet_cell_fetch (sheet, i++, row); if (0 < PX_get_data_short (pxdoc, &data[offset], 2, &value)) { val = value_new_int (value); gnm_cell_set_value (cell, val); } cell = sheet_cell_fetch (sheet, i++, row); val = value_new_int (pxdbinfo.number); gnm_cell_set_value (cell, val); } } row++; } pxdoc->free (pxdoc, data); PX_close (pxdoc); PX_delete (pxdoc); sheet_flag_recompute_spans (sheet); }