Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SSCANF otimizada
- * Por Bruno (27/09/2013)
- * sscanfex( "string para tratar", "formatacao", variáveis )
- *
- * Formatação é:
- * u -> jogadores
- * n, u, i -> valores int
- * s, z -> strings
- * f -> floats
- * H -> decodificar string em HEX
- * pCARACTERE -> colocar novo delimitador: exemplo: p|
- * Ainda não acabei, faltam mais!
- *
- */
- stock sscanfex( string[], const formatex[] , {Float,_}: ... )
- {
- static parametro, arg, args, stringPos;
- new tempBuff[0xff];
- args = numargs();
- #emit CONST.alt 8
- #emit LCTRL 5
- #emit ADD
- #emit ADD.C 12
- #emit STOR.pri parametro
- #if defined DEBUG
- {
- #define D(%1->%0) \
- printf( "SSCANFEX PARAM("#%1")" , %0 )
- }
- #endif
- arg = strlen(formatex) ;
- new delim[] = " ";
- if( strfind( string, " ", false ) != -1 || arg != args - 2)
- return 1;
- stringPos = strfind( string, delim , false ) ;
- for( arg = 0 ; arg != args; ++ arg ) {
- switch( formatex[arg] ) {
- case EOS:
- return false;
- case 'p' : {
- delim[0] = formatex[arg ++] ;
- }
- case 'z', 's' : {
- strcat( ( tempBuff[0] = '\0' , tempBuff) , string , 0xff );
- if( stringPos != -1 ) {
- tempBuff [ stringPos ] = '\0';
- format( string , 0xff , "%s" , string [ stringPos + 1] );
- }
- #if defined DEBUG
- {
- D("%s"->tempBuff) ;
- }
- #endif
- #emit LOAD.PRI parametro
- #emit LOAD.I
- #emit PUSH.C 0xff
- #emit PUSH.ADR tempBuff
- #emit PUSH.PRI
- #emit PUSH.C 12
- #emit SYSREQ.C strcat
- #emit STACK 16
- parametro += 4;
- stringPos = strfind( string, delim , false ) ;
- }
- case 'u' : {
- new bool: isFind [ 1 char ];
- strcat( ( tempBuff[0] = '\0' , tempBuff) , string , 0xff );
- if( stringPos != -1 ) {
- tempBuff [ stringPos ] = '\0';
- format( string , 0xff , "%s" , string [ stringPos + 1] );
- }
- #if !defined foreach
- for(new i = GetMaxPlayers() - 1 ; i > -1; i--) if(IsPlayerConnected(i))
- #else
- #if _FOREACH_LOCAL_VERSION == 17
- foreach (new i : Player)
- #else
- foreach(Player,i)
- #endif
- #endif
- {
- static szName[MAX_PLAYER_NAME];
- GetPlayerName( i, szName, sizeof szName );
- if( -1 != strfind ( szName, tempBuff, true ) ) {
- setarg( arg + 2, 0 , i );
- isFind{0} = true;
- }
- }
- if(!isFind{0}) {
- setarg( arg + 2, 0 , -1 );
- }
- stringPos = strfind( string, delim , false ) ;
- parametro += 4;
- }
- case 'H': {
- strcat( ( tempBuff[0] = '\0' , tempBuff) , string , 0xff );
- if( stringPos != -1 ) {
- tempBuff [ stringPos ] = '\0';
- format( string , 0xff , "%s" , string [ stringPos + 1] );
- }
- new
- stringTexto[0xff],
- stringTemp[4],
- alocarNumerosInts;
- for (new i; tempBuff[i]; i++)
- {
- format(stringTemp, 4, "%s%s", tempBuff[i++], tempBuff[i]);
- switch (stringTemp[1])
- {
- case 0x58 , 0x78: alocarNumerosInts = 0;
- case 0x30 .. 0x39: alocarNumerosInts = (stringTemp[0] << 4) | (stringTemp[1] - 0x30);
- case 0x61 .. 0x66: alocarNumerosInts = (stringTemp[0] << 4) | (stringTemp[1] - (0x61 - 0xA));
- case 0x40 .. 0x46: alocarNumerosInts = (stringTemp[0] << 4) | (stringTemp[1] - (0x41 - 0xA));
- }
- format(stringTexto, 500, "%s%c", stringTexto, alocarNumerosInts);
- }
- #emit LOAD.PRI parametro
- #emit LOAD.I
- #emit PUSH.C 0xff
- #emit PUSH.ADR stringTexto
- #emit PUSH.PRI
- #emit PUSH.C 12
- #emit SYSREQ.C strcat
- #emit STACK 16
- stringPos = strfind( string, delim , false ) ;
- #if defined DEBUG
- {
- D("%s"->stringTexto) ;
- }
- #endif
- parametro += 4;
- }
- case 'x', 'h': {
- strcat( ( tempBuff[0] = '\0' , tempBuff) , string , 0xff );
- if( stringPos != -1 ) {
- tempBuff [ stringPos ] = '\0';
- format( string , 0xff , "%s" , string [ stringPos + 1] );
- }
- new x, set, ch;
- if(tolower(tempBuff[1]) == 'x') x = 2;
- for( ; tempBuff[x]; x++) {
- ch = tempBuff[x];
- switch (ch)
- {
- case '0' .. '9':
- set = (set << 4) | (ch - 48);
- case 'a' .. 'f':
- set = (set << 4) | (ch - 87);
- case 'A' .. 'F':
- set = (set << 4) | (ch - 55);
- }
- }
- stringPos = strfind( string, delim , false ) ;
- setarg( arg + 2, 0 , set );
- #if defined DEBUG
- {
- D("%x"->getarg( arg + 2, 0)) ;
- }
- #endif
- parametro += 4;
- }
- case 'i' , 'd' , 'n' : {
- strcat( ( tempBuff[0] = '\0' , tempBuff) , string , 0xff );
- if( stringPos != -1 ) {
- tempBuff [ stringPos ] = '\0';
- format( string , 0xff , "%s" , string [ stringPos + 1] );
- }
- setarg( arg + 2, 0 , strval ( tempBuff ) );
- stringPos = strfind( string, delim , false ) ;
- #if defined DEBUG
- {
- D("%d"->getarg( arg + 2, 0)) ;
- }
- #endif
- parametro += 4;
- }
- case 'f' : {
- strcat( ( tempBuff[0] = '\0' , tempBuff) , string , 0xff );
- if( stringPos != -1 ) {
- tempBuff [ stringPos ] = '\0';
- format( string , 0xff , "%s" , string [ stringPos + 1] );
- }
- setarg( arg + 2, 0 , _: floatstr ( tempBuff ) );
- parametro += 4;
- #if defined DEBUG
- {
- D("%f"->Float:getarg( arg + 2, 0)) ;
- }
- #endif
- stringPos = strfind( string, delim , false ) ;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement