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
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <hdb/db.h>
using namespace std;
const mstring HEAD10="HTC DB File Format version 1.0\n";
const mstring HEAD11="HDB/1.1";
static fp_stream merr(stderr);
static void Read(FILE* fp,mstring& s,int version) {
int n; fread(&n,4,1,fp);
switch(version) {
case 10:
s.resize(n);
fread(s.data(),1,n,fp);
break;
case 11:
s.resize(n-1);
fread(s.data(),1,n,fp);
s.resize(strlen(s.data()));
if(s.size()>=n) THROW("dbmap: assertion failed on dimension");
break;
default:
THROW("read: bad HDB version");
break;
}
}
void Convert(dbmap& db,const char* path)
{
FILE* fp=fopen(path,"rb");
char line[1024];
fgets(line,1000,fp);
int version=0;
if(sref(line)==HEAD10) { fgetc(fp); version=10; }
else if(First(sref(line))==HEAD11) { version=11; }
if(version==0) THROW("hdbcvt: unknown HDB format");
int tables;
mstring s;
fread(&tables,4,1,fp);
for(int i=0;i<tables;i++) {
Read(fp,s,version);
db.InsertTable(i,s);
}
fread(&tables,4,1,fp);
if(tables!=db.Tables()) THROW("hdbcvt: bad table num\n");
spile defs(tables);
for(int i=0;i<tables;i++) Read(fp,defs[i],version);
merr<<"db: NOTE - default values no longer supported\n";
int index;
fread(&index,4,1,fp);
mstring compname;
Read(fp,compname,version);
db.Order(db.Table(index),compname);
int records;
fread(&records,4,1,fp);
for(int i=0;i<records;i++) {
int n; fread(&n,4,1,fp);
if(n!=tables) THROW("hdbcvt: bad table num\n");
spile rec(n);
for(int j=0;j<n;j++) Read(fp,rec[j],version);
int ok=db.Insert(rec,1);
if(ok<0) merr<<"hdbcvt: bad index ["<<rec[index]<<"] - not unique; skipped\n";
}
}
|