A Blob UDF Example

From InterBase

Go Up to Writing a Blob UDF


The following code creates a UDF, blob_concatenate(), that appends data from one Blob to the end of another Blob to return a third Blob consisting of concatenated Blob data. Notice that it is okay to use malloc() rather than ib_util_malloc() when you free the memory in the same function where you allocate it.

/* Blob control structure */
typedef struct blob {
void (*blob_get_segment) ();
int *blob_handle;
long number_segments;
long max_seglen;
long total_size;
void (*blob_put_segment) ();
} *Blob;

extern char *isc_$alloc();
#define MAX(a, b) (a > b) ? a : b
#define DELIMITER "-----------------------------"

void blob_concatenate(Blob from1, Blob from2, Blob to)
/* Note Blob to, as final input parameter, is actually for output! */
{
char *buffer;
unsigned short length, b_length;

b_length = MAX(from1->max_seglen, from2->max_seglen);
buffer = malloc(b_length);

/* write the from1 Blob into the return Blob, to */
while ((*from1->blob_get_segment) (from1->blob_handle, buffer,
b_length, &length))
(*to->blob_put_segment) (to->blob_handle, buffer, length);

/* now write a delimiter as a dividing line in the blob */
(*to->blob_put_segment) (to->blob_handle, DELIMITER,
sizeof(DELIMITER) - 1);

/* finally write the from2 Blob into the return Blob, to */
while ((*from2->blob_get_segment) (from2->blob_handle, buffer,
b_length, &length))
(*to->blob_put_segment) (to->blob_handle, buffer, length);

/* free the memory allocated to the buffer */
free(buffer);
}

Advance To: