mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-05-17 05:59:32 +08:00
Added support for MDB_BOOL, MDB_MONEY, MDB_BYTE, MDB_SDATETIME
This commit is contained in:
parent
fb576534d8
commit
ee6eacbe22
@ -143,6 +143,7 @@ extern MdbTableDef *mdb_alloc_tabledef(MdbCatalogEntry *entry);
|
||||
/* file.c */
|
||||
extern size_t mdb_read_pg(MdbHandle *mdb, unsigned long pg);
|
||||
extern size_t mdb_read_alt_pg(MdbHandle *mdb, unsigned long pg);
|
||||
extern unsigned char mdb_get_byte(MdbHandle *mdb, int offset);
|
||||
extern int mdb_get_int16(MdbHandle *mdb, int offset);
|
||||
extern long mdb_get_int32(MdbHandle *mdb, int offset);
|
||||
extern double mdb_get_double(MdbHandle *mdb, int offset);
|
||||
|
@ -20,7 +20,7 @@ INSTALL= @INSTALL@
|
||||
LIBLIST = libmdb.a
|
||||
|
||||
INC = -I ../include `glib-config --cflags`
|
||||
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o
|
||||
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o money.o
|
||||
|
||||
all: libmdb
|
||||
|
||||
@ -53,4 +53,5 @@ file.o: file.c ../include/mdbtools.h
|
||||
kkd.o: kkd.c ../include/mdbtools.h
|
||||
mem.o: mem.c ../include/mdbtools.h
|
||||
table.o: table.c ../include/mdbtools.h
|
||||
money.o: money.c
|
||||
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
#include "mdbtools.h"
|
||||
|
||||
char *mdb_money_to_string(MdbHandle *mdb, int start, char *s);
|
||||
|
||||
#define MDB_DEBUG 0
|
||||
|
||||
void mdb_bind_column(MdbTableDef *table, int col_num, void *bind_ptr)
|
||||
@ -65,6 +67,14 @@ int bit_num = (col_num - 1) % 8;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* bool has to be handled specially because it uses the null bit to store its
|
||||
** value*/
|
||||
static int mdb_xfer_bound_bool(MdbHandle *mdb, MdbColumn *col, int value)
|
||||
{
|
||||
if (col->bind_ptr) {
|
||||
strcpy(col->bind_ptr, value ? "0" : "1");
|
||||
}
|
||||
}
|
||||
static int mdb_xfer_bound_data(MdbHandle *mdb, int start, MdbColumn *col, int len)
|
||||
{
|
||||
if (col->bind_ptr) {
|
||||
@ -147,7 +157,9 @@ unsigned char null_mask[33]; /* 256 columns max / 8 bits per byte */
|
||||
col = g_ptr_array_index(table->columns,j);
|
||||
if (mdb_is_fixed_col(col) &&
|
||||
++fixed_cols_found <= fixed_cols) {
|
||||
if (mdb_is_null(null_mask, j+1)) {
|
||||
if (col->col_type == MDB_BOOL) {
|
||||
mdb_xfer_bound_bool(mdb, col, mdb_is_null(null_mask, j+1));
|
||||
} else if (mdb_is_null(null_mask, j+1)) {
|
||||
mdb_xfer_bound_data(mdb, 0, col, 0);
|
||||
} else {
|
||||
mdb_xfer_bound_data(mdb,row_start + col_start, col, col->col_size);
|
||||
@ -196,7 +208,9 @@ unsigned char null_mask[33]; /* 256 columns max / 8 bits per byte */
|
||||
- var_cols_found
|
||||
- 1 - num_of_jumps ] - col_start;
|
||||
|
||||
if (mdb_is_null(null_mask, j+1)) {
|
||||
if (col->col_type == MDB_BOOL) {
|
||||
mdb_xfer_bound_bool(mdb, col, mdb_is_null(null_mask, j+1));
|
||||
} else if (mdb_is_null(null_mask, j+1)) {
|
||||
mdb_xfer_bound_data(mdb, 0, col, 0);
|
||||
} else {
|
||||
mdb_xfer_bound_data(mdb,row_start + col_start, col, len);
|
||||
@ -324,10 +338,15 @@ char *mdb_col_to_string(MdbHandle *mdb, int start, int datatype, int size)
|
||||
{
|
||||
/* FIX ME -- not thread safe */
|
||||
static char text[MDB_BIND_SIZE];
|
||||
time_t t;
|
||||
|
||||
switch (datatype) {
|
||||
case MDB_BOOL:
|
||||
sprintf(text,"%d", mdb->pg_buf[start]);
|
||||
/* shouldn't happen. bools are handled specially
|
||||
** by mdb_xfer_bound_bool() */
|
||||
break;
|
||||
case MDB_BYTE:
|
||||
sprintf(text,"%d",mdb_get_byte(mdb, start));
|
||||
return text;
|
||||
break;
|
||||
case MDB_INT:
|
||||
@ -354,9 +373,20 @@ static char text[MDB_BIND_SIZE];
|
||||
text[size]='\0';
|
||||
return text;
|
||||
break;
|
||||
case MDB_SDATETIME:
|
||||
t = (long int)((mdb_get_double(mdb, start) - 25569.0) * 86400.0);
|
||||
strftime(text, MDB_BIND_SIZE, "%x %X",
|
||||
(struct tm*)gmtime(&t));
|
||||
return text;
|
||||
|
||||
break;
|
||||
case MDB_MEMO:
|
||||
return mdb_memo_to_string(mdb, start, size);
|
||||
break;
|
||||
case MDB_MONEY:
|
||||
mdb_money_to_string(mdb, start, text);
|
||||
return text;
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
break;
|
||||
|
@ -107,6 +107,14 @@ off_t offset = pg * mdb->pg_size;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
unsigned char mdb_get_byte(MdbHandle *mdb, int offset)
|
||||
{
|
||||
unsigned char c;
|
||||
|
||||
c = mdb->pg_buf[offset];
|
||||
mdb->cur_pos++;
|
||||
return c;
|
||||
}
|
||||
int mdb_get_int16(MdbHandle *mdb, int offset)
|
||||
{
|
||||
unsigned char *c;
|
||||
|
125
src/libmdb/money.c
Normal file
125
src/libmdb/money.c
Normal file
@ -0,0 +1,125 @@
|
||||
/* MDB Tools - A library for reading MS Access database file
|
||||
* Copyright (C) 1998-1999 Brian Bruns
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "mdbtools.h"
|
||||
|
||||
#define MAXPRECISION 9
|
||||
/*
|
||||
** these routines are copied from the freetds project which does something
|
||||
** very similiar
|
||||
*/
|
||||
|
||||
static int multiply_byte(unsigned char *product, int num, unsigned char *multiplier);
|
||||
static int do_carry(unsigned char *product);
|
||||
static char *array_to_string(unsigned char *array, int scale, char *s);
|
||||
|
||||
char *mdb_money_to_string(MdbHandle *mdb, int start, char *s)
|
||||
{
|
||||
unsigned char multiplier[MAXPRECISION], temp[MAXPRECISION];
|
||||
unsigned char product[MAXPRECISION];
|
||||
unsigned char *money;
|
||||
int num_bytes = 8;
|
||||
int i;
|
||||
int pos;
|
||||
int neg=0;
|
||||
|
||||
memset(multiplier,0,MAXPRECISION);
|
||||
memset(product,0,MAXPRECISION);
|
||||
multiplier[0]=1;
|
||||
|
||||
money = &mdb->pg_buf[start];
|
||||
if (money[7] && 0x01) {
|
||||
/* negative number -- preform two's complement */
|
||||
neg = 1;
|
||||
for (i=0;i<8;i++) {
|
||||
money[i] = ~money[i];
|
||||
}
|
||||
for (i=7; i>=0; i--) {
|
||||
money[7] += 1;
|
||||
if (money[i]!=0) break;
|
||||
}
|
||||
}
|
||||
for (pos=0;pos<num_bytes;pos++) {
|
||||
multiply_byte(product, money[pos], multiplier);
|
||||
|
||||
memcpy(temp, multiplier, MAXPRECISION);
|
||||
memset(multiplier,0,MAXPRECISION);
|
||||
multiply_byte(multiplier, 256, temp);
|
||||
}
|
||||
if (neg) {
|
||||
s[0]='-';
|
||||
array_to_string(product, 4, &s[1]);
|
||||
} else {
|
||||
array_to_string(product, 4, s);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
static int multiply_byte(unsigned char *product, int num, unsigned char *multiplier)
|
||||
{
|
||||
unsigned char number[3];
|
||||
int i, top, j, start;
|
||||
|
||||
number[0]=num%10;
|
||||
number[1]=(num/10)%10;
|
||||
number[2]=(num/100)%10;
|
||||
|
||||
for (top=MAXPRECISION-1;top>=0 && !multiplier[top];top--);
|
||||
start=0;
|
||||
for (i=0;i<=top;i++) {
|
||||
for (j=0;j<3;j++) {
|
||||
product[j+start]+=multiplier[i]*number[j];
|
||||
}
|
||||
do_carry(product);
|
||||
start++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int do_carry(unsigned char *product)
|
||||
{
|
||||
int j;
|
||||
|
||||
for (j=0;j<MAXPRECISION;j++) {
|
||||
if (product[j]>9) {
|
||||
product[j+1]+=product[j]/10;
|
||||
product[j]=product[j]%10;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static char *array_to_string(unsigned char *array, int scale, char *s)
|
||||
{
|
||||
int top, i, j;
|
||||
|
||||
for (top=MAXPRECISION-1;top>=0 && top>scale && !array[top];top--);
|
||||
|
||||
if (top == -1)
|
||||
{
|
||||
s[0] = '0';
|
||||
s[1] = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
j=0;
|
||||
for (i=top;i>=0;i--) {
|
||||
if (top+1-j == scale) s[j++]='.';
|
||||
s[j++]=array[i]+'0';
|
||||
}
|
||||
s[j]='\0';
|
||||
}
|
@ -103,7 +103,8 @@ int opt;
|
||||
col=g_ptr_array_index(table->columns,j);
|
||||
if (quote_text &&
|
||||
(col->col_type==MDB_TEXT ||
|
||||
col->col_type==MDB_MEMO)) {
|
||||
col->col_type==MDB_MEMO ||
|
||||
col->col_type==MDB_SDATETIME)) {
|
||||
fprintf(stdout,"%s\"%s\"",delimiter,bound_values[j]);
|
||||
} else {
|
||||
fprintf(stdout,"%s%s",delimiter,bound_values[j]);
|
||||
|
Loading…
Reference in New Issue
Block a user