// C++ code (mostly due to the use of "new" instead of "malloc"):
extern "C" __declspec(dllexport) char *repeatString(int argc, char *argv[]) // DM-Args: string, repeat
{
int count = atoi(argv[1]);
long size = strlen(argv[0])*count + 1; // len*repeat + '\0'
char *message = new char[size];
if(message)
{
message[0] = '\0';
while(count > 0)
{
strcat_s(message, size, argv[0]);
--count;
}
return message;
} else return "-1";
}
// DM code:
mob/verb/Repeat_String()
var/s = input(src, "What string?") as null|text
if(s)
var/i = input(src, "How many repeats?") as num
src << call("some.dll", "repeatString")(s, num2text(i))
Now, of course I could create a single-sized (n) buffer on the stack, compare the sizes of the strings and either cap off the string at n-1 characters, or report an error if it exceeds that size, but that makes the function far less flexible in the long run.
I'm just curious because, although this repeatString example isn't indicative of what I'm doing, it demonstrates the point with similar effectiveness to the program at hand I'm working on.