Subject Re: [firebird-support] Blob-Handling UDF
Author Ivan Prenosil
Look at Contributed Downloads section on
http://www.ibphoenix.com
there are lots of free UDFs, many of them with source code.

> function BlobToStream(aBlob: PBlob): TStream;
...
> result := Stream;

Your UDF can't return TStream object. UDFs must return
datatypes that Firebird understand, usually properly allocated CSTRING.

Ivan



----- Original Message -----
From: "tgrundke" <tgrundke@...>
To: <firebird-support@yahoogroups.com>
Sent: Sunday, March 14, 2004 1:01 PM
Subject: [firebird-support] Blob-Handling UDF


> Hallo,
>
> I need BlobData in a UDF. When the BlobData need 2 or more memorysegments in the database then is
the result not ok.
> I have the Source from FB, but i where can i find the implementation "read a blob-segment in a
UDF"?
>
> testSource:
>
> TISC_BlobGetSegment = function(BlobHandle: PInt;
> Buffer: PChar;
> BufferSize: LongInt;
> var ResultLength: LongInt): Short; cdecl;
> TISC_BlobPutSegment = procedure(BlobHandle: PInt;
> Buffer: PChar;
> BufferLength: Short); cdecl;
> TBlob = record
> GetSegment : TISC_BlobGetSegment;
> BlobHandle : PInt;
> SegmentCount : LongInt;
> MaxSegmentLength : LongInt;
> TotalSize : LongInt;
> PutSegment : TISC_BlobPutSegment;
> end;
> PBlob = ^TBlob;
>
> function BlobToStream(aBlob: PBlob): TStream;
> var
> il_TempPos: LongInt;
> il_BytesToRead: LongInt;
> il_BytesRead: LongInt;
> il_temp: integer;
> sl_Buffer: string;
> il_result: integer;
> Stream: TMemoryStream;
> il_count: integer;
> begin
> il_count := 0;
> Stream := TMemoryStream.Create;
> try
> with aBlob^ do
> begin
> il_temp := 0;
> SetString(sl_Buffer, nil, TotalSize);
> il_TempPos := 0;
> il_BytesToRead := TotalSize;
> il_BytesRead := 0;
> while il_BytesToRead > 0 do
> begin
> // il_BytesRead := 0;
> il_result := GetSegment(BlobHandle, @sl_Buffer[il_TempPos + 1], il_BytesToRead,
il_BytesRead);
> // showmessage('Pos: ' + inttostr(il_TempPos) + #13 +
> // 'offen: ' + inttostr(il_BytesToRead) + #13 +
> // 'gelesen: ' + inttostr(il_BytesRead) + #13 +
> // 'result: ' + inttostr(il_result) + #13 +
> // 'segmentcount: ' + inttostr(segmentcount));
> // inc(il_count);
> // if il_count = 20 then exit;
> inc(il_TempPos, il_BytesRead);
> dec(il_BytesToRead, il_BytesRead);
> if il_result = 0 then
> begin
> // showmessage('Pos: ' + inttostr(il_TempPos) + #13 +
> // 'offen: ' + inttostr(il_BytesToRead) + #13 +
> // 'gelesen: ' + inttostr(il_BytesRead) + #13 +
> // 'result: ' + inttostr(il_result) + #13 + 'exit');
> exit;
> end;
> end;
> end;
> Stream.WriteBuffer(PChar(sl_Buffer)[1], length(sl_Buffer));
> Stream.Position := 0;
> sl_Buffer := '';
> result := Stream;
> except
> Stream.Free;
> raise;
> end;
> end;