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; i < list.Length; i++) {
list.GetString(i, text, sizeof(text));
PrintToServer("[%d] >>> %s", i + 1, text);
}
}
public void OnPluginEnd() {
}
public int ParseTest(const char[] text, ArrayList results, int partLength) {
int textLength = strlen(text);
int i, currentChar;
bool foundFirst = false;
bool inASubstring = false;
int tempNumber = 0;
int tempLength = 0;
char[] tempPart = new char[partLength];
for (i = 0; i < textLength; i++) {
currentChar = text[i];
// Skip first '('
if (!foundFirst && currentChar == '(') {
foundFirst = true;
continue;
}
// Enter to the substring
else if (foundFirst && currentChar == '(')
inASubstring = true;
// Skip last ')'
if (i == textLength - 1 && 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