-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBF2C.PAS
202 lines (190 loc) · 4.92 KB
/
DBF2C.PAS
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2023
@website(https://www.gladir.com/c-tools)
@abstract(Target: Turbo Pascal 7, Free Pascal)
}
Program DBF2C;
{$A-}
Uses DOS,Strings;
Function TrimL(S:String):String;
Var
I:Byte;
Begin
For I:=1to Length(S)do Begin
If S[I]<>' 'Then Begin
TrimL:=Copy(S,I,255);
Exit;
End;
End;
TrimL:=S;
End;
Function TrimR(s:String):String;
Var
i:Integer;
Begin
i:=Length(s);
While (i>0)and(s[i]in[#9,' '])do Dec(i);
s[0]:=Chr(i);
TrimR:=S;
End;
Function Trim(s:String):String;Begin
Trim:=TrimL(TrimR(s));
End;
Function Path2Name(S:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(S,D,N,E);
Path2Name:=N;
End;
Function Path2Ext(S:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(S,D,N,E);
Path2Ext:=E;
End;
Type
{Structure de fichier DBase III}
DBaseIIIFileHeaderRec=Record
HeadType,Year,Month,Day:Byte;
RecordCount:LongInt;
HeaderLength,RecordSize:Integer;
Fill:Array[1..20]of Byte;
End;
DBaseIIIFieldRec=Record
FieldName:Array[1..11]of Char;
FieldType:Char;
Spare1,Spare2:Integer;
Width,Dec:Byte;
WorkSpace:Array[1..14]of Byte;
End;
Var
SourceDBF:File;
TargetSQL:Text;
ByteReaded:Word;
HDBase:DBaseIIIFileHeaderRec; { Structure de l'entete d'un fichier DBase }
FieldDBase:DBaseIIIFieldRec; { Structure d'un champ de DBase }
BufOfs,FieldSize,K:Integer;
I,J,FP,NumField:LongInt;
FieldDBaseList:Array[0..127]of DBaseIIIFieldRec;
Buffer:Array[0..16384]of Byte;
FileName,TableName,TStr:String;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('DBF2C - Cette commande permet de convertir un fichier DBF en C');
WriteLn;
WriteLn('Syntaxe : DBF2C source.DBF');
End
Else
Begin
TableName:=Path2Name(ParamStr(1));
FileName:=FExpand(ParamStr(1));
If Path2Ext(FileName)=''Then FileName:=FileName+'.DBF';
FillChar(FieldDBaseList,SizeOf(FieldDBaseList),0);
{$I-}Assign(SourceDBF,FileName);
Reset(SourceDBF,1);{$I+}
If IoResult<>0 Then Begin
WriteLn('Fichier ',FileName,' introuvable !');
Halt;
End;
FillChar(HDBase,SizeOf(HDBase),0);
BlockRead(SourceDBF,HDBase,SizeOf(HDBase),ByteReaded);
If HDBase.HeadType=$3Then Begin { Fichier DBase III+ ? }
FP:=SizeOf(DBaseIIIFileHeaderRec);
NumField:=0;
WriteLn('typedef struct ',TableName,'Rec {');
Repeat
Seek(SourceDBF,FP);
FillChar(FieldDBase,SizeOf(FieldDBase),0);
BlockRead(SourceDBF,FieldDBase,SizeOf(FieldDBase),ByteReaded);
FieldDBaseList[NumField]:=FieldDBase;
Inc(FP,SizeOf(FieldDBase));
If FieldDBase.FieldName[1]=#13 Then Break;
Inc(NumField);
If(FP>FileSize(SourceDBF))Then Break;
Until FieldDBase.FieldName[1]=#13;
If NumField<1 Then Begin
WriteLn('Aucun champ present !');
Halt;
End;
For I:=0 To NumField-1 do Begin
Write(' ':2);
Case(FieldDBaseList[I].FieldType)of
'C':Write('char');
'N':Write('double ');
'I':Write('int');
'L':Write('char');
End;
Write(' ',Trim(StrPas(@FieldDBaseList[I].FieldName)));
Case(FieldDBaseList[I].FieldType)of
'C':Write('[',FieldDBaseList[I].Width,']');
End;
If I<NumField-1 Then Write(';');
WriteLn;
End;
WriteLn('};');
WriteLn;
If HDBase.RecordSize>SizeOf(Buffer)Then Begin
WriteLn('Enregistrement trop grand !');
Halt;
End;
If HDBase.HeaderLength=0Then Seek(SourceDBF,FP-SizeOf(FieldDBase)+2)
Else Seek(SourceDBF,HDBase.HeaderLength+1);
If HDBase.RecordCount>0 Then Begin
WriteLn('const struct ',TableName,'Rec ',TableName,'[',HDBase.RecordCount-1,']={');
End;
For J:=1 to HDBase.RecordCount do Begin
Write(' ':2,'{');
BlockRead(SourceDBF,Buffer,HDBase.RecordSize,ByteReaded);
BufOfs:=0;
For I:=0 To NumField-1 do Begin
If I>0 Then Write(' ':3);
Write('.',Trim(StrPas(@FieldDBaseList[I].FieldName)),'=');
Case FieldDBaseList[I].FieldType of
'C','N':FieldSize:=FieldDBaseList[I].Width;
'I':FieldSize:=2;
'L':FieldSize:=1;
Else FieldSize:=0;
End;
Case FieldDBaseList[I].FieldType of
'C':Begin
Write('"');
TStr:='';
For K:=0 to FieldDBaseList[I].Width-1 do Begin
If Buffer[BufOfs+K]=0 Then Break;
TStr:=TStr+Char(Buffer[BufOfs+K]);
End;
Write(TrimR(TStr));
Write('"');
End;
'N':Begin
TStr:='';
For K:=0 to FieldDBaseList[I].Width-1 do Begin
If Buffer[BufOfs+K]=0 Then Break;
TStr:=TStr+Char(Buffer[BufOfs+K]);
End;
If Trim(TStr)=''Then Write('0')
Else Write(Trim(TStr));
End;
'I':Write(Buffer[BufOfs]+Buffer[BufOfs+1]*256);
'L':Write(Buffer[BufOfs]);
End;
If I<NumField-1 Then WriteLn(',');
Inc(BufOfs,FieldSize);
End;
Write('}');
If J<HDBase.RecordCount Then WriteLn(',');
End;
If HDBase.RecordCount>0 Then Begin
WriteLn;
WriteLn(' };');
End;
End;
Close(SourceDBF);
End;
END.