1. void print_string( char_data *ch, std::string argument )
2. {
3. printf( "You entered %s as an argument.\r\n", argument.c_str() );
4. }
#include <iostream>
#include <printf.h>
#include <string>
int print_string (FILE *stream, const struct printf_info *info, const void *const *args)
{ const std::string *s;
char *buffer;
int len;
/* Format the output into a string. */
s = *((const std::string **) (args[0]));
len = asprintf (&buffer, "%s", s->c_str());
if (len == -1)
return -1;
/* Pad to the minimum field width and print to the stream. */
len = fprintf (stream, "%*s", (info->left ? -info->width : info->width), buffer);
/* Clean up and return. */
free (buffer);
return len;
}
int print_string_arginfo (const struct printf_info *info, size_t n, int *argtypes)
{ /* We always take exactly one argument and this is a pointer to the structure.. */
if (n > 0)
argtypes[0] = PA_POINTER;
return 1;
}
int main (void)
{ std::string *str = new std::string("Lalala");
/* Register the print function for widgets. */
register_printf_function ('W', print_string, print_string_arginfo);
/* Now print the widget. */
printf ("|%W|\n", str);
printf ("|%35W|\n", str);
printf ("|%-35W|\n", str);
return 0;
}
If you're not sure what I'm asking, here's an example:
If there's a C++ equivalent of this, or someone knows how to make one, I'd be very happy to see it.