missed added files

This commit is contained in:
brianb 2003-05-02 22:20:44 +00:00
parent aa19a0892f
commit 8592cf5db5
4 changed files with 587 additions and 0 deletions

127
src/libmdb/props.c Normal file
View File

@ -0,0 +1,127 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 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 "mdbtools.h"
GPtrArray *
mdb_read_props_list(gchar *kkd, int len)
{
guint32 record_len;
int pos = 0;
gchar *name;
GPtrArray *names = NULL;
int i = 0;
names = g_ptr_array_new();
#ifdef MDB_DEBUG
buffer_dump(kkd, 0, len - 1);
#endif
pos = 0;
while (pos < len) {
record_len = mdb_get_int16(kkd, pos);
pos += 2;
#ifdef MDB_DEBUG
printf("%02d ",i++);
buffer_dump(kkd, pos - 2, pos + record_len - 1);
#endif
name = g_malloc(record_len + 1);
strncpy(name, &kkd[pos], record_len);
name[record_len] = '\0';
pos += record_len;
g_ptr_array_add(names, name);
#ifdef MDB_DEBUG
printf("new len = %d\n", names->len);
#endif
}
return names;
}
void
mdb_free_props(MdbProperties *props)
{
if (!props) return;
if (props->name) g_free(props->name);
g_free(props);
}
MdbProperties *
mdb_alloc_props()
{
MdbProperties *props;
props = g_malloc0(sizeof(MdbProperties));
return props;
}
MdbProperties *
mdb_read_props(MdbHandle *mdb, GPtrArray *names, gchar *kkd, int len)
{
guint32 record_len, name_len;
int pos = 0;
int elem, dtype, dsize;
gchar *name, *value;
MdbProperties *props;
int i = 0;
#ifdef MDB_DEBUG
buffer_dump(kkd, 0, len - 1);
#endif
pos = 0;
/* skip the name record */
record_len = mdb_get_int16(kkd, pos);
pos += 4;
name_len = mdb_get_int16(kkd, pos);
pos += 2;
props = mdb_alloc_props();
if (name_len) {
props->name = g_malloc(name_len + 1);
strncpy(props->name, &kkd[pos], name_len);
props->name[name_len]='\0';
}
pos += name_len;
props->hash = g_hash_table_new(g_str_hash, g_str_equal);
while (pos < len) {
record_len = mdb_get_int16(kkd, pos);
elem = mdb_get_int16(kkd, pos + 4);
dtype = kkd[pos + 3];
dsize = mdb_get_int16(kkd, pos + 6);
value = g_malloc(dsize + 1);
strncpy(value, &kkd[pos + 8], dsize);
value[dsize] = '\0';
name = g_ptr_array_index(names,elem);
#ifdef MDB_DEBUG
printf("%02d ",i++);
buffer_dump(kkd, pos, pos + record_len - 1);
printf("elem %d dsize %d dtype %d\n", elem, dsize, dtype);
#endif
if (dtype == MDB_MEMO) dtype = MDB_TEXT;
if (dtype == MDB_BOOL) {
g_hash_table_insert(props->hash, g_strdup(name), g_strdup(kkd[pos + 8] ? "yes" : "no"));
} else {
g_hash_table_insert(props->hash, g_strdup(name), g_strdup(mdb_col_to_string(mdb, kkd, pos + 8, dtype, dsize)));
}
g_free(value);
pos += record_len;
}
return props;
}

240
src/util/mdb-import.c Normal file
View File

