Problem :
---------
convert CHAR type to TEXT type (BLOB) and insert in table.
Solution :
----------
Here is the "C" functions.
One to write the string into the blob and other to read the blob.
(included two "4GL" test programs and two "C" functions.)
Many thanks to :
Rem : This code run with BLOB located in MEMORY.
----------------------------------------- Cut Here -------------------
=> C Code
#include <assert.h>
#include <stdio.h>
#include "/usr/informix/incl/locator.h"
#define BLOCK_SIZE 4096
/*
--------------------------------------------------------------------
append_string : append string to the blobspace
--------------------------------------------------------------------
*/
int blob_append_string(n)
int n;
{
loc_t *blob;
int offset;
int len, c, needed, cnter=0;
char string[BLOCK_SIZE];
if (n != 2)
fgl_fatal(__FILE__, __LINE__, -1318);
/* popquote(string, sizeof(string)); */
popvchar(string, sizeof(string));
poplocator(&blob);
assert(blob->loc_loctype == LOCMEMORY);
if((len=strlen(string)) == 0)
{
/* nothing to append */
retint(-2);
return(1);
}
/* append text to the blob */
if(blob->loc_indicator == -1)
needed = len;
else
needed = blob->loc_size + len;
if(blob->loc_bufsize < needed)
{
if(blob->loc_buffer == 0)
blob->loc_buffer = (char *)malloc(needed);
else
blob->loc_buffer = (char *)realloc(blob->loc_buffer, needed);
assert(blob->loc_buffer != (char *)0);
if(blob->loc_buffer == 0) /* error allocating space */
{
retint(-1);
return(1);
}
}
blob->loc_bufsize = needed;
if(blob->loc_indicator == -1)
{
memcpy(blob->loc_buffer, string, len);
blob->loc_size = len;
blob->loc_indicator = 0;
}
else
{
memcpy(blob->loc_buffer + blob->loc_size, string, len);
blob->loc_size = len;
}
blob->loc_currdata_p = blob->loc_buffer + blob->loc_size;
retint(0);
return(1);
/*Quote:}
--------------------------------------------------------------------
return_string : reads the string from the blobspace
--------------------------------------------------------------------
*/
int blob_return_string(n)
int n;
{
loc_t *blob;
int offset;
int c, cnter=0;
char new_str[BLOCK_SIZE];
if (n != 2)
fgl_fatal(__FILE__, __LINE__, -1318);
popint(&offset);
poplocator(&blob);
/* get the string from the begining of the blob
plus the offset up to the loc_size */
if (offset >= blob->loc_size || offset < 0)
{
/* error or at end of blob */
offset = -1;
strcpy(new_str, "END OF BLOB");
}
else
{
while(offset < blob->loc_size)
{
c = *(blob->loc_buffer + offset++);
new_str[cnter++] = c;
if (cnter == BLOCK_SIZE)
{
/* don't go out of range of array buffer */
cnter=BLOCK_SIZE - 1;
break;
}
/* stop if the string terminates with either a string
terminator or a new-line*/
if (c == '\0' || c == '\n')
break;
}
new_str[cnter] = '\0';
}
retint(offset);
retquote(new_str);
return(2);
----------------------------------------- Cut Here -------------------Quote:}
=> 4GL Programm
# --------------------------------------------------------------------
# 4GL program to write a blob
# --------------------------------------------------------------------
DATABASE indus
MAIN
call test_function()
END MAIN
FUNCTION test_function()
DEFINE xchamp TEXT,
xdonnees TEXT,
offset SMALLINT,
ret_val SMALLINT,
str1 CHAR(40)
LOCATE xchamp IN MEMORY
LOCATE xdonnees IN MEMORY
LET offset = 0
INITIALIZE xchamp TO NULL
INITIALIZE xdonnees TO NULL
LET str1 = "champ1;champ2"
CALL blob_append_string(xchamp, str1) RETURNING ret_val
LET str1 = "Christian;Hernoux"
CALL blob_append_string(xdonnees, str1) RETURNING ret_val
INSERT INTO edition VALUES (0, "ESSAI", xchamp, xdonnees);
END FUNCTION
----------------------------------------- Cut Here -------------------
# -----------------------------------------------------------------
# 4GL program to read blob
# -----------------------------------------------------------------
DATABASE indus
MAIN
CALL test_function()
END MAIN
FUNCTION test_function()
DEFINE xchamps TEXT,
xdonnees TEXT,
octets SMALLINT,
str_champs CHAR(4096),
str_donnees CHAR(4096)
LOCATE xchamps IN MEMORY
LOCATE xdonnees IN MEMORY
LET octets = 0
INITIALIZE xchamps TO NULL
INITIALIZE xdonnees TO NULL
SELECT champs,donnees INTO xchamps, xdonnees
FROM edition
WHERE no_courrier = 1
CALL blob_return_string(xchamps, octets) RETURNING octets, str_champs
DISPLAY "Champs : ",str_champs CLIPPED
CALL blob_return_string(xdonnees, octets) RETURNING octets, str_donnees
DISPLAY "Donnees : ",str_donnees CLIPPED
END FUNCTION
----------------------------------------- Cut Here -------------------
=> SQL Table creation
CREATE TABLE edition {
no_courrier SERIAL,
champs TEXT IN TABLE,
donnees TEXT IN TABLE
----------------------------------------- Cut Here -------------------Quote:}
Amicalement Net.