string_pad_get.cpp

The string_pad_get.cpp sample program takes an input string position value and returns the character that is stored in stringpad at that position of a character array. The stringpad must be created and populated by the string_pad_create function.

#include "udxinc.h"
#include <string.h>
using namespace nz::udx_ver2;

struct Root
{
    char* data;
    int size;
};
class StringPadGet: public Udf  // Exposed udf class in a udxinc.h
{
public:
StringPadGet(UdxInit *pInit) : Udf(pInit)
{
}
static Udf* instantiate(UdxInit *pInit);
virtual ReturnValue evaluate()
{
    /*
     ** Gets the CPad with name stringpad.
     */
    CPad* pad = getPad("stringpad");

    /*
     ** getRootObject verifies whether stringpad already exists.     
     ** If the pad does not exist, the program creates it and returns
     ** true; otherwise, the function does not create another pad and
     ** returns false. As a best practice, your UDX should check for
     ** the presence of the CPad before it creates the CPad.
     */
    Root *ro = (Root*) pad->getRootObject(sizeof(Root));
    if(!ro)
    {
        throwUdxException("Pad does not exist");
    }
    else
    {
        int index = int32Arg(0);
        if(index<0||index>=ro->size)
        {
            throwUdxException("Index out of bounds");
        }
        StringReturn *ret = stringReturnInfo();
        ret->size=1;
        /*
         ** Returns the specified index character.
         */
        ret->data[0]=ro->data[index];
        NZ_UDX_RETURN_STRING(ret);
    }
}
};
Udf* StringPadGet::instantiate(UdxInit *pInit)
{
    return new StringPadGet;
}