@ -0,0 +1,240 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 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 "mdbtools.h"
#define MAX_ROW_SIZE 4096
char *delimiter = ",";
char header_rows = 0;
void
free_values(MdbField *fields, int num_fields)
{
int i;
for (i=0;i<num_fields;i++) {
if (!fields[i].is_null)
g_free(fields[i].value);
}
}
int
convert_field(MdbColumn *col, char *s, MdbField *field)
{
char *c;
MdbAny any;
switch (col->col_type) {
case MDB_TEXT:
field->value = g_strdup(s);
field->siz = strlen(s);
break;
case MDB_BYTE:
any.i = strtol(s, &c, 16);
if (*c) return 1;
field->siz = mdb_col_fixed_size(col);
field->value = g_malloc(field->siz);
((unsigned char *)field->value)[0] = any.i;
break;
case MDB_INT:
any.i = strtol(s, &c, 16);
if (*c) return 1;
field->siz = mdb_col_fixed_size(col);
field->value = g_malloc(field->siz);
_mdb_put_int16(field->value, 0, any.i);
break;
case MDB_LONGINT:
any.i = strtol(s, &c, 16);
if (*c) return 1;
field->siz = mdb_col_fixed_size(col);
field->value = g_malloc(field->siz);
_mdb_put_int32(field->value, 0, any.i);
break;
case MDB_BOOL:
if (*s == '1') {
field->is_null = 1;
} else if (*s == '0') {
field->is_null = 0;
} else {
fprintf(stderr, "%c is not a valid value for type BOOLEAN\n", *s);
return 1;
}
/*
case MDB_FLOAT:
any.d = strtod(s, &c);
if (*c) return 1;
field.value = g_malloc(mdb_col_fixed_size(col));
_mdb_put_single(field.value, 0, any.i);
break;
case MDB_DOUBLE:
any.d = strtod(s, &c);
if (*c) return 1;
field.value = g_malloc(mdb_col_fixed_size(col));
_mdb_put_double(field.value, 0, any.i);
break;
*/
/*
case MDB_MONEY:
case MDB_SDATETIME:
case MDB_OLE:
case MDB_MEMO:
case MDB_REPID:
case MDB_NUMERIC:
*/
default:
fprintf(stderr,"Conversion of type %02x not supported yet.\n", col->col_type);
return 1;
break;
}
return 0;
}
int
prep_row(MdbTableDef *table, unsigned char *line, MdbField *fields)
{
MdbColumn *col;
char delims[20];
char *s;
int file_cols;
/*
* pull apart the row and turn it into something respectable
*/
sprintf(delims, "%c\n", delimiter[0]);
s = strtok(line, delims);
file_cols = 0;
do {
if (file_cols >= table->num_cols) {
fprintf(stderr, "Number of columns in file exceeds number in table.\n");
return 0;
}
if (s[0]=='"' && s[strlen(s)-1]=='"') {
s[strlen(s)-1] = '\0';
s++;
}
printf("field = %s\n", s);
col = g_ptr_array_index (table->columns, file_cols);
if (convert_field(col, s, &fields[file_cols])) {
fprintf(stderr, "Format error in column %d\n", file_cols+1);
return 0;
}
file_cols++;
} while (s = strtok(NULL, delims));
/*
* verify number of columns in table against number in row
*/
if (file_cols < table->num_cols) {
fprintf(stderr, "Row has %d columns, but table has %d\n", file_cols, table->num_cols);
free_values(fields, file_cols);
return 0;
}
return file_cols;
}
int
main(int argc, char **argv)
{
int i, row;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbField fields[256];
unsigned char line[MAX_ROW_SIZE];
int num_fields;
/* doesn't handle tables > 256 columns. Can that happen? */
int opt;
char *s;
FILE *in;
while ((opt=getopt(argc, argv, "H:d:"))!=-1) {
switch (opt) {
case 'H':
header_rows = atol(optarg);
break;
case 'd':
delimiter = (char *) malloc(strlen(optarg)+1);
strcpy(delimiter, optarg);
break;
default:
break;
}
}
/*
** optind is now the position of the first non-option arg,
** see getopt(3)
*/
if (argc-optind < 3) {
fprintf(stderr,"Usage: %s [options] <database> <table> <csv file>\n",argv[0]);
fprintf(stderr,"where options are:\n");
fprintf(stderr," -H <rows> skip <rows> header rows\n");
fprintf(stderr," -d <delimiter> specify a column delimiter\n");
exit(1);
}
mdb_init();
if (!(mdb = _mdb_open(argv[optind], TRUE))) {
exit(1);
}
mdb_read_catalog(mdb, MDB_TABLE);
for (i=0;i<mdb->num_catalog;i++) {
entry = g_ptr_array_index(mdb->catalog,i);
if (entry->object_type == MDB_TABLE &&
!strcmp(entry->object_name,argv[argc-2])) {
table = mdb_read_table(entry);
mdb_read_columns(table);
mdb_read_indices(table);
mdb_rewind_table(table);
break;
}
}
/*
* open the CSV file and read any header rows
*/
in = fopen(argv[argc-1], "r");
if (!in) {
fprintf(stderr, "Can not open file %s\n", argv[argc-1]);
exit(1);
}
for (i=0;i<header_rows;i++)
fgets(line, MAX_ROW_SIZE, in);
row = 1;
while (fgets(line, MAX_ROW_SIZE, in)) {
num_fields = prep_row(table, line, fields);
if (!num_fields) {
fprintf(stderr, "Aborting import at row %d\n", row);
exit(1);
}
/*
* all the prep work is done, let's add the row
*/
mdb_insert_row(table, num_fields, fields);
}
fclose(in);
mdb_free_handle(mdb);
mdb_exit();
exit(0);
}

154
src/util/mdb-prop.c Normal file
View File

