mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-07-15 15:16:47 +08:00
new utilities from Karl
This commit is contained in:
parent
e469aed2d7
commit
a619228310
@ -3,7 +3,7 @@ CC = gcc
|
||||
|
||||
INC = -I ../include `glib-config --cflags`
|
||||
LIBS = -L ../libmdb -lmdb `glib-config --libs`
|
||||
PROGS = prcat prkkd prtable prdata prdump schema mdb-export
|
||||
PROGS = prcat prkkd prtable prdata prdump mdb-schema mdb-export mdb-tables mdb-header mdb-parsecsv
|
||||
PRCATOBJS = prcat.o
|
||||
PRKKDOBJS = prkkd.o
|
||||
PRTABLEOBJS = prtable.o
|
||||
@ -11,6 +11,9 @@ PRDATAOBJS = prdata.o
|
||||
PRDUMPOBJS = prdump.o
|
||||
SCHEMAOBJS = schema.o
|
||||
EXPORTOBJS = mdb-export.o
|
||||
HEADEROBJS = header.o
|
||||
TABLEOBJS = tables.o
|
||||
CSVOBJS = parsecsv.o
|
||||
|
||||
all: $(PROGS)
|
||||
|
||||
@ -29,12 +32,21 @@ prdata: $(PRDATAOBJS)
|
||||
prdump: $(PRDUMPOBJS)
|
||||
$(CC) -g -o $@ $(PRDUMPOBJS) $(LIBS)
|
||||
|
||||
schema: $(SCHEMAOBJS)
|
||||
mdb-schema: $(SCHEMAOBJS)
|
||||
$(CC) -g -o $@ $(SCHEMAOBJS) $(LIBS)
|
||||
|
||||
mdb-export: $(EXPORTOBJS)
|
||||
$(CC) -g -o $@ $(EXPORTOBJS) $(LIBS)
|
||||
|
||||
mdb-tables: $(TABLEOBJS)
|
||||
$(CC) -g -o $@ $(TABLEOBJS) $(LIBS)
|
||||
|
||||
mdb-header: $(HEADEROBJS)
|
||||
$(CC) -g -o $@ $(HEADEROBJS) $(LIBS)
|
||||
|
||||
mdb-parsecsv: $(CSVOBJS)
|
||||
$(CC) -g -o $@ $(CSVOBJS) $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f core *.o $(PROGS)
|
||||
|
||||
|
151
src/util/header.c
Normal file
151
src/util/header.c
Normal file
@ -0,0 +1,151 @@
|
||||
/* MDB Tools - A library for reading MS Access database file
|
||||
* Copyright (C) 2000 Brian Bruns
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/* this utility dumps the C headers for an existing database */
|
||||
/* it will create three files - types.h and dump_types.[ch] */
|
||||
|
||||
#include "mdbtools.h"
|
||||
#include <string.h>
|
||||
|
||||
void copy_header (FILE *f)
|
||||
{
|
||||
fprintf (f, "/******************************************************************/\n");
|
||||
fprintf (f, "/* THIS IS AN AUTOMATICALLY GENERATED FILE. DO NOT EDIT IT!!!!!! */\n");
|
||||
fprintf (f, "/******************************************************************/\n");
|
||||
}
|
||||
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int i, j, k;
|
||||
MdbHandle *mdb;
|
||||
MdbCatalogEntry entry;
|
||||
MdbTableDef *table;
|
||||
MdbColumn *col;
|
||||
FILE *typesfile;
|
||||
FILE *headerfile;
|
||||
FILE *cfile;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf (stderr, "Usage: %s <file>\n",argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
/* open the database */
|
||||
|
||||
mdb = mdb_open (argv[1]);
|
||||
if (!mdb) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
typesfile = fopen ("types.h", "w");
|
||||
headerfile = fopen ("dumptypes.h", "w");
|
||||
cfile = fopen ("dumptypes.c", "w");
|
||||
|
||||
copy_header (typesfile);
|
||||
copy_header (headerfile);
|
||||
fprintf (headerfile, "#include \"types.h\"\n");
|
||||
copy_header (cfile);
|
||||
fprintf (cfile, "#include <stdio.h>\n");
|
||||
fprintf (cfile, "#include \"dumptypes.h\"\n");
|
||||
|
||||
/* read the catalog */
|
||||
|
||||
mdb_read_catalog (mdb, MDB_TABLE);
|
||||
|
||||
/* loop over each entry in the catalog */
|
||||
|
||||
for (i=0; i < mdb->num_catalog; i++)
|
||||
{
|
||||
entry = g_array_index (mdb->catalog, MdbCatalogEntry, i);
|
||||
|
||||
/* if it's a table */
|
||||
|
||||
if (entry.object_type == MDB_TABLE)
|
||||
{
|
||||
/* skip the MSys tables */
|
||||
if (strncmp (entry.object_name, "MSys", 4))
|
||||
{
|
||||
|
||||
/* make sure it's a table (may be redundant) */
|
||||
|
||||
if (!strcmp (mdb_get_objtype_string (entry.object_type), "Table"))
|
||||
{
|
||||
|
||||
fprintf (typesfile, "typedef struct _%s\n", entry.object_name);
|
||||
fprintf (typesfile, "{\n");
|
||||
|
||||
fprintf (headerfile, "void dump_%s (%s x);\n",
|
||||
entry.object_name, entry.object_name);
|
||||
fprintf (cfile, "void dump_%s (%s x)\n{\n",
|
||||
entry.object_name, entry.object_name);
|
||||
fprintf (cfile, "\tfprintf (stdout, \"**************** %s ****************\\n\");\n", entry.object_name);
|
||||
table = mdb_read_table (&entry);
|
||||
|
||||
/* get the columns */
|
||||
mdb_read_columns (table);
|
||||
|
||||
/* loop over the columns, dumping the names and types */
|
||||
|
||||
for (k = 0; k < table->num_cols; k++)
|
||||
{
|
||||
col = g_ptr_array_index (table->columns, k);
|
||||
fprintf (cfile, "\tfprintf (stdout, \"x.");
|
||||
for (j = 0; j < strlen (col->name); j++)
|
||||
{
|
||||
fprintf (cfile, "%c", tolower (col->name [j]));
|
||||
}
|
||||
fprintf (cfile, " = \");\n");
|
||||
switch (col->col_type)
|
||||
{
|
||||
case MDB_INT:
|
||||
fprintf (typesfile, "\tint\t");
|
||||
fprintf (cfile, "\tdump_int (x.");
|
||||
break;
|
||||
case MDB_LONGINT:
|
||||
fprintf (typesfile, "\tlong\t");
|
||||
fprintf (cfile, "\tdump_long (x.");
|
||||
break;
|
||||
case MDB_TEXT:
|
||||
case MDB_MEMO:
|
||||
fprintf (typesfile, "\tchar *\t");
|
||||
fprintf (cfile, "\tdump_string (x.");
|
||||
break;
|
||||
}
|
||||
for (j = 0; j < strlen (col->name); j++)
|
||||
{
|
||||
fprintf (typesfile, "%c", tolower (col->name [j]));
|
||||
fprintf (cfile, "%c", tolower (col->name [j]));
|
||||
}
|
||||
fprintf (typesfile, ";\n");
|
||||
fprintf (cfile, ");\n");
|
||||
}
|
||||
|
||||
fprintf (typesfile, "\n} %s ;\n", entry.object_name);
|
||||
fprintf (typesfile, "\n");
|
||||
fprintf (cfile, "}\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose (typesfile);
|
||||
fclose (cfile);
|
||||
|
||||
mdb_free_handle (mdb);
|
||||
}
|
||||
|
161
src/util/parsecsv.c
Normal file
161
src/util/parsecsv.c
Normal file
@ -0,0 +1,161 @@
|
||||
/* MDB Tools - A library for reading MS Access database file
|
||||
* Copyright (C) 2000 Brian Bruns
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/* this utility converts a CSV from an existing database to a C file */
|
||||
/* input FOO.txt, output FOO.c */
|
||||
/* generates an array of type FOO */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define FILENAMESIZE 128
|
||||
#define BUFFERSIZE 4096
|
||||
#define LF 10
|
||||
#define NL 13
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
void copy_header (FILE *f)
|
||||
{
|
||||
fprintf (f, "/******************************************************************/\n");
|
||||
fprintf (f, "/* THIS IS AN AUTOMATICALLY GENERATED FILE. DO NOT EDIT IT!!!!!! */\n");
|
||||
fprintf (f, "/******************************************************************/\n");
|
||||
}
|
||||
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
char txt_filename [FILENAMESIZE];
|
||||
char c_filename [FILENAMESIZE];
|
||||
FILE *txtfile;
|
||||
FILE *cfile;
|
||||
int count;
|
||||
char input [BUFFERSIZE];
|
||||
int location;
|
||||
char c;
|
||||
int instring;
|
||||
int lastcomma;
|
||||
int i;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s <file> (assumed extension .txt)\n",argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
strcpy (txt_filename, argv [1]);
|
||||
txtfile = fopen (txt_filename, "r");
|
||||
if (!txtfile) {
|
||||
strcat (txt_filename, ".txt");
|
||||
txtfile = fopen (txt_filename, "r");
|
||||
if (!txtfile) exit(1);
|
||||
} else {
|
||||
/* strip away extension */
|
||||
for (i=strlen(txt_filename);i > 0 && txt_filename[i]!='.';i--);
|
||||
if (txt_filename[i]=='.') txt_filename[i]='\0';
|
||||
}
|
||||
|
||||
strcpy (c_filename, argv [1]);
|
||||
strcat (c_filename, ".c");
|
||||
|
||||
cfile = fopen (c_filename, "w");
|
||||
|
||||
copy_header (cfile);
|
||||
fprintf (cfile, "\n");
|
||||
fprintf (cfile, "#include <stdio.h>\n");
|
||||
fprintf (cfile, "#include \"types.h\"\n");
|
||||
fprintf (cfile, "#include \"dump.h\"\n");
|
||||
fprintf (cfile, "\n");
|
||||
|
||||
fprintf (cfile, "const %s %s_array [] = {\n", argv [1], argv [1]);
|
||||
|
||||
c = 0;
|
||||
count = 0;
|
||||
while (c != -1)
|
||||
{
|
||||
location = 0;
|
||||
memset (input, 0, BUFFERSIZE);
|
||||
while ((c = fgetc (txtfile)) != NL)
|
||||
{
|
||||
if (c == -1)
|
||||
break;
|
||||
input [location++] = c;
|
||||
}
|
||||
input [location] = '\0';
|
||||
if (location > 0)
|
||||
{
|
||||
if (count != 0)
|
||||
{
|
||||
fprintf (cfile, ",\n");
|
||||
}
|
||||
fprintf (cfile, "{\t\t\t\t/* %6d */\n\t", count);
|
||||
instring = FALSE;
|
||||
lastcomma = FALSE;
|
||||
for (i = 0; i < location; i++)
|
||||
{
|
||||
if (instring)
|
||||
{
|
||||
if (input [i] == '\\')
|
||||
fprintf (cfile, "\\\\");
|
||||
else if (input [i] == LF)
|
||||
fprintf (cfile, "\\n");
|
||||
else if (input [i] != NL)
|
||||
fprintf (cfile, "%c", input [i]);
|
||||
if (input [i] == '"')
|
||||
{
|
||||
instring = FALSE;
|
||||
lastcomma = FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (input [i])
|
||||
{
|
||||
case ',':
|
||||
if (lastcomma)
|
||||
fprintf (cfile, "\"\"");
|
||||
fprintf (cfile, ",\n\t");
|
||||
lastcomma = TRUE;
|
||||
break;
|
||||
case '"':
|
||||
fprintf (cfile, "%c", input [i]);
|
||||
lastcomma = FALSE;
|
||||
instring = TRUE;
|
||||
break;
|
||||
default:
|
||||
fprintf (cfile, "%c", input [i]);
|
||||
lastcomma = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lastcomma)
|
||||
fprintf (cfile, "\"\"\n");
|
||||
fprintf (cfile, "\n}");
|
||||
count++;
|
||||
}
|
||||
c = fgetc (txtfile);
|
||||
}
|
||||
|
||||
fprintf (cfile, "\n};\n");
|
||||
fprintf (cfile, "\nconst int %s_array_length = %d;\n", argv [1], count);
|
||||
|
||||
fclose (txtfile);
|
||||
fclose (cfile);
|
||||
|
||||
fprintf (stdout, "count = %d\n", count);
|
||||
|
||||
}
|
@ -31,7 +31,7 @@ MdbHandle *mdb;
|
||||
|
||||
|
||||
if (argc<2) {
|
||||
fprintf(stderr,"Usage: prcat <file> [<objtype>]\n");
|
||||
fprintf(stderr,"Usage: %s <file> [<objtype>]\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ int j;
|
||||
int page, start, stop;
|
||||
|
||||
if (argc<4) {
|
||||
fprintf(stderr,"Usage: prdump <file> <page> <start> <stop>\n");
|
||||
fprintf(stderr,"Usage: %s <file> <page> <start> <stop>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ GList *l;
|
||||
|
||||
|
||||
if (argc<2) {
|
||||
fprintf(stderr,"Usage: prtable <file> <table>\n");
|
||||
fprintf(stderr,"Usage: %s <file> <table>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ MdbTableDef *table;
|
||||
MdbColumn *col;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf (stderr, "Usage: schema <file>\n");
|
||||
fprintf (stderr, "Usage: %s <file>\n",argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
|
72
src/util/tables.c
Normal file
72
src/util/tables.c
Normal file
@ -0,0 +1,72 @@
|
||||
/* MDB Tools - A library for reading MS Access database file
|
||||
* Copyright (C) 2000 Brian Bruns
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/* this utility dumps the schema for an existing database */
|
||||
|
||||
#include "mdbtools.h"
|
||||
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int i, j, k;
|
||||
MdbHandle *mdb;
|
||||
MdbCatalogEntry entry;
|
||||
MdbTableDef *table;
|
||||
MdbColumn *col;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf (stderr, "Usage: %s <file>\n",argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
/* open the database */
|
||||
|
||||
mdb = mdb_open (argv[1]);
|
||||
|
||||
/* read the catalog */
|
||||
|
||||
mdb_read_catalog (mdb, MDB_TABLE);
|
||||
|
||||
/* loop over each entry in the catalog */
|
||||
|
||||
for (i=0; i < mdb->num_catalog; i++)
|
||||
{
|
||||
entry = g_array_index (mdb->catalog, MdbCatalogEntry, i);
|
||||
|
||||
/* if it's a table */
|
||||
|
||||
if (entry.object_type == MDB_TABLE)
|
||||
{
|
||||
/* skip the MSys tables */
|
||||
if (strncmp (entry.object_name, "MSys", 4))
|
||||
{
|
||||
|
||||
/* make sure it's a table (may be redundant) */
|
||||
|
||||
if (!strcmp (mdb_get_objtype_string (entry.object_type), "Table"))
|
||||
{
|
||||
/* drop the table if it exists */
|
||||
fprintf (stdout, "%s ", entry.object_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fprintf (stdout, "\n");
|
||||
|
||||
mdb_free_handle (mdb);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user