(*
/*
CLASS {NATIVE_Stream_on_C_Stream|_VCL}, NATIVE,
ABSTRACT :

IMPORT, ANYWAY: {Stream} .
IMPORT, ANYWAY: {System_functions} .
IMPORT, ANYWAY: {String_functions} .
IMPORT, ANYWAY: {Mathematics} .

NATIVE : @
        "///copy NATIVE_File_stream.pas".

*/
*)

unit NATIVE_Stream_on_C_Stream;

{$I AL4_options.inc}

interface uses Classes, SysUtils, AL4, All_classes
	{$IFnDEF _D6orHigher}{$IFnDEF FPC} , Windows {$ENDIF}{$ENDIF} ;

type

    TNATIVE_Stream_on_C_Stream = class(TStream)
        C_S: C_Stream;
        function GetSize: Int64;
        	{$IFDEF _D7orHigher} override {$ELSE} reintroduce {$ENDIF};
        function Seek(
        	{$IFDEF MODERN_CODE} const Offset: Int64
            {$ELSE} Offset: Longint 
            {$ENDIF}; 
        	Origin: 
					{$IFDEF MODERN_CODE} TSeekOrigin
        			{$ELSE} Word 
        			{$ENDIF} )
        	: 
			{$IFDEF MODERN_CODE} Int64 
			{$ELSE} Longint 
			{$ENDIF}; override;
        function Read(var Buffer; Count: LongInt): LongInt; override;
        function Write(const Buffer; Count: Longint): Longint; override;
    end;

function Stream_from_C_Stream(CS: C_Stream): TNATIVE_Stream_on_C_Stream;

implementation

function TNATIVE_Stream_on_C_Stream.Seek(
            {$IFDEF MODERN_CODE} const Offset: Int64
            {$ELSE} Offset: Longint
            {$ENDIF};
            Origin: 
					{$IFDEF MODERN_CODE} TSeekOrigin
            		{$ELSE} Word 
            		{$ENDIF})
            : 
			{$IFDEF MODERN_CODE} Int64 
			{$ELSE} Longint 
			{$ENDIF};
var pos_to: st_long__C_Long_int;
	new_pos: st_long__C_Long_int;
begin
	pos_to.F_Lo := Offset;
	pos_to.F_Hi := {$IFDEF MODERN_CODE} Offset shr 32
				   {$ELSE} 0 
				   {$ENDIF};
    C_S.M_Set_pos(pos_to, Integer(Origin));
    new_pos := C_S.M_Position;
    RESULT := new_pos.F_Lo
    	{$IFDEF MODERN_CODE}
    		  or (Int64(new_pos.F_Hi) shl 32)
    	{$ENDIF} ;
end;

function TNATIVE_Stream_on_C_Stream.GetSize: Int64; 
var siz: st_long__C_Long_int;
begin
	siz := C_S.M_Size; 
    Result := siz.F_Lo
    	{$IFDEF MODERN_CODE} or (Int64(siz.F_Hi) shl 32) {$ENDIF};
end;

function TNATIVE_Stream_on_C_Stream.Read(
            var Buffer;
            Count: LongInt): LongInt; 
var BA: DynAByte;
begin
    BA := New_FixAByte_Array(nil, PByte(@Buffer), Count, 0, Count-1);
    try
        Result := C_S.M_Read_bytes(BA);
    finally
        BA.Free;
    end;
end;

function TNATIVE_Stream_on_C_Stream.Write(const Buffer; Count: Longint): Longint;
var BA: DynAByte; 
begin
	BA := New_FixAByte_Array(nil, PByte(@Buffer), Count, 0, Count-1);
	try
    	Result := C_S.M_Save_bytes(BA);
	finally
		BA.Free;
	end;
end;

function Stream_from_C_Stream(CS: C_Stream): TNATIVE_Stream_on_C_Stream;
begin
    Result := TNATIVE_Stream_on_C_Stream.Create;
    Result.C_S := CS;
end;

(*
/*
END
*/
*)
end.
