Skip to content

Commit 931ee46

Browse files
committed
speed improvements
1 parent 50ca25e commit 931ee46

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

load/adif_parser.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
/*
3+
Copyrigth 2017-2024 Jason McCormick N8EI
34
Copyright 2011-2013 Jason Harris KJ4IWX
45
56
Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,16 +33,21 @@ class ADIF_Parser
3233

3334
public function initialize() //this function locates the <EOH>
3435
{
35-
$pos = stripos($this->data, "<eoh>");
36-
if($pos == false) //did we find the end of headers?
36+
preg_match("/\<EOH\>/i", $this->data, $matches, PREG_OFFSET_CAPTURE);
37+
$pos = $matches[0][1];
38+
39+
if(count($matches) < 1)
3740
{
38-
echo "Error: Adif_Parser Already Initialized or No <EOH> in ADIF File";
39-
return 0;
40-
};
41-
42-
41+
echo "Error: No <EOH> found in ADIF File; file is out-of-spec";
42+
return false;
43+
}
44+
45+
if(count($matches) > 1){
46+
echo"Error: Multiple <EOH> found in ADIF file; file is out-of-spec";
47+
return false;
48+
}
49+
4350
//get headers
44-
4551
$this->i = 0;
4652
$in_tag = false;
4753
$tag = "";
@@ -106,15 +112,15 @@ public function initialize() //this function locates the <EOH>
106112

107113
$this->i++;
108114

109-
};
115+
}
110116

111117
$this->i = $pos+5; //iterate past the <eoh>
112118
if($this->i >= strlen($this->data)) //is this the end of the file?
113119
{
114120
echo "Error: ADIF File Does Not Contain Any QSOs";
115-
return 0;
116-
};
117-
return 1;
121+
return false;
122+
}
123+
return true;
118124
}
119125

120126
public function feed($input_data) //allows the parser to be fed a string
@@ -124,7 +130,6 @@ public function feed($input_data) //allows the parser to be fed a string
124130

125131
public function load_from_file($fname) //allows the user to accept a filename as input
126132
{
127-
file_put_contents($fname, str_replace("<EOR>", "<eor>", file_get_contents($fname)));
128133
$this->data = file_get_contents($fname);
129134
}
130135

@@ -192,11 +197,13 @@ public function get_record()
192197
{
193198
return array(); //return nothing
194199
};
195-
$end = stripos($this->data, "<eor>", $this->i);
196-
if($end == false) //is this the end?
200+
201+
preg_match("/\<EOR\>/i", $this->data, $matches, PREG_OFFSET_CAPTURE, $this->i);
202+
if(count($matches) < 1 )
197203
{
198-
return array(); //return nothing
199-
};
204+
return array();
205+
}
206+
$end = $matches[0][1];
200207
$record = substr($this->data, $this->i, $end-$this->i);
201208
$this->i = $end+5;
202209
return $this->record_to_array($record); //process and return output

0 commit comments

Comments
 (0)