
(********************************************************************)
(*                                                                  *)
(*  binary.s7i    Functions to read binary data from string or file *)
(*  Copyright (C) 2009  Thomas Mertes                               *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library is distributed in the hope that it    *)
(*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
(*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(*  PURPOSE.  See the GNU Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser General       *)
(*  Public License along with this program; if not, write to the    *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


include "file.s7i";


const func string: get_asciiz (in string: stri, inout integer: currPos) is func
  result
    var string: result is "";
  local
    var integer: startPos is 0;
  begin
    startPos := currPos;
    while stri[currPos] <> '\0\' do
      incr(currPos);
    end while;
    result := stri[startPos .. pred(currPos)];
    incr(currPos);
  end func;


(**
 *  Read two bytes from a file and return their little endian value.
 *)
const func integer: get_uint16_le (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 2);
    number := ord(stri[1]) +
              ord(stri[2]) * 256;
  end func;


(**
 *  Read four bytes from a file and return their little endian value.
 *)
const func integer: get_uint32_le (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 4);
    number := ord(stri[1]) +
              ord(stri[2]) * 256 +
              ord(stri[3]) * 65536 +
              ord(stri[4]) * 16777216;
  end func;


(**
 *  Read two bytes from a file and return their big endian value.
 *)
const func integer: get_uint16_be (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 2);
    number := ord(stri[1]) * 256 +
              ord(stri[2]);
  end func;


(**
 *  Read four bytes from a file and return their big endian value.
 *)
const func integer: get_uint32_be (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 4);
    number := ord(stri[1]) * 16777216 +
              ord(stri[2]) * 65536 +
              ord(stri[3]) * 256 +
              ord(stri[4]);
  end func;
