Upper and lower functions in C for Informix 9.x
Posted in 1998
Topics: Stored Procedures & SPL, Data Types & Schema Design, Platform-Specific Issues
I few weeks ago, I searched this newsgroup for discussions on
string functions (such as upper() and lower()) for the Universal
Server (now marketed as the "Informix Dynamic Server with
Universal Data Option"). There were various pointers to SPL
string handling functions which were torturous in design because
of SPL limitations.
I wanted to start conversations around a standard set of C string
handling routines and to start this of, I provide some simple
routines. Any suggested improvements would be greatly
appreciated, particularly by experienced DataBlade programmers.
My environment is Solaris 2.5.1 and IDS w/ UDO v
9.14.UC1. Specifics regarding compiling and the creation of
shared libraries is likely to be highly platform specific
(completely different for NT servers, for example). The source
should be fairly portable, though.
Rob
--
Robert M. Ryan, Principal
Business Architects, LLC
phone: 617.859.3737 fax: 617.859.3730
--
To make C functions accessible from the UNIX Informix database
server (as described in "Extending INFORMIX Universal Server:
User Defined Routines") you must build shared libraries. The
following commands build the shared library and make them
accessible to the database (I've also included the gcc syntax
that I tested):
1. Compile the code with a standard compiler:
cc -I $INFORMIXDIR/incl -c stringutils.c
With the GNU compiler, the syntax is:
gcc -I $INFORMIXDIR/incl -fpic -c stringutils.c
2. Create a shared object library (.so file extension on Solaris):
cc -K stringutils.o -o stringutils.so
With the GNU compiler, the syntax is:
gcc -shared stringutils.o -o stringutils.so
3. Put the shared library in a directory readable by the user informix
and set permissions to 755 or 775.
4. Make the functions accessible from Informix:
CREATE FUNCTION upper (str LVARCHAR)
RETURNING LVARCHAR; EXTERNAL NAME
"/usr/code/stringutils.so(upper)"
LANGUAGE C
END FUNCTION;
CREATE FUNCTION lower (str LVARCHAR)
RETURNING LVARCHAR; EXTERNAL NAME
"/usr/code/stringutils.so(lower)"
LANGUAGE C
END FUNCTION;
The C source code for my upper() and lower() functions is as follows:
/**********************************************************************
* $Workfile: stringutils.c $
*
* Description:
*
* This is a series of string handling routines that are to be
* called as external functions from Informix.
*
* This defines routines "upper" and "lower".
*
* Author:
*
* Robert M. Ryan, Business Architects, LLC, 1998
*
* $History: stringutils.c $
*
* ***************** Version 2 *****************
* User: Rryan Date: 6/16/98 Time: 1:44p
* Updated
* Tweaked comments
*
* ***************** Version 1 *****************
* User: Rryan Date: 6/16/98 Time: 1:40p
* Created
* Basic string handling routines including upper() and lower().
**********************************************************************/
/**********************************************************************
* INCLUDES
**********************************************************************/
#include <string.h>
#include <ctype.h>
#include "mi.h"
/**********************************************************************
* PROTOTYPES
**********************************************************************/
static void strtoupper(char *szTmp);
static void strtolower(char *szTmp);
/**********************************************************************
* CODE
**********************************************************************/
/* upper
*
* This is the upper() function that will be called from the Informix database.
*/
mi_lvarchar *upper(mi_lvarchar *lvMixed)
{
mi_string *mstrUpper;
mi_lvarchar *lvUpper;
/* get the lvarchar in zero terminated string format */
mstrUpper = mi_lvarchar_to_string(lvMixed);
/* if it failed, return NULL */
if (mstrUpper == NULL)
return NULL;
/* convert the string to uppercase */
strtoupper(mstrUpper);
/* convert the results back to lvarchar */
lvUpper = mi_string_to_lvarchar(mstrUpper);
/* free the char * that we were using */
mi_free(mstrUpper);
/* if there was a problem converting back to lvarchar, return NULL */
if (lvUpper == NULL)
return NULL;
/* return results */
return lvUpper;
} /* upper */
/* lower
*
* This is the lower() function that will be called from the Informix database.
*/
mi_lvarchar *lower(mi_lvarchar *lvMixed)
{
mi_string *mstrLower;
mi_lvarchar *lvLower;
/* get the lvarchar in zero terminated string format */
mstrLower = mi_lvarchar_to_string(lvMixed);
/* if it failed, return NULL */
if (mstrLower == NULL)
return NULL;
/* convert the string to lowercase */
strtolower(mstrLower);
/* convert the results back to lvarchar */
lvLower = mi_string_to_lvarchar(mstrLower);
/* free the char * that we were using */
mi_free(mstrLower);
/* if there was a problem converting back to lvarchar, return NULL */
if (lvLower == NULL)
return NULL;
/* return results */
return lvLower;
} /* lower */
/* strtolower
*
* Convert string to lowercase.
*/
static void strtolower(char *szTmp)
{
int i;
/* loop through string, converting to lowercase */
for (i = 0; i < strlen(szTmp); i++)
{
szTmp[i] = tolower(szTmp[i]);
}
return;
} /* strtolower */
/* strtoupper
*
* Convert string to uppercase.
*/
static void strtoupper(char *szTmp)
{
int i;
/* loop through string, converting to uppercase */
for (i = 0; i < strlen(szTmp); i++)
{
szTmp[i] = toupper(szTmp[i]);
}
return;
} /* strtoupper */