(*
/*

CLASS {NATIVE_Stream_on_C_Stream|_KOL},
NATIVE,
ABSTRACT :

IMPORT, ANYWAY: {Stream}, {Long_int} .
IMPORT, ANYWAY: {System_functions} .

*/
*)

unit NATIVE_Stream_on_C_Stream;
{$I EXTERNAL_DEFINES.inc}

interface uses Windows, KOL, All_classes, AL4;

type
    PNATIVE_Stream_on_C_Stream = ^TNATIVE_Stream_on_C_Stream;
    TNATIVE_Stream_on_C_Stream = object(TStream)
        C_S: C_Stream;
    end;

function Stream_from_C_Stream(CS: C_Stream): PNATIVE_Stream_on_C_Stream;

implementation

function StCS_Seek(
         Strm: PStream;  
         {$IFNDEF STREAM_COMPAT} const {$ENDIF} MoveTo: TStrmMove;
         MoveMethod: TMoveMethod): TStrmSize;
var move_to: st_long__C_Long_int;
	new_pos: st_long__C_Long_int;
begin
	move_to.F_Lo := MoveTo;
	move_to.F_Hi := {$IFDEF STREAM_LARGE64}
					MoveTo shr 32
					{$ELSE}
					0
					{$ENDIF} ;
    PNATIVE_Stream_on_C_Stream(Strm).C_S.M_Set_pos(move_to, Integer(MoveMethod));
    new_pos := PNATIVE_Stream_on_C_Stream(Strm).C_S.M_Position;
    {$IFDEF STREAM_LARGE64}
    	RESULT := new_pos.F_Lo or (new_pos.F_Hi shl 32);
    {$ELSE}
    	RESULT := new_pos.F_Lo;
    {$ENDIF}
end;

function StCS_GetSize(
	Strm: PStream): TStrmSize;
var siz: st_long__C_Long_int;
begin
    siz := PNATIVE_Stream_on_C_Stream(Strm).C_S.M_Size;
    {$IFDEF STREAM_LARGE64}
    	RESULT := siz.F_Lo or (Int64(siz.F_Hi) shl 32);
    {$ELSE}
    	RESULT := siz.F_Lo;
    {$ENDIF}
end;

function StCS_Read(
        Strm: PStream;
        var Buffer; 
        {$IFNDEF STREAM_COMPAT} const {$ENDIF} Count: TStrmSize ): TStrmSize;
var BA: DynAByte;
begin
    BA := New_FixAByte_Array(nil, PByte(@Buffer), Count, 0, Count-1);
    try
        Result := PNATIVE_Stream_on_C_Stream(Strm).C_S.M_Read_bytes(BA);
    finally
        BA.Free;
    end;
end;

procedure StCS_Close(Strm: PStream);
begin
    PNATIVE_Stream_on_C_Stream(Strm).C_S.M_Close;
end;

function Stream_from_C_Stream(
        CS: C_Stream): PNATIVE_Stream_on_C_Stream;
begin
    new(Result, Create);
    Result.C_S := CS;
    Result.fMethods.fSeek := StCS_Seek;
    Result.fMethods.fGetSiz := StCS_GetSize;
    Result.fMethods.fRead := StCS_Read;
    Result.fMethods.fClose := StCS_Close;
end;

end.

(*
/*
END
*/
*)
