1
1
<?php
2
2
/*
3
+ Copyrigth 2017-2024 Jason McCormick N8EI
3
4
Copyright 2011-2013 Jason Harris KJ4IWX
4
5
5
6
Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,16 +33,21 @@ class ADIF_Parser
32
33
33
34
public function initialize () //this function locates the <EOH>
34
35
{
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 )
37
40
{
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
+
43
50
//get headers
44
-
45
51
$ this ->i = 0 ;
46
52
$ in_tag = false ;
47
53
$ tag = "" ;
@@ -106,15 +112,15 @@ public function initialize() //this function locates the <EOH>
106
112
107
113
$ this ->i ++;
108
114
109
- };
115
+ }
110
116
111
117
$ this ->i = $ pos +5 ; //iterate past the <eoh>
112
118
if ($ this ->i >= strlen ($ this ->data )) //is this the end of the file?
113
119
{
114
120
echo "Error: ADIF File Does Not Contain Any QSOs " ;
115
- return 0 ;
116
- };
117
- return 1 ;
121
+ return false ;
122
+ }
123
+ return true ;
118
124
}
119
125
120
126
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
124
130
125
131
public function load_from_file ($ fname ) //allows the user to accept a filename as input
126
132
{
127
- file_put_contents ($ fname , str_replace ("<EOR> " , "<eor> " , file_get_contents ($ fname )));
128
133
$ this ->data = file_get_contents ($ fname );
129
134
}
130
135
@@ -192,11 +197,13 @@ public function get_record()
192
197
{
193
198
return array (); //return nothing
194
199
};
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 )
197
203
{
198
- return array (); //return nothing
199
- };
204
+ return array ();
205
+ }
206
+ $ end = $ matches [0 ][1 ];
200
207
$ record = substr ($ this ->data , $ this ->i , $ end -$ this ->i );
201
208
$ this ->i = $ end +5 ;
202
209
return $ this ->record_to_array ($ record ); //process and return output
0 commit comments