@ -0,0 +1,154 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 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 "mdbtools.h"
#define MSYSOBJECTS "MSysObjects"
#define LVPROP "LvProp"
#undef MDB_DEBUG
//#define MDB_DEBUG 1
void dump_kkd(gchar *kkd, int len);
MdbHandle *mdb;
int
main(int argc, char **argv)
{
int i, len, pos, col_num;
MdbColumn *col;
MdbTableDef *table;
MdbCatalogEntry *entry;
gchar name[256];
gchar buf[MDB_BIND_SIZE];
gchar kkd_pg[200000];
gchar kkd_ptr[MDB_MEMO_OVERHEAD+1];
/*
** optind is now the position of the first non-option arg,
** see getopt(3)
*/
if (argc < 2) {
fprintf(stderr,"Usage: %s <file> <name> [<prop col>]\n",argv[0]);
exit(1);
}
mdb_init();
if (!(mdb = mdb_open(argv[optind]))) {
exit(1);
}
mdb_read_catalog(mdb, MDB_TABLE);
for (i=0;i<mdb->num_catalog;i++) {
entry = g_ptr_array_index(mdb->catalog,i);
if (!strcmp(entry->object_name,MSYSOBJECTS)) {
break;
}
}
table = mdb_read_table(entry);
mdb_read_columns(table);
mdb_rewind_table(table);
col_num = mdb_bind_column_by_name(table, LVPROP, buf);
mdb_bind_len(table, col_num, &len);
mdb_bind_column_by_name(table, "Name", name);
while(mdb_fetch_row(table)) {
if (!strcmp(name, argv[optind+1])) {
memcpy(kkd_ptr, buf, MDB_MEMO_OVERHEAD);
col=g_ptr_array_index(table->columns,col_num - 1);
len = mdb_ole_read(mdb, col, kkd_ptr, MDB_BIND_SIZE);
memcpy(kkd_pg, buf, len);
pos = len;
while ((len = mdb_ole_read_next(mdb, col, kkd_ptr))) {
memcpy(&kkd_pg[pos], buf, len);
pos += len;
}
len = pos;
dump_kkd(kkd_pg, len);
}
}
mdb_free_handle(mdb);
mdb_exit();
exit(0);
}
void print_keyvalue(gpointer key, gpointer value, gpointer user_data)
{
printf("%s = %s\n", (gchar *)key, (gchar *)value);
}
void dump_kkd(gchar *kkd, int len)
{
guint32 record_len, record_type;
int pos = 0;
GPtrArray *names = NULL;
gchar *name;
MdbProperties *props;
#ifdef MDB_DEBUG
buffer_dump(kkd, 0, len);
#endif
if (strcmp("KKD", kkd)) {
fprintf(stderr, "Unrecognized format.\n");
return;
}
pos = 4;
while (pos < len) {
record_len = mdb_get_int32(kkd, pos);
record_type = mdb_get_int16(kkd, pos + 4);
//printf("len = %d type = %d\n", record_len, record_type);
switch (record_type) {
case 0x80:
names = mdb_read_props_list(&kkd[pos+6], record_len - 6);
break;
case 0x00:
if (!names) {
printf("sequence error!\n");
break;
}
props = mdb_read_props(mdb, names, &kkd[pos+6], record_len - 6);
printf("type 0x00 name %s\n", props->name ? props->name : "(none)");
g_hash_table_foreach(props->hash, print_keyvalue, NULL);
mdb_free_props(props);
break;
case 0x01:
if (!names) {
printf("sequence error!\n");
break;
}
props = mdb_read_props(names, &kkd[pos+6], record_len - 6);
printf("type 0x01 name %s\n", props->name ? props->name : "(none)");
g_hash_table_foreach(props->hash, print_keyvalue, NULL);
mdb_free_props(props);
break;
default:
fprintf(stderr,"Unknown record type %d\n", record_type);
return;
}
pos += record_len;
}
}

66
src/util/prfreemap.c Normal file
View File

@ -0,0 +1,66 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 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 "mdbtools.h"
int
main(int argc, char **argv)
{
MdbHandle *mdb;
int j;
long int pgnum = 0, row_start, row_end, map_sz;
int coln = 1;
unsigned char *map_buf;
if (argc<2) {
fprintf(stderr,"Usage: %s <file>\n",argv[0]);
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_read_pg (mdb, 1);
row_start = mdb_get_int16(mdb, (mdb->fmt->row_count_offset + 2));
row_end = mdb_find_end_of_row(mdb, 0);
map_buf = &mdb->pg_buf[row_start];
map_sz = row_end - row_start;
/*
* trim the end of a type 0 map
*/
if (map_buf[0]==0)
while (map_buf[map_sz]==0xff) map_sz--;
while (pgnum = mdb_map_find_next(mdb, map_buf, map_sz, pgnum)) {
printf("%6lu ",(long unsigned) pgnum);
if (coln==10) {
printf("\n");
coln = 0;
}
coln++;
}
if (coln!=1) printf("\n");
mdb_free_handle(mdb);
mdb_exit();
exit(0);
}