[patch] fix for handling decimal (numeric) values

View: New views
1 Messages — Rating Filter:   Alert me  

[patch] fix for handling decimal (numeric) values

by Jaroslaw Staniek-2 :: Rate this Message:

| View Threaded | Show Only this Message


Hello

A patch to include in mdbtools.

- fixed importing decimal (numeric) values
   (by fixing setting fractional point's position and adding negative sign when
    needed)

Notes:
1. The "negative" bit is the one most significant from the first byte:

  ( *(mdb->pg_buf+start) & 0x80) )

2. I used int negtive variable to speed up the calculations.

3. There was also a bug in using memmove()

4. The patch is already tested and included within Kexi MDB Import driver,
which utilizes mdbtools.

--
regards / pozdrawiam,
  Jaroslaw Staniek / OpenOffice Polska

Sponsored by OpenOffice Polska to work on
* Kexi & KOffice: http://www.kexi-project.org | http://koffice.org/kexi
* KDE3 & KDE4 Libraries For Developing MS Windows Applications:
                   http://www.kdelibs.com/wiki
See also:
* Kexi For MS Windows: http://kexi.pl/wiki/index.php/Kexi_for_MS_Windows
* Kexi Support:        http://www.kexi-project.org/support.html

Index: src/mdbtools/libmdb/data.c
===================================================================
--- src/mdbtools/libmdb/data.c (revision 535579)
+++ src/mdbtools/libmdb/data.c (working copy)
@@ -691,15 +691,20 @@
 mdb_num_to_string(MdbHandle *mdb, int start, int datatype, int prec, int scale)
 {
  char *text;
+ int negative;
  gint32 l;
 
  memcpy(&l, mdb->pg_buf+start+13, 4);
-
- text = (char *) g_malloc(prec+2);
- sprintf(text, "%0*" G_GINT32_FORMAT, prec, GINT32_FROM_LE(l));
+ negative = (*(mdb->pg_buf+start) & 0x80) ? 1 : 0;
+ text = (char *) g_malloc(prec+2+negative);
+ if (negative) {
+ sprintf(text, "-%0*" G_GINT32_FORMAT, prec, GINT32_FROM_LE(l));
+ } else {
+ sprintf(text, "%0*" G_GINT32_FORMAT, prec, GINT32_FROM_LE(l));
+ }
  if (scale) {
- memmove(text+prec-scale, text+prec-scale+1, scale+1);
- text[prec-scale] = '.';
+ memmove(text+prec-scale+1+negative, text+prec-scale+negative, scale+1);
+ text[prec-scale+negative] = '.';
  }
  return text;
 }