_Alloca( ) API Library Routine Example

The following example duplicates the action of the Visual FoxPro REPLICATE( ) function. The memory temporarily used to duplicate the character comes from _Alloca( ).

Visual FoxPro Code

SET LIBRARY TO ALLOCA  
x = xREPLICATE("x", 120)
? x
? LEN(x)

C Code

#include <pro_ext.h>

void FAR allocaEx(ParamBlk FAR *parm)
{
   char FAR *rep;
   char c = *(char *) _HandToPtr(parm->p[0].val.ev_handle);

   rep = _Alloca((int) parm->p[1].val.ev_long + 1);
   _MemFill(rep, c, (int) parm->p[1].val.ev_long);
  rep[parm->p[1].val.ev_long] = '\0';  // null terminate

   _RetChar(rep);
}

FoxInfo myFoxInfo[] =
{
   {"XREPLICATE", (FPFI) allocaEx, 2, "C,I"},
};

FoxTable _FoxTable =
{
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};