Monday, June 4, 2012

fcmd - command handler

Hy everyone!
Im sorry for my bad English, I am Hungarian.

I needed an simple command handler, for this i write this.

An other command handler(zcmd,ycmd..) not have been using an accent character (á,é..).

Include (in code):


Code:
    stock fParamsNotUsed( params[][], maxparams ) {
            maxparams--;
           
            if( params[maxparams][0] == '\0' ) return true;
           
            return false;
    }
     
    stock fNull(params[][], maxparams) {
            for( new i = 0; i < maxparams; i++ ) {
                for( new a = 0; a < strlen(params[i]); a++ ) {
                            params[i][a] = 0;
                    }
            }
    }
     
    stock fCommand(cmdtext[], cmd[] ) {
            new ok;
            new karakterek;
     
     
            for( new i = 0; i < strlen(cmd); i++ ) {
                if( cmd[i] == cmdtext[i] ) {
                        ok++;
                    }
                    karakterek++;
            }
     
            // ----------------------------------
            new FP = fGetFirstDelim( cmdtext );
            new felhasz[128 + 1];
            format( felhasz, sizeof(felhasz), "%s", cmdtext );
     
            strdel( felhasz, FP, strlen(felhasz) );
            if( strlen(felhasz) != strlen(cmd) ) return false;
            // ----------------------------------
     
            if( ok == strlen(cmd) && karakterek == strlen(cmd) ) return true;
            return false;
    }
     
    stock fGetFirstDelim(txt[], const delim=' ') {
            new space = strlen(txt);
     
            for( new i = 0; i < strlen(txt); i++ ) {
                if( txt[i] == delim && i < space ) {
                    space = i;
                    }
            }
            return space;
    }
     
    stock fSliceParams(command[], params[][], maxparams, const delim=' ') {
            fNull( params, maxparams );
     
            new pNum = 0;
            new idxNum;
            new len;
            new idxNumPLUS;
     
            new delimeter[16 + 1];
            format( delimeter, sizeof(delimeter), "%s", delim );
     
            strdel( command, 0, fGetFirstDelim(command) + 1 );
            strins( command, delimeter, strlen(command), (128 + 1) );
     
            while( idxNum <= strlen(command) ) {
                if( command[idxNum] == delim && pNum < maxparams ) {
                            len = strmid( params[pNum], command, idxNumPLUS, idxNum , (128 + 1) );
     
     
                            if( maxparams != 1 ) {
                                    if( (maxparams-pNum) != 1 ) {
                                            pNum++;
     
                                            idxNumPLUS = idxNum + 1;
                                            params[pNum][len] = 0;
                                    }
                            }
                    }
                    idxNum++;
            }
            return 1;
    }
Usage(example):

Code:
public OnPlayerCommandText( playerid, cmdtext[] ) {
 new params[8][64 + 1];

 if( fCommand(cmdtext, "/cmd") ) {
  fSliceParams( cmdtext, params, 2 );

  if( fParamsNotUsed(params, 2) ) return SendClientMessage( playerid, -1, "Usage: /cmd (param1) [param2]" );


  printf( "%s | %s", params[0], params[1] );
 }

 return 1;
}
CREDITS:

dexi(Fl0rian) - Create the include
Kalcor - SAMP

No comments:

Post a Comment