AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   String parsing problem (https://forums.alliedmods.net/showthread.php?t=322780)

Ludak 04-04-2020 14:51

String parsing problem
 
Hello!
I have ported this code from the AMXX:
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() {
    
AddCommandListener(Chat_Handle"say");
}

public 
Action Chat_Handle(int player, const char[] commandint argCommands) {
    if (
IsClientInGame(player)) {
        
char message[256];
        
        
GetCmdArgString(messagesizeof(message));
        
StripQuotes(message);
        
        if (!
strlen(message))
            return 
Plugin_Handled;
        
        if (
StrEqual(message"/parse")) {
            
PrintToChat(player"---------> Test <--------- ");
            
            
char results[12][256];
            
int number ParseTest("(some,(weird,string),list,1234,,(more,text,in,a,substring),some,more,$$$,###,@annotation)"results256);
            
            for (
int i 0numberi++)
            
PrintToChat(player"Part %d: %s"1results[i]);
            
            
PrintToChat(player"---------> Test <--------- ");
            return 
Plugin_Handled;
        }
    }
    
    return 
Plugin_Continue;
}

public 
int ParseTest(const char[] textchar[][] resultsint maxLength) {
    
int index 0;
    
int items 0;
    
int startIndex 0;
    
int len 0;
    
int parenthesesCounter 0;
    
    while (
text[index] != EOS) {
        if (
text[index] == '(')
            
parenthesesCounter++;
        
        if (
text[index] == ')')
            
parenthesesCounter--;
        
        if ((
text[index] == ',' || text[index 1] == EOS) && parenthesesCounter == 0) {
            
len index startIndex + (text[index 1] == EOS 0);
            
strcopy(results[items++], maxLength len maxLength lentext[startIndex]);
            
            
items++;
            
startIndex index 1;
        }
        
        
index++;
    }
    
    return 
items;


But the problem that it is not working, it just prints this output:
http://i.pics.rs/vvWhg.png
Instead of:
PHP Code:

Part 1some
Part 2
: (weird,string)
Part 3: list
Part 41234
Part 5

Part 6: (more,text,in,a,substring)
Part 7some
Part 8
more
Part 9
: $$$
Part 10###
Part 11: @annotation 

The original AMXX Code: https://forums.alliedmods.net/showpo...99&postcount=4

I do not know that is wrong with it, everything seems normal.
I have tried using '\0' instead of EOS, it does not work.
Can someone help me, thank you.
Also, it is possible to pass ArrayList by a reference and push the resulting string into it?
I am new to source pawn and programming in general.

MAGNAT2645 04-04-2020 15:54

Re: String parsing problem
 
I think you don't need to pass ArrayList by reference just to push something in it because ArrayList is a child from Handle, Handle itself is like a pointer to object in memory. You work with Handle object and its data via its pointer.
So you can just pass ArrayList (and any other Handle-based thing) as value, not a reference.

Fyren 04-04-2020 20:01

Re: String parsing problem
 
Code:

text[index] == ',' || text[index + 1] == EOS) && parenthesesCounter == 0
This is the expression you're using to determine whether to copy something over into your results. It can only possibly be true when parentheses are balanced. Since your sample input starts with an open parentheses, that means it'll only actually be true at the very end, when the parentheses balance out (and the next character is null for the end of the string).

The code from fysiks doesn't appear to do what you want.

Ludak 04-05-2020 06:17

Re: String parsing problem
 
Quote:

Originally Posted by MAGNAT2645 (Post 2690667)
I think you don't need to pass ArrayList by reference just to push something in it because ArrayList is a child from Handle, Handle itself is like a pointer to object in memory. You work with Handle object and its data via its pointer.
So you can just pass ArrayList (and any other Handle-based thing) as value, not a reference.

Thanks

Ludak 04-05-2020 06:19

Re: String parsing problem
 
Quote:

Originally Posted by Fyren (Post 2690711)
Code:

text[index] == ',' || text[index + 1] == EOS) && parenthesesCounter == 0
This is the expression you're using to determine whether to copy something over into your results. It can only possibly be true when parentheses are balanced. Since your sample input starts with an open parentheses, that means it'll only actually be true at the very end, when the parentheses balance out (and the next character is null for the end of the string).

The code from fysiks doesn't appear to do what you want.

It has worked in AMXX as intended, weird. So I will have to port my own, my own does not parse the last value for some reason.

milutinke 04-05-2020 09:58

Re: String parsing problem
 
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 



All times are GMT -4. The time now is 09:29.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.