/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
 
 This file is part of LiveCode.
 
 LiveCode is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License v3 as published by the Free
 Software Foundation.
 
 LiveCode 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 General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
 along with LiveCode.  If not see <http://www.gnu.org/licenses/>.  */

/*  
This library consists of the operations on binary strings provided by the standard library of LiveCode Builder.
*/

module com.livecode.binary

use com.livecode.foreign

public foreign handler MCBinaryExecPutBytesBefore(in Source as Data, inout Target as Data) returns nothing binds to "<builtin>"
public foreign handler MCBinaryExecPutBytesAfter(in Source as Data, inout Target as Data) returns nothing binds to "<builtin>"

public foreign handler MCBinaryEvalConcatenateBytes(in Left as Data, in Right as Data, out Result as Data) returns nothing binds to "<builtin>"

public foreign handler MCBinaryEvalContainsBytes(in Target as Data, in Needle as Data, out Value as CBool) returns nothing binds to "<builtin>"

public foreign handler MCBinaryEvalIsEqualTo(in Left as Data, in Right as Data, out Value as CBool) returns nothing binds to "<builtin>"
public foreign handler MCBinaryEvalIsNotEqualTo(in Left as Data, in Right as Data, out Value as CBool) returns nothing binds to "<builtin>"
public foreign handler MCBinaryEvalIsLessThan(in Left as Data, in Right as Data, out Value as CBool) returns nothing binds to "<builtin>"
public foreign handler MCBinaryEvalIsGreaterThan(in Left as Data, in Right as Data, out Value as CBool) returns nothing binds to "<builtin>"

public foreign handler MCDataEvalEmpty(out Value as Data) returns nothing binds to "<builtin>"
--

/*
Summary:            Prepends <Source> bytes to <Target> bytes.

Source: 			An expression which evaluates to binary data.
Target: 			An expression which evaluates to a binary container.
	
Description:
Use to insert bytes into data without replacement. Can be used either with a chunk expression to insert at a specified location, or without to prepend to the target data.

Tags: Binary
*/

syntax PutBytesBefore is statement
    "put" <Source: Expression> "before" <Target: Expression>
begin
    MCBinaryExecPutBytesBefore(Source, Target)
end syntax

/*
Summary:            Appends <Source> bytes to <Target> bytes.

Source: 			An expression which evaluates to binary data.
Target: 			An expression which evaluates to a binary container.

Related: 			PutBytesAfter

Description:
Use to insert bytes into data without replacement. Can be used either with a chunk expression to insert at a specified location, or without to append to the target data.

Tags: Binary
*/

syntax PutBytesAfter is statement
    "put" <Source: Expression> "after" <Target: Expression>
begin
    MCBinaryExecPutBytesAfter(Source, Target)
end syntax

--

/*
Summary:            Concatenates <Left> and <Right>.

Left: 				An expression which evaluates to binary data.
Right: 				An expression which evaluates to binary data.

Returns:			Binary data consisting of the value of the left hand expression with the value of the right hand expression appended to the end.

Description:
The result consists of the bytes of <Left> followed by those of <Right>.

Tags: Binary
*/

syntax ConcatenateBytes is left binary operator with precedence 2
    <Left: Expression> "&" <Right: Expression>
begin
    MCBinaryEvalConcatenateBytes(Left, Right, output)
end syntax

--

/*
Summary:    Determines whether <Left> and <Right> are equal or not.

Left:       An expression which evaluates to binary data.
Right:      An expression which evaluates to binary data.

Returns: 	Returns true if the result of evaluating <Left> is the same as that of <Right>, and false otherwise.

Description:
Performs a byte by byte comparison of <Left> and <Right>, returning false if there is any difference.

Tags: Binary
*/

syntax DataIsData is neutral binary operator with precedence 7
    <Left: Expression> "is" <Right: Expression>
begin
    MCBinaryEvalIsEqualTo(Left, Right, output)
end syntax

/*
Summary:    Determines whether <Left> and <Right> are equal or not.

Left:       An expression which evaluates to binary data.
Right:      An expression which evaluates to binary data.

Returns: 	Returns false if the result of evaluating <Left> is the same as that of <Right>, and true otherwise.

Description:
Performs a byte by byte comparison of <Left> and <Right>, returning true if there is any difference.

Tags: Binary
*/

syntax DataIsNotData is neutral binary operator with precedence 7
    <Left: Expression> "is not" <Right: Expression>
begin
    MCBinaryEvalIsNotEqualTo(Left, Right, output)
end syntax

/*
Summary:    Determines whether <Left> is less than <Right> under a byte by byte comparison

Left:       An expression which evaluates to binary data.
Right:      An expression which evaluates to binary data.


Returns: 	True if <Left> is less than <Right>, and false otherwise.

Description:
<Left> is less than <Right> if they are not equal, and the first byte in <Right> that is not equal to the corresponding byte in <Left> is of greater value.
            
Tags: Binary
*/

syntax DataIsLessThanData is neutral binary operator with precedence 5
    <Left: Expression> "<" <Right: Expression>
begin
    MCBinaryEvalIsLessThan(Left, Right, output)
end syntax

/*
Summary:    Determines whether <Left> is greater than <Right> under a byte by byte comparison

Left:       An expression which evaluates to binary data.
Right:      An expression which evaluates to binary data.


Returns: 	True if <Left> is greater than <Right>, and false otherwise.

Description:
<Left> is greater than <Right> if they are not equal, and the first byte in <Left> that is not equal to the corresponding byte in <Right> is of greater value.

Tags: Binary
*/

syntax DataIsGreaterThanData is neutral binary operator with precedence 5
    <Left: Expression> ">" <Right: Expression>
begin
    MCBinaryEvalIsGreaterThan(Left, Right, output)
end syntax

--

/* 

Summary: 		Designates data of length 0.

Example:
	variable tVar as Data
	variable tCount as Number
	put the empty data into tVar
	put the number of bytes in tVar into tCount -- tCount is 0
	
Description:
Use ```the empty data``` to initialise a data variable.

Tags: Binary

*/

syntax EmptyData is expression
	"the" "empty" "data"
begin
	MCDataEvalEmpty(output)
end syntax

end module
