diff --git a/php/classes/VOTableMgr.php b/php/classes/VOTableMgr.php index 53d1091..d77a780 100644 --- a/php/classes/VOTableMgr.php +++ b/php/classes/VOTableMgr.php @@ -13,6 +13,7 @@ class VOTableMgr { private $logVotable; // The log file. private $stream; // The stream in the VOTable private $c; // Current character position on the stream + private $is_little_endian; function __construct() { @@ -28,6 +29,7 @@ class VOTableMgr { function load($fileName) { $this->addLog("File name" . $fileName); + $this->is_little_endian = array_values(unpack('L1L', pack('V', 1)))[0] == 1; // see http://php.net/manual/en/domdocument.load.php#91384 $opts = array( @@ -212,9 +214,14 @@ class VOTableMgr { return $desc->item(0)->nodeValue; } - private function get_block_size($data_type) { - switch($data_type) { - case 'boolean': + private function get_row_size($field_node) { + $datatype = $field_node->getAttribute("datatype"); + + if($datatype == 'boolean') { + return 1; + } + + switch($datatype) { case 'unsignedByte': case 'char': $block_size = 1; @@ -233,24 +240,16 @@ class VOTableMgr { $block_size = 8; break; case 'double_complex': - $block_size = 8; - break; + $block_size = 16; default: $block_size = 0; break; } - return $block_size; - } - private function get_row_size($field_node) { - $datatype = $field_node->getAttribute("datatype"); - $block_size = $this->get_block_size($datatype); if($field_node->getAttribute("arraysize") == NULL) { $array_size = $block_size; } else if("*" == $field_node->getAttribute("arraysize")) { - // echo "substr: " . json_encode(substr($this->stream, $this->c, 4)) . "\n"; $array_size = unpack("Ns", substr($this->stream, $this->c, 4))["s"] * $block_size; - // echo "array_size: $array_size\n"; $this->c+=4; } else { $array_size = (int)($field_node->getAttribute("arraysize")) * $block_size; @@ -270,7 +269,6 @@ class VOTableMgr { if($query_stream == NULL) return NULL; $this->stream = base64_decode($query_stream->textContent); - // echo "stream: " . json_encode($this->stream) . "\n"; $stream_len = strlen($this->stream); if($stream_len == 0) return NULL; @@ -293,34 +291,85 @@ class VOTableMgr { return $data; } + private function JDTodate($jd) { + list($month, $day, $year) = split('/', JDToGregorian($jd)); + return "$day/$month/$year"; + } + + # because unpack for floats is hardware dependant + // private function big_endian_unpack ($format, $data) { + // $ar = unpack ($format, $data); + // $vals = array_values ($ar); + // echo "vals: " . json_encode($vals) . "\n"; + // $f = explode ('/', $format); + // $i = 0; + // foreach ($f as $f_k => $f_v) { + // $repeater = intval (substr ($f_v, 1)); + // + // if ($repeater == 0) { + // $repeater = 1; + // } + // if ($f_v{1} == '*') { + // $repeater = count ($ar) - $i; + // } + // if ($f_v{0} != 'd') { + // $i += $repeater; continue; + // } + // $j = $i + $repeater; + // + // for ($a = $i; $a < $j; ++$a) { + // $p = pack ('d', $vals[$i]); + // $p = strrev ($p); + // ++$i; + // } + // } + // $a = 0; + // foreach ($ar as $ar_k => $ar_v) { + // $ar[$ar_k] = $vals[$a]; + // ++$a; + // } + // return $ar; + // } + private function process_datablock($field_node) { $data_type = $field_node->getAttribute("datatype"); $row_size = $this->get_row_size($field_node); - // echo "datatype: " . $field_node->getAttribute("datatype") . "\n"; - // echo "Row size: $row_size\n"; + $substr = substr($this->stream, $this->c, $row_size); + switch ($data_type) { case 'boolean': case 'unsignedByte': - $res = $this->stream[$this->c] == "T" || $this->stream[$this->c] == "t" || $this->stream[$this->c] == "1"; + $b = $substr; + $res = $b == "T" || $b == "t" || $b == "1"; break; case 'char': case 'unicodeChar': - $res = $row_size !=0 ? substr($this->stream, $this->c, $row_size) : NULL; + $res = $row_size!=0 ? $substr : NULL; break; case 'short': - $res = unpack("ss", substr($this->stream, $this->c, 2))["s"]; + $res = unpack("ss", $substr)["s"]; break; case 'int': - $res = unpack("Ns", substr($this->stream, $this->c, 4))["s"]; + $res = unpack("Ns", $substr)["s"]; break; case 'long': - $res = unpack("Js", substr($this->stream, $this->c, 8))["s"]; // /!\ J -> PHP 5.6 only + $res = unpack("Js", $substr)["s"]; // /!\ J -> PHP 5.6 only break; case 'float': - $res = unpack("fs", substr($this->stream, $this->c, 4))["s"]; + $res = unpack("fs", $substr)["s"]; + + // If machine is little endian: + if($this->is_little_endian) { + $res = unpack('f1f', strrev(pack('f', $res)))["f"]; + } break; case 'double': - $res = unpack("ds", substr($this->stream, $this->c, 8))["s"]; + $res = unpack("ds", $substr)["s"]; + + // If machine is little endian: + if($this->is_little_endian) { + $res = unpack('d1d', strrev(pack('d', $res)))["d"]; + } break; default: $res = NULL; -- libgit2 0.21.2