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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2004 by Marco van de Voort
member of the Free Pascal development team.
An implementation for unit convutils, which converts between
units and simple combinations of them.
Based on a guessed interface derived from some programs on the web. (Like
Marco Cantu's EuroConv example), so things can be a bit Delphi
incompatible. Also part on Delphibasics.co.uk.
Quantities are mostly taken from my HP48g/gx or the unix units program
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.
**********************************************************************}
unit convutils;
interface
{$mode objfpc}
{$H+}
{$ifndef FPUNONE}
Type TConvType = type Integer;
TConvFamily = type Integer;
TConvFamilyArray = array of TConvFamily;
TConvTypeArray = array of TConvType;
TConversionProc = function(const AValue: Double): Double;
TConvUtilFloat = double;
Function RegisterConversionFamily(Const S : String):TConvFamily;
Function RegisterConversionType(Fam:TConvFamily;Const S:String;Value:TConvUtilFloat):TConvType;
function Convert ( const Measurement : Double; const FromType, ToType : TConvType ) :TConvUtilFloat;
function Convert ( const Measurement : Double; const FromType1, FromType2, ToType1, ToType2 : TConvType ) :TConvUtilFloat;
function ConvFamilyToDescription(const AFamily: TConvFamily): string;
function ConvTypeToDescription(const AType: TConvType): string;
procedure GetConvFamilies(out AFamilies: TConvFamilyArray);
procedure GetConvTypes(const AFamily: TConvFamily; out ATypes: TConvTypeArray);
Type
TConvTypeInfo = Class(Tobject)
private
FDescription : String;
FConvFamily : TConvFamily;
FConvType : TConvType;
public
Constructor Create(Const AConvFamily : TConvFamily;const ADescription:String);
function ToCommon(const AValue: Double) : Double; virtual; abstract;
function FromCommon(const AValue: Double) : Double; virtual; abstract;
property ConvFamily : TConvFamily read FConvFamily;
property ConvType : TConvType read FConvType;
property Description: String read FDescription;
end;
TConvTypeFactor = class(TConvTypeInfo)
private
FFactor: Double;
protected
property Factor: Double read FFactor;
public
constructor Create(const AConvFamily: TConvFamily; const ADescription: string;
const AFactor: Double);
function ToCommon(const AValue: Double): Double; override;
function FromCommon(const AValue: Double): Double; override;
end;
TConvTypeProcs = class(TConvTypeInfo)
private
FToProc: TConversionProc;
FFromProc: TConversionProc;
public
constructor Create(const AConvFamily: TConvFamily; const ADescription: string;
const AToProc, AFromProc: TConversionProc);
function ToCommon(const AValue: Double): Double; override;
function FromCommon(const AValue: Double): Double; override;
end;
Implementation
Type ResourceData = record
Description : String;
Value : TConvUtilFloat;
Fam : TConvFamily;
end;
var TheUnits : array of ResourceData =nil;
TheFamilies : array of string =nil;
function ConvFamilyToDescription(const AFamily: TConvFamily): string;
begin
result:='';
if AFamily<length(TheFamilies) then
result:=TheFamilies[AFamily];
end;
procedure GetConvFamilies(out AFamilies: TConvFamilyArray);
var i : integer;
begin
setlength(AFamilies,length(thefamilies));
for i:=0 to length(TheFamilies)-1 do
AFamilies[i]:=i;
end;
procedure GetConvTypes(const AFamily: TConvFamily; out ATypes: TConvTypeArray);
var i,j,nrTypes:integer;
begin
nrTypes:=0;
for i:=0 to length(TheUnits)-1 do
if TheUnits[i].fam=AFamily Then
inc(nrTypes);
setlength(atypes,nrtypes);
j:=0;
for i:=0 to length(TheUnits)-1 do
if TheUnits[i].fam=AFamily Then
begin
atypes[j]:=i;
inc(j);
end;
end;
function ConvTypeToDescription(const AType: TConvType): string;
Begin
result:='';
if AType<length(TheUnits) then
result:=TheUnits[AType].Description;
end;
Function RegisterConversionFamily(Const S:String):TConvFamily;
var i,l : Longint;
begin
l:=Length(TheFamilies);
If l=0 Then
begin
SetLength(TheFamilies,1);
TheFamilies[0]:=S;
Result:=0;
end
else
begin
i:=0;
while (i<l) and (s<>TheFamilies[i]) do inc(i);
if i=l Then
begin
SetLength(TheFamilies,l+1);
TheFamilies[l]:=s;
end;
Result:=i;
end;
end;
Function CheckFamily(i:TConvFamily):Boolean;
begin
Result:=i<Length(TheFamilies);
end;
const macheps=1E-9;
Function RegisterConversionType(Fam:TConvFamily;Const S:String;Value:TConvUtilFloat):TConvType;
var l1 : Longint;
begin
If NOT CheckFamily(Fam) Then exit(-1); // family not registered.
if (value+1.0)<macheps then // not properly defined yet.
exit(-1);
l1:=length(theunits);
Setlength(theunits,l1+1);
theunits[l1].description:=s;
theunits[l1].value:=value;
theunits[l1].fam:=fam;
Result:=l1;
end;
function SearchConvert(TheType:TConvType; var r:ResourceData):Boolean;
var l1 : longint;
begin
l1:=length(TheUnits);
if thetype>=l1 then
exit(false);
r:=theunits[thetype];
result:=true;
end;
function Convert ( const Measurement : Double; const FromType, ToType : TConvType ) :TConvUtilFloat;
var
fromrec,torec : resourcedata;
begin
if not SearchConvert(fromtype,fromrec) then
exit(-1.0); // raise exception?
if not SearchConvert(totype,torec) then
exit(-1.0); // raise except?
if fromrec.fam<>torec.fam then
exit(-1.0);
result:=Measurement*fromrec.value/torec.value;
end;
function Convert ( const Measurement : Double; const FromType1, FromType2, ToType1, ToType2 : TConvType ) :TConvUtilFloat;
var
fromrec1,fromrec2,torec1 ,
torec2 : resourcedata;
begin
if not SearchConvert(fromtype1,fromrec1) then
exit(-1.0); // raise exception?
if not SearchConvert(totype1,torec1) then
exit(-1.0); // raise except?
if not SearchConvert(fromtype2,fromrec2) then
exit(-1.0); // raise exception?
if not SearchConvert(totype2,torec2) then
exit(-1.0); // raise except?
if (fromrec1.fam<>torec1.fam) or (fromrec1.fam<>torec1.fam) then
exit(-1.0);
result:=Measurement*(fromrec1.value/fromrec2.value)/(torec1.value/torec2.value);
end;
Constructor TConvTypeInfo.Create(Const AConvFamily : TConvFamily;const ADescription:String);
begin
FDescription:=ADescription;
FConvFamily :=AConvFamily;
end;
constructor TConvTypeFactor.Create(const AConvFamily: TConvFamily; const ADescription: string;const AFactor: Double);
begin
inherited create(AConvFamily,ADescription);
FFactor:=AFactor;
end;
function TConvTypeFactor.ToCommon(const AValue: Double): Double;
begin
result:=AValue * FFactor;
end;
function TConvTypeFactor.FromCommon(const AValue: Double): Double;
begin
result:=AValue / FFactor;
end;
constructor TConvTypeProcs.Create(const AConvFamily: TConvFamily; const ADescription: string; const AToProc, AFromProc: TConversionProc);
begin
inherited create(AConvFamily,ADescription);
ftoproc:=AToProc;
ffromproc:=AFromProc;
end;
function TConvTypeProcs.ToCommon(const AValue: Double): Double;
begin
result:=FTOProc(Avalue);
end;
function TConvTypeProcs.FromCommon(const AValue: Double): Double;
begin
result:=FFromProc(Avalue);
end;
finalization
setlength(theunits,0);
setlength(thefamilies,0);
{$else}
implementation
{$endif}
end.
|