Adding a User Function

This section provides an overview of the ufunc.c file and discusses how to:

  • Add a function prototype.

  • Add an entry to the USERFUNCS table.

  • Add implementation code.

  • Relink SQR.

The code examples in the following sections demonstrate how to extend SQR with an initcap function.

The key to this process is an SQR source file called ufunc.c. This file contains a list of user-defined functions. It also contains comments with a description of the process of adding a function to SQR. Ufunc.c is in the lib subdirectory (LIBW in Microsoft Windows).

To add initcap to SQR, you must add it to a global array called userfuncs in ufunc.c.

Begin by adding a function prototype to the function declaration list:

static void max CC_ARGS((int, double * >= maxlen) break; /* don't exceed maxlen */
    if (isalnum(*ptr)) {
       if (flag) *p = islower(*ptr)?toupper(*ptr):*ptr;
       else *p = isupper(*ptr)?tolower(*ptr):*ptr;
       flag = 0;
    } else {
       flag = 1;
       *p = *ptr;
    }
     p++; ptr++;
  }
  *p = '\0';
  return;
}

Note the use of the CC_ARGL, CC_ARG, and CC_LARG macros. You can also write the code as follows (only the first five lines are shown):

static void initcap(argc,argv,result,maxlen)
int argc;       /* Number of actual arguments */
char* argv[];   /* Pointers to arguments: */
char* result;   /* Where to store result */
int maxlen;     /* Result's maximum length */

After you modify ask, you must relink SQR. Use the make file that is provided in the LIB (or LIBW) subdirectory of SQR. This step is specific to the operating system and database. SQR is linked with the database libraries, whose names and locations may vary. You may have to modify the make file for your system.

After SQR is relinked, you are ready to test. Try the following program:

begin-program
  let $a = initcap('MR. JOSEPH JEFFERSON')
  print $a ()
end-program

The result in the output file should be:

Mr. Joseph Jefferson

See the ufunc.c file for further information about argument types in user-defined functions.

See the product documentation for PeopleSoft 9.2 Application Installation.