CommandAttribut.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. //using System.Fire.Serialize.Property;
  9. namespace System.ConsoleApp {
  10. public class ComandInfo<T> {
  11. public string Command;
  12. public string ArgumentLine;
  13. public string GenericAttribute;
  14. public T Attribute;
  15. }
  16. public static class CommandAttribut {
  17. private static Regex cmdRegexCmd = new Regex("^(?<cmd>[a-zA-Z][a-zA-Z0-9]*)(?:[\\s\\t]+(?<gen>.+?))?(?<args>[\\s\\t]+\\-[a-zA-Z0-9].*?)$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
  18. //private static Regex cmdRegexArg = new Regex("(?:[\\s\\t]+\\-(?<arg>[a-zA-Z0-9]+)(?:\\s(?<val>[^([\\s\\t]+\\-)]+))?)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
  19. public static ComandInfo<T> GetCommand<T>(string s) {
  20. Match m = cmdRegexCmd.Match(s);
  21. System.Diagnostics.Debug.WriteLine("RegRes: cmd = '"+m.Groups["cmd"].Value+"'");
  22. System.Diagnostics.Debug.WriteLine("RegRes: gen = '" + m.Groups["gen"].Value + "'");
  23. System.Diagnostics.Debug.WriteLine("RegRes: args = '" + m.Groups["args"].Value + "'");
  24. NameValueCollection attr = GetAttribute(m.Groups["args"].Value);
  25. for (int i=0,c= attr.Count;i<c;i++) {
  26. System.Diagnostics.Debug.WriteLine("RegRes Arg['" + attr.GetKey(i) + "'] = '" + attr.Get(i) + "'");
  27. }
  28. ComandInfo<T> res = new ComandInfo<T>();
  29. res.ArgumentLine = s;
  30. res.Command = m.Groups["cmd"].Value;
  31. res.GenericAttribute = m.Groups["gen"].Value;
  32. res.Attribute = Activator.CreateInstance<T>();
  33. Type type = typeof(T);
  34. TypeInfo typeinfo = type.GetTypeInfo();
  35. FieldInfo[] fis = typeinfo.GetFields();
  36. foreach(FieldInfo fi in fis) {
  37. if (fi.IsPublic && !fi.IsStatic) {
  38. string name = "";
  39. string argName = "";
  40. if (Array.IndexOf<string>(attr.AllKeys, fi.Name.ToLower()) >= 0) {
  41. name = fi.Name;
  42. argName = fi.Name.ToLower();
  43. } else {
  44. IEnumerable<ArgumentAlias> aas = fi.GetCustomAttributes<ArgumentAlias>(false);
  45. foreach (ArgumentAlias aa in aas) {
  46. if (Array.IndexOf<string>(attr.AllKeys, aa.Alias.ToLower()) >= 0) {
  47. name = fi.Name;
  48. argName = aa.Alias.ToLower();
  49. break;
  50. }
  51. }
  52. }
  53. if(name.Length>0) {
  54. string sval = attr[argName];
  55. if (fi.FieldType == typeof(string)) {
  56. fi.SetValue(res.Attribute, sval);
  57. } else if(fi.FieldType == typeof(int)) {
  58. int i;
  59. if(int.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  60. else fi.SetValue(res.Attribute, 0);
  61. } else if (fi.FieldType == typeof(uint)) {
  62. uint i;
  63. if (uint.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  64. else fi.SetValue(res.Attribute, 0);
  65. } else if (fi.FieldType == typeof(byte)) {
  66. byte i;
  67. if (byte.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  68. else fi.SetValue(res.Attribute, 0);
  69. } else if (fi.FieldType == typeof(sbyte)) {
  70. sbyte i;
  71. if (sbyte.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  72. else fi.SetValue(res.Attribute, 0);
  73. } else if (fi.FieldType == typeof(short)) {
  74. short i;
  75. if (short.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  76. else fi.SetValue(res.Attribute, 0);
  77. } else if (fi.FieldType == typeof(ushort)) {
  78. ushort i;
  79. if (ushort.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  80. else fi.SetValue(res.Attribute, 0);
  81. } else if (fi.FieldType == typeof(float)) {
  82. float i;
  83. if (float.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  84. else fi.SetValue(res.Attribute, 0);
  85. } else if (fi.FieldType == typeof(double)) {
  86. double i;
  87. if (double.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
  88. else fi.SetValue(res.Attribute, 0);
  89. } else if (fi.FieldType == typeof(bool)) {
  90. fi.SetValue(res.Attribute, true);
  91. }
  92. }
  93. }
  94. }
  95. PropertyInfo[] pis = typeinfo.GetProperties();
  96. foreach (PropertyInfo pi in pis) {
  97. if (pi.CanWrite) {
  98. string name = "";
  99. string argName = "";
  100. if (Array.IndexOf<string>(attr.AllKeys, pi.Name.ToLower()) >= 0) {
  101. name = pi.Name;
  102. argName = pi.Name.ToLower();
  103. }
  104. IEnumerable<ArgumentAlias> aas = pi.GetCustomAttributes<ArgumentAlias>(false);
  105. foreach (ArgumentAlias aa in aas) {
  106. if (Array.IndexOf<string>(attr.AllKeys, aa.Alias.ToLower()) >= 0) {
  107. name = pi.Name;
  108. argName = aa.Alias.ToLower();
  109. break;
  110. }
  111. }
  112. if (name.Length > 0) {
  113. string sval = attr[argName];
  114. if (pi.PropertyType == typeof(string)) {
  115. pi.SetValue(res.Attribute, sval);
  116. } else if (pi.PropertyType == typeof(int)) {
  117. int i;
  118. if (int.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  119. else pi.SetValue(res.Attribute, 0);
  120. } else if (pi.PropertyType == typeof(uint)) {
  121. uint i;
  122. if (uint.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  123. else pi.SetValue(res.Attribute, 0);
  124. } else if (pi.PropertyType == typeof(byte)) {
  125. byte i;
  126. if (byte.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  127. else pi.SetValue(res.Attribute, 0);
  128. } else if (pi.PropertyType == typeof(sbyte)) {
  129. sbyte i;
  130. if (sbyte.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  131. else pi.SetValue(res.Attribute, 0);
  132. } else if (pi.PropertyType == typeof(short)) {
  133. short i;
  134. if (short.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  135. else pi.SetValue(res.Attribute, 0);
  136. } else if (pi.PropertyType == typeof(ushort)) {
  137. ushort i;
  138. if (ushort.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  139. else pi.SetValue(res.Attribute, 0);
  140. } else if (pi.PropertyType == typeof(float)) {
  141. float i;
  142. if (float.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  143. else pi.SetValue(res.Attribute, 0);
  144. } else if (pi.PropertyType == typeof(double)) {
  145. double i;
  146. if (double.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
  147. else pi.SetValue(res.Attribute, 0);
  148. } else if (pi.PropertyType == typeof(bool)) {
  149. pi.SetValue(res.Attribute, true);
  150. }
  151. }
  152. }
  153. }
  154. return res;
  155. }
  156. public static NameValueCollection GetAttribute(string s) {
  157. int spaceCount = 1;
  158. string currentTag = "general";
  159. bool bInArgDefinition = false;
  160. bool bHasValue = false;
  161. string argValue = "";
  162. NameValueCollection argRes = new NameValueCollection();
  163. foreach (char c in s) {
  164. if (c == ' ' || c=='\t' || c=='\r' || c=='\n') {
  165. if (bInArgDefinition) {
  166. bInArgDefinition = false;
  167. }
  168. spaceCount++;
  169. } else if (c == '-' && spaceCount > 0) {
  170. if (currentTag.Length > 0) {
  171. if (!bHasValue) {
  172. argRes[currentTag.ToLower()] = "";
  173. }
  174. argRes[currentTag.ToLower()] += argValue.Trim();
  175. }
  176. spaceCount = 0;
  177. argValue = "";
  178. currentTag = "";
  179. bInArgDefinition = true;
  180. bHasValue = false;
  181. } else {
  182. if (bInArgDefinition) {
  183. currentTag += c;
  184. } else {
  185. if (spaceCount > 0)
  186. argValue += new String(' ', spaceCount);
  187. argValue += c;
  188. bHasValue = true;
  189. }
  190. spaceCount = 0;
  191. }
  192. }
  193. argRes[currentTag] += argValue.Trim();
  194. return argRes;
  195. }
  196. }
  197. }