SphereCommunity
[SOURCE] STRTOKEN - Printable Version

+- SphereCommunity (https://forum.spherecommunity.net)
+-- Forum: Sphere 0.56d (/Forum-Sphere-0-56d)
+--- Forum: Script Submissions (/Forum-Script-Submissions)
+--- Thread: [SOURCE] STRTOKEN (/Thread-SOURCE-STRTOKEN)



[SOURCE] STRTOKEN - xwerswoodx - 02-16-2017 03:15 AM

Addes STRTOKEN function to sphere;

Usage: <STRTOKEN text,n,seperator>

<STRTOKEN a.b.c.d.e,3,.> returns c
<STRTOKEN a.b.c.d.e,0,.> returns 5
<STRTOKEN a.b.c.d.e,6,.> returns null

You can also specify a range of tokens:
<STRTOKEN a.b.c.d.e,2-,.> returns 2nd token onwards = b.c.d.e
<STRTOKEN a.b.c.d.e,2-4,.> returns tokens 2 through 4 = b.c.d

Installation;
Note: Please backup your files before starting to edit.

Open File: src/common/CScriptObj.cpp

find:
Code:
        case SSC_EXPLODE:

add before:
Code:
        case SSC_StrToken:
            {
                TCHAR * ppArgs[3];
                size_t iQty = Str_ParseCmds(const_cast<TCHAR *>(pszKey), ppArgs, COUNTOF(ppArgs), ",");
                if ( iQty < 3 )
                    return false;

                if ( *ppArgs[2] == '"' )
                   ppArgs[2]++;
                
                for (TCHAR * pEnd = ppArgs[2] + strlen(ppArgs[2]) - 1; pEnd >= ppArgs[2]; pEnd--)
                {
                    if ( *pEnd == '"' )
                    {
                        *pEnd = '\0';
                        break;
                    }
                }
                TCHAR * iSep = ppArgs[2];
                for (TCHAR *iSeperator = ppArgs[2] + strlen(ppArgs[2]) - 1; iSeperator > ppArgs[2]; iSeperator--)
                    *iSeperator = '\0';

                if ( *ppArgs[0] == '"' )
                    ppArgs[0]++;
                for (TCHAR * pEnd = ppArgs[0] + strlen(ppArgs[0]) - 1; pEnd >= ppArgs[0]; pEnd--)
                {
                    if ( *pEnd == '"' )
                    {
                        *pEnd = '\0';
                        break;
                    }
                }

                sVal = "";
                TCHAR *ppCmd[255];
                size_t count = Str_ParseCmds(ppArgs[0], ppCmd, COUNTOF(ppCmd), iSep);

                TCHAR * ppArrays[2];
                size_t iArrays = Str_ParseCmds(ppArgs[1], ppArrays, COUNTOF(ppArrays), "-");
                INT64    iValue = Exp_GetVal(ppArgs[1]);
                INT64     iValueEnd = iValue;
                
                if (iArrays > 1)
                {
                    iValue = Exp_GetVal(ppArrays[0]);
                    iValueEnd = Exp_GetVal(ppArrays[1]);
                    if (iValueEnd <= 0 || iValueEnd > count)
                        iValueEnd = count;                    
                }
                
                if (iValue < 0)
                    return false;
                else if (iValue > 0)
                {
                    if (iValue > count)
                        return false;
                    else if (iValue == iValueEnd)
                        sVal = ppCmd[iValue - 1];
                    else
                    {
                        sVal.Add(ppCmd[iValue - 1]);
                        INT64 i = iValue + 1;
                        for ( ; i <= iValueEnd; i++)
                        {
                            sVal.Add(iSep);
                            sVal.Add(ppCmd[i - 1]);
                        }
                    }
                }
                else
                    sVal.FormatVal(static_cast<long>(count));
            }
            return true;

Open File: src/tables/CScriptObj_functions.tbl

find:
Code:
ADD(StrSub,            "StrSub")

add after:
Code:
ADD(StrToken,        "StrToken")



RE: [SOURCE] STRTOKEN - rastrero - 03-04-2017 05:08 AM

Oh its so nice... But I cant imagine why do u need this. Can you give me an example?


RE: [SOURCE] STRTOKEN - xwerswoodx - 06-01-2017 03:07 AM

Sorry for late answer I saw your message now, basically, it lets you to seperate sentences with commas.

For example:
If you have string "1,000 2,000 3,000" and you want to get 2,000 you can use <STRTOKEN "1,000 2,000 3,000",2," ">.

For example if you have in-game forum like me, you can censor swearing words with strtoken but explode overrides commas so it's not possible if sentence has commas or if there are 6-7 commas you can use for/while to check everystring inside commas to do it.

Code:
[FUNCTION DIFF]
SERV.LOG ARGS: <ARGV[2]>
SERV.LOG STRTOKEN: <STRTOKEN "<ARGS>",3," ">
ARGS=<EXPLODE " ",<ARGS>>
SERV.LOG EXPLODE: <ARGV[2]>

23:40:'xwerswoodx' commands 'diff 1,000 2,000 5,000 100,000 9,000'=1
23:40:(insurance.scp,2)ARGS: 000 5
23:40:(insurance.scp,3)STRTOKEN: 5,000
23:40:(insurance.scp,5)EXPLODE: 2