/
SymCommand.cs
executable file
·111 lines (106 loc) · 3.57 KB
/
SymCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Symitar
{
public class SymCommand
{
//========================================================================
public string command = "";
public string data = "";
public Dictionary<string, string> parameters = new Dictionary<string, string>();
private static Regex cmdPattern = new Regex("(.*?)~.*");
private static int currMsgId = 10000;
//========================================================================
public SymCommand()
{
parameters.Add("MsgId", currMsgId.ToString());
currMsgId++;
}
//------------------------------------------------------------------------
public SymCommand(string cmd)
{
command = cmd;
parameters.Add("MsgId", currMsgId.ToString());
currMsgId++;
}
//------------------------------------------------------------------------
public SymCommand(string cmd, Dictionary<string, string>parms, string dat)
{
command = cmd;
parameters = parms;
data = dat;
parameters.Add("MsgId", currMsgId.ToString());
currMsgId++;
}
//========================================================================
public static SymCommand Parse(string dat)
{
char[] seperators = {'~'};
SymCommand cmd = new SymCommand();
cmd.data = dat;
if((dat.IndexOf("~") != -1) && (dat.IndexOf('\u00FD') == -1))
{
Match match = cmdPattern.Match(dat);
cmd.command = match.Groups[1].Value;
string[] sep = dat.Substring(cmd.command.Length + 1).Split(seperators);
for(int i=0; i<sep.Length; i++)
{
int eqPos = sep[i].IndexOf("=");
if(eqPos != -1)
cmd.SetParam(sep[i].Substring(0,eqPos), sep[i].Substring(eqPos+1));
else
cmd.SetParam(sep[i],"");
}
}
else
cmd.command = dat;
return cmd;
}
//------------------------------------------------------------------------
public string GetFileData()
{
int fd = data.IndexOf('\u00FD');
int fe = data.IndexOf('\u00FE');
if((fd != -1) && (fe != -1))
return data.Substring(fd+1, fe-fd-1);
return "";
}
//------------------------------------------------------------------------
public string Packet()
{
string dat = command + '~';
foreach(KeyValuePair<string, string> param in parameters)
{
dat += param.Key;
if(param.Value != "")
dat += '=' + param.Value;
dat += '~';
}
dat = dat.Substring(0, dat.Length - 1);
return '\u0007' + dat.Length.ToString() + '\r' + dat;
}
//========================================================================
public void SetParam(string prm, string value)
{
if (parameters.ContainsKey(prm) == true)
parameters[prm] = value;
else
parameters.Add(prm, value);
}
//------------------------------------------------------------------------
public bool HasParam(string prm)
{
return parameters.ContainsKey(prm);
}
//------------------------------------------------------------------------
public string GetParam(string prm)
{
if (parameters.ContainsKey(prm) == true)
return parameters[prm];
return "";
}
//------------------------------------------------------------------------
}
}