From e085f2ecef5b7e08cf4328466d87f2d9ece3f9b4 Mon Sep 17 00:00:00 2001 From: otakup0pe Date: Fri, 19 Jan 2007 10:09:23 +0000 Subject: [PATCH] I found this one hiding under the bed. git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@857 52acb1d6-8a22-11de-b505-999d5b087335 --- .../examples/TestClient/Parsing.cs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 libsecondlife-cs/examples/TestClient/Parsing.cs diff --git a/libsecondlife-cs/examples/TestClient/Parsing.cs b/libsecondlife-cs/examples/TestClient/Parsing.cs new file mode 100644 index 00000000..1e081c1c --- /dev/null +++ b/libsecondlife-cs/examples/TestClient/Parsing.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace libsecondlife.TestClient { + class Parsing { + public static string[] ParseArguments(string str) { + List list = new List(); + string current = ""; + string trimmed = null; + bool withinQuote = false; + bool escaped = false; + foreach (char c in str) { + if (c == '"') { + if (escaped) { + current += '"'; + escaped = false; + } else { + current += '"'; + withinQuote = !withinQuote; + } + } else if (c == ' ' || c == '\t') { + if (escaped || withinQuote) { + current += c; + escaped = false; + } else { + trimmed = current.Trim(); + if (trimmed.StartsWith("\"") && trimmed.EndsWith("\"")) { + trimmed = trimmed.Remove(0, 1); + trimmed = trimmed.Remove(trimmed.Length - 1); + trimmed = trimmed.Trim(); + } + if (trimmed.Length > 0) + list.Add(trimmed); + current = ""; + } + } else if (c == '\\') { + if (escaped) { + current += '\\'; + escaped = false; + } else { + escaped = true; + } + } else { + if (escaped) + throw new FormatException(c.ToString() + " is not an escapable character."); + current += c; + } + } + trimmed = current.Trim(); + if (trimmed.StartsWith("\"") && trimmed.EndsWith("\"")) { + trimmed = trimmed.Remove(0, 1); + trimmed = trimmed.Remove(trimmed.Length - 1); + trimmed = trimmed.Trim(); + } + if (trimmed.Length > 0) + list.Add(trimmed); + return list.ToArray(); + } + } +}