cs_set_convert

Description

Installs or retrieves a user-defined conversion routine.

Syntax

CS_RETCODE cs_set_convert(context, action, srctype,
               desttype, func)
 
 CS_CONTEXT      *context;
 CS_INT                 action;
 CS_INT                 srctype;
 CS_INT                 desttype;
 CS_CONV_FUNC  *func;

Parameters

context

A pointer to a CS_CONTEXT structure. A CS_CONTEXT structure defines a Client-Library application context.

action

One of the following symbolic values:

Value of action

cs_set_convert

CS_SET

Installs a conversion routine.

CS_GET

Retrieves the current conversion routine of this type.

CS_CLEAR

Clears the current conversion routine by replacing it with CS-Library’s default conversion routine of this type.

srctype

The datatype of the source data for the conversion.

desttype

The datatype of the destination data.

func

A pointer to a CS_CONV_FUNC variable, which is a pointer to a custom conversion function. “Defining a custom conversion routine” describes the prototype for a custom conversion function.

If a conversion routine is being installed, *func points to the conversion routine that you wish to install.

If a conversion routine is being retrieved, cs_set_convert sets *func to point to the currently installed conversion routine.

If a conversion routine is being cleared, pass *func as NULL.

Notefunc represents a pointer to a pointer to a function. There are special requirements for passing this parameter. See the example code fragment under “Installing a custom conversion routine”.

Returns

cs_set_convert returns:

Returns

Indicates

CS_SUCCEED

The routine completed successfully.

CS_FAIL

The routine failed.

The most common reason for a cs_set_convert failure is an invalid parameter.

Usage


Defining a custom conversion routine

Table 2-14: Return values for a custom conversion routine

Return value

Indicates

CS_SUCCEED

Successful conversion.

CS_TRUNCATED

The conversion resulted in truncation.

CS_MEM_ERROR

A memory allocation failure has occurred.

CS_EBADXLT

Some characters could not be translated.

CS_ENOXLT

The requested translation is not supported.

CS_EDOMAIN

The source value is outside the domain of legal values for the datatype.

CS_EDIVZERO

Division by 0 is not allowed.

CS_EOVERFLOW

The conversion resulted in overflow.

CS_EUNDERFLOW

The conversion resulted in underflow.

CS_EPRECISION

The conversion resulted in loss of precision.

CS_ESCALE

An illegal scale value was encountered.

CS_ESYNTAX

The conversion resulted in a value which is not syntactically correct for the destination type.

CS_ESTYLE

The conversion operation was stopped due to a style error.


Installing a custom conversion routine

The following code demonstrates calling cs_set_convert to install a custom conversion routine, MyConvert, which converts from CS_CHAR to the user defined type indicated by MY_USER_TYPE. The code assumes that MyConvert is a a custom conversion routine that has been defined correctly. (See “Defining a custom conversion routine”.) The program variable myfunc is used to pass the address of the conversion routine.

#define MY_USER_TYPE (CS_USER_TYPE + 2)
CS_CONV_FUNC p_conv_func;
p_conv_func = MyConvert;
 if (cs_set_convert(context, CS_SET, CS_CHAR_TYPE, MY_USER_TYPE,
     &p_conv_func) != CS_SUCCEED)
 {
     fprintf(stdout, "cs_set_convert(MY_USER_TYPE) failed!\n");
     (CS_VOID)ct_exit(context, CS_FORCE_EXIT);
     (CS_VOID)cs_ctx_drop(context);
     exit(1);
 }

See also

cs_convert, cs_manage_convert, cs_setnull, ct_bind