Skip to content

Commit 635fcc4

Browse files
authored
Merge pull request #48 from lawson89/main
calculate temp in one step
2 parents 7e40fe6 + 02e6201 commit 635fcc4

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

entries/rlawson/src/parser.pas

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ implementation
2929
BUFFER_SIZE = READ_SIZE + 1024;
3030
REC_SEP: char = ';';
3131
LF: char = chr(10);
32-
ANSI_ZERO: integer = 48;
3332
DECIMAL_POINT: char = '.';
3433
NEGATIVE_SIGN: char = '-';
3534

@@ -47,6 +46,7 @@ procedure ProcessMeasurements(var buffer: array of char; bufferLength: integer;
4746
// read the city by looking for semicolon
4847
city := '';
4948
cityStart := idx;
49+
Inc(idx); // city has to be at least one character
5050
while buffer[idx] <> REC_SEP do Inc(idx);
5151
SetString(city, @buffer[cityStart], (idx - cityStart));
5252
// parse the temp reading
@@ -58,17 +58,20 @@ procedure ProcessMeasurements(var buffer: array of char; bufferLength: integer;
5858
currentTempSign := -1;
5959
Inc(idx);
6060
end;
61-
// look ahead - is decimal point 2 spaces away then we have two digits
61+
// look ahead - is decimal point 2 spaces away then we have temp = dd.d
62+
// other wise d.d
6263
temp := 0;
6364
if buffer[idx + 2] = DECIMAL_POINT then
6465
begin
65-
temp := 100 * (byte(buffer[idx]) - ANSI_ZERO);
66-
Inc(idx);
66+
temp := currentTempSign * (100 * Integer(buffer[idx]) + 10 *
67+
Integer(buffer[idx + 1]) + Integer(buffer[idx + 3]) - 5328);
68+
idx := idx + 6;
69+
end
70+
else
71+
begin
72+
temp := currentTempSign * (10 * Integer(buffer[idx]) + Integer(buffer[idx + 2]) - 528);
73+
idx := idx + 5;
6774
end;
68-
temp := temp + 10 * (byte(buffer[idx]) - ANSI_ZERO);
69-
idx := idx + 2;
70-
temp := currentTempSign * (temp + (byte(buffer[idx]) - ANSI_ZERO));
71-
idx := idx + 3;
7275
reading := results.Find(city);
7376
if reading = nil then
7477
begin

0 commit comments

Comments
 (0)