I wrote this thing below for myself once to create SQL query strings
pawn Код:
#include <a_samp>
#include <pointers> // <- Required! forum.sa-mp.com/showthread.php?t=311757
stock multistrcatex(dest[], dest_len = sizeof dest, const frmt[], {Float,_}:...)
{
new num_args = numargs(), format_len = strlen(frmt);
if(format_len < num_args-3)
{
print("multistrcatex() error: More arguments than the format actually assings.");
return;
}
else if(format_len > num_args-3) printf("multistrcatex() warning: Too many formats.");
new buffer[32];
for(new i = 3; i < num_args; i++)
{
switch(frmt[i-3])
{
case 'c': format(buffer, sizeof(buffer), "%c", @arg[i]);
case 'd', 'i': valstr(buffer, @arg[i]);
case 'x': format(buffer, sizeof(buffer), "%x", @arg[i]);
case 'f': format(buffer, sizeof(buffer), "%.8f", @arg[i]);
case 's':
{
strcat(dest, @arg[i], dest_len);
continue;
}
default:
{
printf("multistrcatex() error: Unknown format %c", frmt[i-4]);
return;
}
}
strcat(dest, buffer, dest_len);
}
}
Example usage:
pawn Код:
new my_str[256], my_array[3] = {10, 20, 30};
multistrcatex(my_str, _, "sdsdsdscccccsfc", "First came ", my_array[0], ", then ", my_array[1], ", after that ", my_array[3], ", and what ", 'a', 'b', 'o', 'u', 't', " these, and ", 1234.5678, '?');
Prints:
Код:
First came 10, then 20, after that 30, and what about these, and 1234.56780000?
Imagine to do this with large strings