Created
May 29, 2015 23:24
-
-
Save maca134/94b78268f6727f5967f8 to your computer and use it in GitHub Desktop.
Parse ARMA String Array in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace mgroups.Core | |
{ | |
public class ArmaArray : List<object> | |
{ | |
public static ArmaArray Unserialize(string strarray) | |
{ | |
if (strarray.ElementAt(0) != '[') | |
{ | |
throw new ArmaArrayException("String does not start with a ["); | |
} | |
strarray = strarray.Substring(1); | |
ArmaArray array = new ArmaArray(); | |
char[] nums = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', '-' }; | |
for (int i = 0; i < strarray.Length; i++) | |
{ | |
if (strarray[i] == '[') | |
{ | |
StringBuilder str = new StringBuilder(); | |
int inArr = 0; | |
while (true) | |
{ | |
if (strarray[i] == '[') | |
{ | |
inArr++; | |
} | |
if (strarray[i] == ']') | |
{ | |
inArr--; | |
} | |
str.Append(strarray[i]); | |
i++; | |
if (inArr == 0) | |
{ | |
break; | |
} | |
} | |
List<object> innerArray = Unserialize(str.ToString()); | |
array.Add(innerArray); | |
} | |
else if (strarray[i] == '"') | |
{ | |
StringBuilder str = new StringBuilder(); | |
bool isEnd = false; | |
i++; | |
while (true) | |
{ | |
try | |
{ | |
if (strarray[i] == '"') | |
{ | |
isEnd = !isEnd; | |
} | |
} | |
catch | |
{ | |
break; | |
} | |
if (isEnd && (strarray[i] == ',' || strarray[i] == ']')) | |
{ | |
break; | |
} | |
str.Append(strarray[i]); | |
i++; | |
} | |
array.Add(str.ToString().TrimEnd('"')); | |
} | |
else if (nums.Contains(strarray[i])) | |
{ | |
StringBuilder str = new StringBuilder(); | |
bool isFloat = false; | |
while (nums.Contains(strarray[i])) | |
{ | |
if (strarray[i] == '.') | |
isFloat = true; | |
str.Append(strarray[i]); | |
i++; | |
} | |
if (isFloat) | |
{ | |
double num = Convert.ToDouble(str.ToString()); | |
array.Add(num); | |
} | |
else | |
{ | |
int num = Convert.ToInt32(str.ToString()); | |
array.Add(num); | |
} | |
} | |
else if (Substring(strarray, i, 4).ToLower() == "true") | |
{ | |
array.Add(true); | |
i = i + 4; | |
} | |
else if (Substring(strarray, i, 5).ToLower() == "false") | |
{ | |
array.Add(false); | |
i = i + 5; | |
} | |
} | |
return array; | |
} | |
public static string Serialize(ArmaArray array) | |
{ | |
StringBuilder data = new StringBuilder(); | |
data.Append("["); | |
if (array == null) | |
{ | |
data.Append("]"); | |
return data.ToString(); | |
} | |
foreach (object d in array) | |
{ | |
if (d is string) | |
{ | |
data.Append("\""); | |
string s = d as string; | |
data.Append(s); | |
data.Append("\""); | |
} | |
else if (d is int || d is double || d is bool) | |
{ | |
data.Append(d.ToString()); | |
} | |
else if (d is ArmaArray) | |
{ | |
ArmaArray a = d as ArmaArray; | |
data.Append(Serialize(a)); | |
} | |
data.Append(","); | |
} | |
if (data[data.Length - 1] == ',') | |
{ | |
data.Length--; | |
} | |
data.Append("]"); | |
return data.ToString(); | |
} | |
private static string Substring(string input, int start, int length) | |
{ | |
int inputLength = input.Length; | |
if (start + length >= inputLength) | |
{ | |
return input.Substring(start); | |
} | |
return input.Substring(start, length); | |
} | |
public int AsInt(int index) | |
{ | |
try | |
{ | |
return Convert.ToInt32(this[index]); | |
} | |
catch | |
{ | |
return -1; | |
} | |
} | |
public float AsFloat(int index) | |
{ | |
try | |
{ | |
return Convert.ToSingle(this[index]); | |
} | |
catch | |
{ | |
return -1f; | |
} | |
} | |
public ArmaArray AsArray(int index) | |
{ | |
return (this[index] as ArmaArray); | |
} | |
public string AsString(int index) | |
{ | |
return (this[index] as string); | |
} | |
public bool AsBool(int index) | |
{ | |
try | |
{ | |
return Convert.ToBoolean(this[index]); | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
public override string ToString() | |
{ | |
return Serialize(this); | |
} | |
public long AsLong(int index) | |
{ | |
try | |
{ | |
return Convert.ToInt64(this[index]); | |
} | |
catch | |
{ | |
return -1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment