View Single Post
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 04-05-2020 , 09:58   Re: String parsing problem
Reply With Quote #6

PHP Code:
#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =  {
    
name "Test Parse"
    
author "Ludak"
    
description "Test Parse"
    
version "1.0.0"
    
url ""
};

public 
void OnPluginStart() {
    
ArrayList list = new ArrayList(256);
    
char text[256];
    
    
ParseTest("(some,(weird,string),list,1234,,(more,text,in,a,substring),some,more,$$$,###,@annotation)", list, 256);
    
    for (
int i 0< list.Lengthi++) {
        list.
GetString(itextsizeof(text));
        
PrintToServer("[%d] >>> %s"1text);
    }
}

public 
void OnPluginEnd() {
    
}

public 
int ParseTest(const char[] textArrayList resultsint partLength) {
    
int textLength strlen(text);
    
int icurrentChar;
    
bool foundFirst false;
    
bool inASubstring false;
    
    
int tempNumber 0;
    
int tempLength 0;
    
char[] tempPart = new char[partLength];
    
    for (
0textLengthi++) {
        
currentChar text[i];
        
        
// Skip first '('
        
if (!foundFirst && currentChar == '(') {
            
foundFirst true;
            continue;
        }
        
// Enter to the substring
        
else if (foundFirst && currentChar == '(')
            
inASubstring true;
        
        
// Skip last ')'
        
if (== textLength && currentChar == ')') {
            
tempPart[tempLength] = EOS;
            
results.PushString(tempPart);
            
tempLength 0;
            
tempNumber++;
            break;
        }
        
        
// Exit the substring
        
else if (currentChar == ')')
            
inASubstring false;
        
        
// Skip ',' and move on to the next part if not in the substring
        
if (!inASubstring && currentChar == ',') {
            
tempPart[tempLength] = EOS;
            
results.PushString(tempPart);
            
tempLength 0;
            
tempNumber++;
            continue;
        }
        
        
// Truncate over partLength
        
if (tempLength partLength) {
            
tempLength partLength;
            
tempPart[tempLength] = EOS;
            
results.PushString(tempPart);
            
tempLength 0;
            
tempNumber++;
            continue;
        }
        
        
tempPart[tempLength++] = currentChar;
    }
    
    return 
tempNumber;

PHP Code:
[1] >>> some
[2] >>> (weird,string)
[
3] >>> list
[
4] >>> 1234
[5] >>>
[
6] >>> (more,text,in,a,substring)
[
7] >>> some
[8] >>> more
[9] >>> $$$
[
10] >>> ###
[11] >>> @annotation 
milutinke is offline
Send a message via Skype™ to milutinke