Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UTF-8 / multi-byte charset support to ADIF parser #830

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/libraries/AdifHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getAdifLine($qso) {

function getAdifFieldLine($adifcolumn, $dbvalue) {
if ($dbvalue != "") {
return "<" . $adifcolumn . ":" . strlen($dbvalue) . ">" . $dbvalue;
return "<" . $adifcolumn . ":" . mb_strlen($dbvalue, "UTF-8") . ">" . $dbvalue;
} else {
return "";
}
Expand Down
76 changes: 37 additions & 39 deletions application/libraries/Adif_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ADIF_Parser
public function initialize() //this function locates the <EOH>
{

$pos = stripos(strtoupper($this->data), "<EOH>");;
$pos = mb_stripos(mb_strtoupper($this->data, "UTF-8"), "<EOH>", 0, "UTF-8");

if($pos == false) //did we find the end of headers?
{
Expand All @@ -46,11 +46,11 @@ public function initialize() //this function locates the <EOH>
while($this->i < $pos)
{
//skip comments
if($this->data[$this->i] == "#")
if(mb_substr($this->data, $this->i, 1, "UTF-8") == "#")
{
while($this->i < $pos)
{
if($this->data[$this->i] == "\n")
if(mb_substr($this->data, $this->i, 1, "UTF-8") == "\n")
{
break;
}
Expand All @@ -59,38 +59,38 @@ public function initialize() //this function locates the <EOH>
}
}else{
//find the beginning of a tag
if($this->data[$this->i] == "<")
if(mb_substr($this->data, $this->i, 1, "UTF-8") == "<")
{
$this->i++;
//record the key
while($this->data[$this->i] < $pos && $this->data[$this->i] != ':')
while($this->i < $pos && mb_substr($this->data, $this->i, 1, "UTF-8") != ':')
{
$tag = $tag.$this->data[$this->i];
$tag = $tag.mb_substr($this->data, $this->i, 1, "UTF-8");
$this->i++;
}

$this->i++; //iterate past the :

//find out how long the value is

while($this->data[$this->i] < $pos && $this->data[$this->i] != '>')
while($this->i < $pos && mb_substr($this->data, $this->i, 1, "UTF-8") != '>')
{
$value_length = $value_length.$this->data[$this->i];
$value_length = $value_length.mb_substr($this->data, $this->i, 1, "UTF-8");
$this->i++;
}

$this->i++; //iterate past the >

$len = (int)$value_length;
//copy the value into the buffer
while($len > 0 && $this->i < $pos)
{
$value = $value.$this->data[$this->i];
$len--;
$this->i++;
};

$this->headers[strtolower(trim($tag))] = $value; //convert it to lowercase and trim it in case of \r
if ($this->i + $len > $pos) {
$len = $len - ($this->i + $len - $pos);
}
$value = mb_substr($this->data, $this->i, $len, "UTF-8");
$this->i = $this->i + $len;

$this->headers[mb_strtolower(trim($tag), "UTF-8")] = $value; //convert it to lowercase and trim it in case of \r
//clear all of our variables
$tag = "";
$value_length = "";
Expand All @@ -104,20 +104,20 @@ public function initialize() //this function locates the <EOH>
};

$this->i = $pos+5; //iterate past the <eoh>
if($this->i >= strlen($this->data)) //is this the end of the file?
if($this->i >= mb_strlen($this->data, "UTF-8")) //is this the end of the file?
{
echo "Error: ADIF File Does Not Contain Any QSOs";
return 0;
};

$this->datasplit = preg_split("/<eor>/i", substr($this->data, $this->i));
$this->datasplit = preg_split("/<eor>/i", mb_substr($this->data, $this->i, NULL, "UTF-8"));
return 1;
}

public function feed($input_data) //allows the parser to be fed a string
{
$this->data = $input_data;
$this->datasplit = preg_split("/<eor>/i", substr($this->data, $this->i));
$this->datasplit = preg_split("/<eor>/i", mb_substr($this->data, $this->i, NULL, "UTF-8"));
}

public function load_from_file($fname) //allows the user to accept a filename as input
Expand All @@ -129,48 +129,46 @@ public function load_from_file($fname) //allows the user to accept a filename as
public function record_to_array($record)
{
$return = array();
for($a = 0; $a < strlen($record); $a++)
for($a = 0; $a < mb_strlen($record, "UTF-8"); $a++)
{
if($record[$a] == '<') //find the start of the tag
if(mb_substr($record, $a, 1, "UTF-8") == '<') //find the start of the tag
{
$tag_name = "";
$value = "";
$len_str = "";
$len = 0;
$a++; //go past the <
while($record[$a] != ':') //get the tag
while(mb_substr($record, $a, 1, "UTF-8") != ':') //get the tag
{
$tag_name = $tag_name.$record[$a]; //append this char to the tag name
$tag_name = $tag_name.mb_substr($record, $a, 1, "UTF-8"); //append this char to the tag name
$a++;
};
$a++; //iterate past the colon
while($record[$a] != '>' && $record[$a] != ':')
while(mb_substr($record, $a, 1, "UTF-8") != '>' && mb_substr($record, $a, 1, "UTF-8") != ':')
{
$len_str = $len_str.$record[$a];
$len_str = $len_str.mb_substr($record, $a, 1, "UTF-8");
$a++;
};
if($record[$a] == ':')
if(mb_substr($record, $a, 1, "UTF-8") == ':')
{
while($record[$a] != '>')
while(mb_substr($record, $a, 1, "UTF-8") != '>')
{
$a++;
};
};
$len = (int)$len_str;
while($len > 0)
{
$a++;
$value = $value.$record[$a];
$len--;
};
$return[strtolower($tag_name)] = $value;
$a++;

$value = mb_substr($record, $a, $len, "UTF-8");
$a = $a + $len - 1;
$return[mb_strtolower($tag_name, "UTF-8")] = $value;
};
//skip comments
if($record[$a] == "#")
if(mb_substr($record, $a, 1, "UTF-8") == "#")
{
while($a < strlen($record))
while($a < mb_strlen($record, "UTF-8"))
{
if($record[$a] == "\n")
if(mb_substr($record, $a, 1, "UTF-8") == "\n")
{
break;
}
Expand All @@ -189,7 +187,7 @@ public function get_record()
return array(); //return nothing
} else {
// Is this a valid QSO?
if (stristr($this->datasplit[$this->currentarray],"<CALL:")) {
if (mb_stristr($this->datasplit[$this->currentarray], "<CALL:", false, "UTF-8")) {
return $this->record_to_array($this->datasplit[$this->currentarray++]); //process and return output
}
else {
Expand All @@ -200,9 +198,9 @@ public function get_record()

public function get_header($key)
{
if(array_key_exists(strtolower($key), $this->headers))
if(array_key_exists(mb_strtolower($key, "UTF-8"), $this->headers))
{
return $this->headers[strtolower($key)];
return $this->headers[mb_strtolower($key, "UTF-8")];
}else{
return NULL;
}
Expand Down