forked from mdbtools/mdbtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdb-header.c
More file actions
159 lines (133 loc) · 4.63 KB
/
mdb-header.c
File metadata and controls
159 lines (133 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* this utility dumps the C headers for an existing database */
/* it will create three files - types.h and dump_types.[ch] */
#include <string.h>
#include "mdbtools.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");
}
int
main (int argc, char **argv)
{
unsigned int i, j, k;
unsigned int unsupported = 0;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbColumn *col;
FILE *typesfile;
FILE *headerfile;
FILE *cfile;
fputs("mdb-header is deprecated and will disappear in a future version of mdbtools.\n", stderr);
fputs("Please drop us a line if you have any use of it.\n", stderr);
fputs("See https://github.com/mdbtools/mdbtools/issues/197\n", stderr);
fputs("\n", stderr);
if (argc < 2) {
fprintf (stderr, "Usage: %s <file>\n",argv[0]);
exit (1);
}
/* open the database */
mdb = mdb_open (argv[1], MDB_NOFLAGS);
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_ptr_array_index (mdb->catalog, i);
if (!mdb_is_user_table(entry))
continue;
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);
if (!table) {
fprintf(stderr, "Error: Table %s does not exist in this database.\n", entry->object_name);
/* Don't bother clean up memory before exit */
exit(1);
}
/* 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;
default:
fprintf(stderr, "ERROR: unsupported type: 0x%02x\n", col->col_type);
unsupported = 1;
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");
mdb_free_tabledef(table);
}
fclose (headerfile);
fclose (typesfile);
fclose (cfile);
mdb_close (mdb);
exit(unsupported);
}