comparison Cci/CciCommand.cs @ 0:3ab940a0c7a0

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 11 Sep 2012 16:28:53 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3ab940a0c7a0
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using UCIS.Pml;
5
6 namespace UCIS.Cci {
7 public enum CciResultType {
8 Success,
9 Error,
10 Value,
11 Message,
12 List,
13 Pml,
14 Object,
15 Binary
16 }
17
18 public class CciResult {
19 private CciResultType _type;
20 private Object _value;
21
22 public CciResult(CciResultType type, Object value) {
23 _type = type;
24 _value = value;
25 }
26 public CciResult(CciResultType type) : this(type, null) { }
27 public CciResult(Exception ex) : this(CciResultType.Error, ex) { }
28 public CciResult(Pml.PmlElement pml) : this(CciResultType.Pml, pml) { }
29 public CciResult(byte[] binary) : this(CciResultType.Binary, binary) { }
30 public CciResult(string[] stringList) : this(CciResultType.List, stringList) { }
31 public CciResult(string message) : this(CciResultType.Message, message) { }
32
33 public override string ToString() {
34 if (_value == null) {
35 return _type.ToString();
36 } else {
37 return _type.ToString() + ": " + ValueToString();
38 }
39 }
40 public string ValueToString() {
41 if (_value == null) {
42 return "";
43 } else if (_value is string) {
44 return (string)_value;
45 } else if (_value is byte[]) {
46 return Encoding.UTF8.GetString((byte[])_value);
47 } else if (_value is PmlElement) {
48 return Pml.PmlTextWriter.GetMessageString((PmlElement)_value);
49 } else if (_value is IEnumerable<Object>) {
50 StringBuilder sb = new StringBuilder();
51 foreach (Object i in (Array)_value) sb.AppendLine(i.ToString());
52 return sb.ToString();
53 } else {
54 return _value.ToString();
55 }
56 }
57 public PmlElement ValueToPml() {
58 if (_value == null) return new PmlNull();
59 switch (_type) {
60 case CciResultType.Binary: return new PmlBinary((byte[])_value);
61 case CciResultType.Message:
62 case CciResultType.Object:
63 case CciResultType.Value:
64 case CciResultType.Error: return new PmlString(_value.ToString());
65 case CciResultType.Success: return new PmlInteger(1);
66 case CciResultType.List: {
67 PmlCollection c = new PmlCollection();
68 foreach (Object i in (Array)_value) c.Add(i.ToString());
69 return c;
70 }
71 case CciResultType.Pml: return (PmlElement)_value;
72 default: return new PmlString("Unknown type: " + _type.ToString());
73 }
74 }
75 public PmlElement ToPml() {
76 PmlDictionary d = new PmlDictionary();
77 //d.Add("Type", (int)_type);
78 d.Add("Type", (int)CciResultType.Message);
79 //d.Add("TypeName", _type.ToString());
80 d.Add("TypeName", "Message");
81 //d.Add("Value", ValueToPml());
82 d.Add("Value", ToString());
83 d.Add("String", ToString());
84 return d;
85 }
86
87 public CciResultType Type { get { return _type; } }
88 public Object Value { get { return _value; } }
89 }
90
91 public class CciCommand {
92 private string[] _command;
93 private int _offset;
94 private CciResult _result;
95
96 public CciCommand(string[] command) {
97 _command = command;
98 _offset = 0;
99 _result = null;
100 }
101
102 public int Offset { get { return _offset; } set { _offset = value; } }
103
104 public static CciCommand Parse(string command, bool urlEncode, bool cEscape, bool quotes) {
105 List<string> l = new List<string>();
106
107 StringBuilder part = new StringBuilder();
108 bool inQuotes = false;
109 int len = command.Length;
110 for (int i = 0; i < len; i++) {
111 char c = command[i];
112 if (quotes && !inQuotes && c == '"') {
113 inQuotes = true;
114 } else if (quotes && inQuotes && c == '"') {
115 inQuotes = false;
116 } else if (urlEncode && c == '%') {
117 part.Append(Uri.HexUnescape(command, ref i));
118 i--;
119 } else if (cEscape && c == '\\' && i + 1 < len) {
120 i++;
121 switch (command[i]) {
122 case 't': part.Append('\t'); break;
123 case 'n': part.Append('\n'); break;
124 case 'r': part.Append('\r'); break;
125 default: part.Append(command[i]); break;
126 }
127 } else if (!inQuotes && (c == ' ' || c == '\t')) {
128 if (part.Length > 0) l.Add(part.ToString());
129 part.Length = 0;
130 } else {
131 part.Append(c);
132 }
133 }
134 if (part.Length > 0) l.Add(part.ToString());
135
136 return new CciCommand(l.ToArray());
137 }
138
139 public PmlCollection ToPml() { return ToPml(0); }
140 public PmlCollection ToPml(int offset) {
141 PmlCollection c = new PmlCollection();
142 for (int i = _offset + offset; i < _command.Length; i++) c.Add(_command[i]);
143 return c;
144 }
145 public static CciCommand FromPml(PmlCollection pml) {
146 List<string> l = new List<string>();
147 foreach (PmlElement e in pml) l.Add(e.ToString());
148 return new CciCommand(l.ToArray());
149 }
150
151 public string Command {
152 get {
153 return _command[_offset];
154 }
155 }
156
157 public string GetArgument(int index) {
158 return _command[_offset + index + 1];
159 }
160
161 public CciCommand Jump(int offset) {
162 _offset += offset;
163 return this;
164 }
165
166 public int Count {
167 get {
168 return _command.Length - _offset - 1;
169 }
170 }
171
172 public CciResult Result {
173 get {
174 return _result;
175 }
176 set {
177 _result = value;
178 }
179 }
180
181 public CciCommand Return(CciResult result) {
182 _result = result;
183 return this;
184 }
185 public CciCommand Return(CciResultType type, Object value) { return Return(new CciResult(type, value)); }
186 public CciCommand Return() { return Return(CciResultType.Success); }
187 public CciCommand Return(int value) { return Return(CciResultType.Value, value); }
188 public CciCommand Return(CciResultType type) { return Return(type, null); }
189 public CciCommand Return(Exception ex) { return Return(CciResultType.Error, ex); }
190 public CciCommand Return(PmlElement pml) { return Return(CciResultType.Pml, pml); }
191 public CciCommand Return(byte[] binary) { return Return(CciResultType.Binary, binary); }
192 public CciCommand Return(string[] stringList) { return Return(CciResultType.List, stringList); }
193 public CciCommand Return(string message) { return Return(CciResultType.Message, message); }
194 }
195 }