string_pad_create.cpp

The string_pad_create.cpp sample program takes an input string size and string and creates a CPad to store the string as an array of characters.

#include "udxinc.h"
#include <string.h>
using namespace nz::udx_ver2;
struct Root
{
char* data;
int size;
};
class StringPadCreate: public Udf
{
private:
public:
StringPadCreate(UdxInit *pInit) : Udf(pInit)
{
}
static Udf* instantiate(UdxInit *pInit);
virtual ReturnValue evaluate()
{
    /*
     ** Gets the CPad with name stringpad.
     */
    CPad* pad = getPad("stringpad");
    /*
     ** getRootObject which 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)
    {
        ro=PAD_NEW(pad,Root);
        int32 size = int32Arg(0);
        StringArg* a = stringArg(1);
        int32 stringSize=a->length;
        int32 TotalSize = 0; 
        int32  iObjectSize = 0;
        bool isArray = false;

        if (size < 1 || size > 64000)
        {
            throwUdxException("Given size is out of range.");
        }
        if(stringSize > size)
        {
            throwUdxException("Given string bigger than given size.");
        }
        ro->size=size;
        ro->data=PAD_NEW(pad,char)[size];
        for(int i=0; i<size; i++)
        {
            /*
             ** Assigning the data to CPad area.
             */
            if(i<stringSize)
            {
                ro->data[i]=a->data[i];
            }
            else
            {
                ro->data[i]=' ';
            }
        }
        /*
         ** The program sets the root object for the CPad so that
         ** subsequent calls or functions can reference the CPad.
         */
        pad->setRootObject(ro, sizeof(Root));

        isArray = pad->isObjectArray(ro);

        iObjectSize = pad->getObjectSize(ro);
        
        TotalSize = pad->getTotalSize();

        NZ_UDX_RETURN_INT32(TotalSize - iObjectSize);
    }
    else
    {
        NZ_UDX_RETURN_INT32(0);
    }
}
};

Udf* StringPadCreate::instantiate(UdxInit *pInit)
{
     return new StringPadCreate;
}