2007-01-19 10:09:23 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2008-07-21 21:12:59 +00:00
|
|
|
namespace OpenMetaverse.TestClient
|
2007-08-29 08:55:53 +00:00
|
|
|
{
|
|
|
|
|
class Parsing
|
|
|
|
|
{
|
|
|
|
|
public static string[] ParseArguments(string str)
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
List<string> list = new List<string>();
|
2019-10-30 20:39:56 -05:00
|
|
|
string current = string.Empty;
|
2007-01-19 10:09:23 +00:00
|
|
|
string trimmed = null;
|
|
|
|
|
bool withinQuote = false;
|
|
|
|
|
bool escaped = false;
|
2007-08-29 08:55:53 +00:00
|
|
|
|
|
|
|
|
foreach (char c in str)
|
|
|
|
|
{
|
|
|
|
|
if (c == '"')
|
|
|
|
|
{
|
|
|
|
|
if (escaped)
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
current += '"';
|
|
|
|
|
escaped = false;
|
2007-08-29 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
current += '"';
|
|
|
|
|
withinQuote = !withinQuote;
|
|
|
|
|
}
|
2007-08-29 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
else if (c == ' ' || c == '\t')
|
|
|
|
|
{
|
|
|
|
|
if (escaped || withinQuote)
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
current += c;
|
|
|
|
|
escaped = false;
|
2007-08-29 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
trimmed = current.Trim();
|
2007-08-29 08:55:53 +00:00
|
|
|
if (trimmed.StartsWith("\"") && trimmed.EndsWith("\""))
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
trimmed = trimmed.Remove(0, 1);
|
|
|
|
|
trimmed = trimmed.Remove(trimmed.Length - 1);
|
|
|
|
|
trimmed = trimmed.Trim();
|
|
|
|
|
}
|
|
|
|
|
if (trimmed.Length > 0)
|
|
|
|
|
list.Add(trimmed);
|
2007-08-29 08:55:53 +00:00
|
|
|
current = String.Empty;
|
2007-01-19 10:09:23 +00:00
|
|
|
}
|
2007-08-29 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
else if (c == '\\')
|
|
|
|
|
{
|
|
|
|
|
if (escaped)
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
current += '\\';
|
|
|
|
|
escaped = false;
|
2007-08-29 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
escaped = true;
|
|
|
|
|
}
|
2007-08-29 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
if (escaped)
|
|
|
|
|
throw new FormatException(c.ToString() + " is not an escapable character.");
|
|
|
|
|
current += c;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-29 08:55:53 +00:00
|
|
|
|
2007-01-19 10:09:23 +00:00
|
|
|
trimmed = current.Trim();
|
2007-08-29 08:55:53 +00:00
|
|
|
|
|
|
|
|
if (trimmed.StartsWith("\"") && trimmed.EndsWith("\""))
|
|
|
|
|
{
|
2007-01-19 10:09:23 +00:00
|
|
|
trimmed = trimmed.Remove(0, 1);
|
|
|
|
|
trimmed = trimmed.Remove(trimmed.Length - 1);
|
|
|
|
|
trimmed = trimmed.Trim();
|
|
|
|
|
}
|
2007-08-29 08:55:53 +00:00
|
|
|
|
2007-01-19 10:09:23 +00:00
|
|
|
if (trimmed.Length > 0)
|
|
|
|
|
list.Add(trimmed);
|
2007-08-29 08:55:53 +00:00
|
|
|
|
2007-01-19 10:09:23 +00:00
|
|
|
return list.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|