06.08.2012, 19:31
(
Last edited by Misiur; 21/08/2012 at 12:55 PM.
)
Hello. This is example code
Result:
Wat? I thought "{Float,_}:..." in argument list means that all tags are acceptable. Dammit pawn, cooperate with me! (upd: now I know that it accepts only weak tags and Float)
pawn Code:
main() {
new src[] = "I liek pancakes|52|tomatoes"; //String parsed by sscanf
new str[5][64], int[5], Float:flo[5]; //Variable holders
new is_str[5], is_int[5]; //Variable type holders
new pattern[5] = "sds"; //Sscanf pattern
//Simple loop checking variable type from pattern, char by char (string, decimal and float only allowed)
for(new i = 0, l = strlen(pattern); i < l; i++) {
switch(pattern[i]) {
case 's': {
is_str[i] = true;
}
case 'i': {
is_int[i] = true;
}
}
}
/*
Equivalent to:
if(is_str[%0]) str[%0];
else {
if(is_int[%0]) int[%0];
else flo[%0];
}
*/
#define VAL(%0) ((is_str[%0]) ? (str[%0]) : ((is_int[%0]) ? (int[%0]) : (flo[%0])))
sscanf(src, pattern, VAL(0), VAL(1), VAL(2), VAL(3), VAL(4));
/*
After preprocessing (pawn.cc -l):
sscanf(src, pattern, ((is_str[0]) ? (str[0]) : ((is_int[0]) ? (int[0]) : (flo[0]))), ((is_str[1]) ? (str[1]) : ((is_int[1]) ? (int[1]) : (flo[1]))), ((is_str[2]) ? (str[2]) : ((is_int[2]) ? (int[2]) : (flo[2]))), ((is_str[3]) ? (str[3]) : ((is_int[3]) ? (int[3]) : (flo[3]))), ((is_str[4]) ? (str[4]) : ((is_int[4]) ? (int[4]) : (flo[4]))));
*/
}
pawn Code:
niga.pwn(23) : warning 213: tag mismatch
niga.pwn(23) : error 033: array must be indexed (variable "str")
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase