summaryrefslogtreecommitdiff
path: root/src/hdb/cvt.cc
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
committerHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
commit5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch)
tree1a81af141708b826e9c61e8a04019994fcca8298 /src/hdb/cvt.cc
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/hdb/cvt.cc')
-rw-r--r--src/hdb/cvt.cc83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/hdb/cvt.cc b/src/hdb/cvt.cc
new file mode 100644
index 0000000..4788bf1
--- /dev/null
+++ b/src/hdb/cvt.cc
@@ -0,0 +1,83 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <hdb/db.h>
21using namespace std;
22
23const mstring HEAD10="HTC DB File Format version 1.0\n";
24const mstring HEAD11="HDB/1.1";
25
26static fp_stream merr(stderr);
27
28static void Read(FILE* fp,mstring& s,int version) {
29 int n; fread(&n,4,1,fp);
30 switch(version) {
31 case 10:
32 s.resize(n);
33 fread(s.data(),1,n,fp);
34 break;
35 case 11:
36 s.resize(n-1);
37 fread(s.data(),1,n,fp);
38 s.resize(strlen(s.data()));
39 if(s.size()>=n) THROW("dbmap: assertion failed on dimension");
40 break;
41 default:
42 THROW("read: bad HDB version");
43 break;
44 }
45}
46
47void Convert(dbmap& db,const char* path)
48{
49 FILE* fp=fopen(path,"rb");
50 char line[1024];
51 fgets(line,1000,fp);
52 int version=0;
53 if(sref(line)==HEAD10) { fgetc(fp); version=10; }
54 else if(First(sref(line))==HEAD11) { version=11; }
55 if(version==0) THROW("hdbcvt: unknown HDB format");
56 int tables;
57 mstring s;
58 fread(&tables,4,1,fp);
59 for(int i=0;i<tables;i++) {
60 Read(fp,s,version);
61 db.InsertTable(i,s);
62 }
63 fread(&tables,4,1,fp);
64 if(tables!=db.Tables()) THROW("hdbcvt: bad table num\n");
65 spile defs(tables);
66 for(int i=0;i<tables;i++) Read(fp,defs[i],version);
67 merr<<"db: NOTE - default values no longer supported\n";
68 int index;
69 fread(&index,4,1,fp);
70 mstring compname;
71 Read(fp,compname,version);
72 db.Order(db.Table(index),compname);
73 int records;
74 fread(&records,4,1,fp);
75 for(int i=0;i<records;i++) {
76 int n; fread(&n,4,1,fp);
77 if(n!=tables) THROW("hdbcvt: bad table num\n");
78 spile rec(n);
79 for(int j=0;j<n;j++) Read(fp,rec[j],version);
80 int ok=db.Insert(rec,1);
81 if(ok<0) merr<<"hdbcvt: bad index ["<<rec[index]<<"] - not unique; skipped\n";
82 }
83}