-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit1.pas
147 lines (127 loc) · 4.03 KB
/
unit1.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
{ Convertor - A free & open sorce unit converter
Copyright (C) 2012-2016 H. Raz [email protected]
This source 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 code 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.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
}
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DOM, XMLRead, Dialogs;
type
{ unit object will be attached to string }
TUnitData = class(TObject)
private
FName: string;
FLF: double;
FCF: double;
FABBR: string;
FInfo: string;
public
property Name: string read FName;
property LF: double read FLF;
property CF: double read FCF;
property ABBR: string read FABBR;
property Info: string read FInfo;
function ToUnit(x: double): double;
function FromUnit(x: double): double;
end;
{ category will be attached to string, and will hold the relevant
units inside a TStringList object }
TUnitCategory = class(TObject)
private
FName: string;
FUnitList: TStringList;
FLeftUnit: TUnitData;
FRightUnit: TUnitData;
FLIndx: integer;
FRIndx: integer;
public
property UnitList: TStringList read FUnitList;
property Name: string read FName;
property LeftUnit: TUnitData read FLeftUnit write FLeftUnit;
property RightUnit: TUnitData read FRightUnit write FRightUnit;
property LIndx: integer read FLIndx write FLIndx;
property RIndx: integer read FRIndx write FRIndx;
end;
procedure UnitInit;
var
CatList: TStringList; // global list to hold the categories and units
implementation
function TUnitData.FromUnit(x: double): double;
begin
Result := x * Self.LF + Self.CF;
end;
function TUnitData.ToUnit(x: double): double;
begin
Result := (x - Self.CF) / Self.LF;
end;
procedure UnitInit;
var
Doc: TXMLDocument;
Child: TDOMNode;
i, j, k, Childs: integer;
atr, val: string;
UCat: TUnitCategory;
UDat: TUnitData;
begin
try
// Read in xml file from disk
ReadXMLFile(Doc, 'UnitData.xml');
// process nodes
Childs := Doc.DocumentElement.ChildNodes.Count;
CatList := TStringList.Create;
for i := 0 to (Childs - 1) do
begin
Child := Doc.DocumentElement.ChildNodes[i];
UCat := TUnitCategory.Create;
UCat.FUnitList := TStringList.Create;
Ucat.FName := Child.Attributes.Item[0].NodeValue;
// using ChildNodes method
with Child.ChildNodes do
begin
try
for j := 0 to (Count - 1) do
begin
UDat := TUnitData.Create;
for k := 0 to (Item[j].Attributes.Length - 1) do
begin
atr := Item[j].Attributes.Item[k].NodeName;
val := AnsiToUTF8(Item[j].Attributes.Item[k].NodeValue);
case UpCase(atr[1]) of
'C': UDat.FCF := StrToFloat(val); // cf
'L': UDat.FLF := StrToFloat(val); // lf
'I': UDat.FInfo := WrapText(val, 60); // info
'A': UDat.FABBR := val; // abbr
'N': UDat.FName := val; // name
else
ShowMessage('Wrong attribute: ' + atr);
end;
end;
if UDat.FInfo <> '' then
UDat.FName := '(i) ' + UDat.FName;
UCat.FUnitList.AddObject(UDat.FName, UDat);
end;
finally
Free;
end;
end;
// populate the category list and add the units as objects
CatList.AddObject(UCat.FName, UCat);
end;
finally
Doc.Free;
CatList.Sort;
end;
end;
end.