| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Reflection;
- using System.Runtime.InteropServices;
- //using System.Fire.Serialize.Property;
- namespace System.ConsoleApp {
- public class ComandInfo<T> {
- public string Command;
- public string ArgumentLine;
- public string GenericAttribute;
- public T Attribute;
- }
- public static class CommandAttribut {
- 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);
- //private static Regex cmdRegexArg = new Regex("(?:[\\s\\t]+\\-(?<arg>[a-zA-Z0-9]+)(?:\\s(?<val>[^([\\s\\t]+\\-)]+))?)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
- public static ComandInfo<T> GetCommand<T>(string s) {
- Match m = cmdRegexCmd.Match(s);
- System.Diagnostics.Debug.WriteLine("RegRes: cmd = '"+m.Groups["cmd"].Value+"'");
- System.Diagnostics.Debug.WriteLine("RegRes: gen = '" + m.Groups["gen"].Value + "'");
- System.Diagnostics.Debug.WriteLine("RegRes: args = '" + m.Groups["args"].Value + "'");
- NameValueCollection attr = GetAttribute(m.Groups["args"].Value);
- for (int i=0,c= attr.Count;i<c;i++) {
- System.Diagnostics.Debug.WriteLine("RegRes Arg['" + attr.GetKey(i) + "'] = '" + attr.Get(i) + "'");
- }
- ComandInfo<T> res = new ComandInfo<T>();
- res.ArgumentLine = s;
- res.Command = m.Groups["cmd"].Value;
- res.GenericAttribute = m.Groups["gen"].Value;
- res.Attribute = Activator.CreateInstance<T>();
-
- Type type = typeof(T);
- TypeInfo typeinfo = type.GetTypeInfo();
- FieldInfo[] fis = typeinfo.GetFields();
- foreach(FieldInfo fi in fis) {
- if (fi.IsPublic && !fi.IsStatic) {
- string name = "";
- string argName = "";
- if (Array.IndexOf<string>(attr.AllKeys, fi.Name.ToLower()) >= 0) {
- name = fi.Name;
- argName = fi.Name.ToLower();
- } else {
- IEnumerable<ArgumentAlias> aas = fi.GetCustomAttributes<ArgumentAlias>(false);
- foreach (ArgumentAlias aa in aas) {
- if (Array.IndexOf<string>(attr.AllKeys, aa.Alias.ToLower()) >= 0) {
- name = fi.Name;
- argName = aa.Alias.ToLower();
- break;
- }
- }
- }
- if(name.Length>0) {
- string sval = attr[argName];
- if (fi.FieldType == typeof(string)) {
- fi.SetValue(res.Attribute, sval);
- } else if(fi.FieldType == typeof(int)) {
- int i;
- if(int.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(uint)) {
- uint i;
- if (uint.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(byte)) {
- byte i;
- if (byte.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(sbyte)) {
- sbyte i;
- if (sbyte.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(short)) {
- short i;
- if (short.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(ushort)) {
- ushort i;
- if (ushort.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(float)) {
- float i;
- if (float.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(double)) {
- double i;
- if (double.TryParse(sval, out i)) fi.SetValue(res.Attribute, i);
- else fi.SetValue(res.Attribute, 0);
- } else if (fi.FieldType == typeof(bool)) {
- fi.SetValue(res.Attribute, true);
- }
- }
- }
- }
- PropertyInfo[] pis = typeinfo.GetProperties();
- foreach (PropertyInfo pi in pis) {
- if (pi.CanWrite) {
- string name = "";
- string argName = "";
- if (Array.IndexOf<string>(attr.AllKeys, pi.Name.ToLower()) >= 0) {
- name = pi.Name;
- argName = pi.Name.ToLower();
- }
- IEnumerable<ArgumentAlias> aas = pi.GetCustomAttributes<ArgumentAlias>(false);
- foreach (ArgumentAlias aa in aas) {
- if (Array.IndexOf<string>(attr.AllKeys, aa.Alias.ToLower()) >= 0) {
- name = pi.Name;
- argName = aa.Alias.ToLower();
- break;
- }
- }
- if (name.Length > 0) {
- string sval = attr[argName];
- if (pi.PropertyType == typeof(string)) {
- pi.SetValue(res.Attribute, sval);
- } else if (pi.PropertyType == typeof(int)) {
- int i;
- if (int.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(uint)) {
- uint i;
- if (uint.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(byte)) {
- byte i;
- if (byte.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(sbyte)) {
- sbyte i;
- if (sbyte.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(short)) {
- short i;
- if (short.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(ushort)) {
- ushort i;
- if (ushort.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(float)) {
- float i;
- if (float.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(double)) {
- double i;
- if (double.TryParse(sval, out i)) pi.SetValue(res.Attribute, i);
- else pi.SetValue(res.Attribute, 0);
- } else if (pi.PropertyType == typeof(bool)) {
- pi.SetValue(res.Attribute, true);
- }
- }
- }
- }
- return res;
- }
- public static NameValueCollection GetAttribute(string s) {
- int spaceCount = 1;
- string currentTag = "general";
- bool bInArgDefinition = false;
- bool bHasValue = false;
- string argValue = "";
- NameValueCollection argRes = new NameValueCollection();
- foreach (char c in s) {
- if (c == ' ' || c=='\t' || c=='\r' || c=='\n') {
- if (bInArgDefinition) {
- bInArgDefinition = false;
- }
- spaceCount++;
- } else if (c == '-' && spaceCount > 0) {
- if (currentTag.Length > 0) {
- if (!bHasValue) {
- argRes[currentTag.ToLower()] = "";
- }
- argRes[currentTag.ToLower()] += argValue.Trim();
- }
- spaceCount = 0;
- argValue = "";
- currentTag = "";
- bInArgDefinition = true;
- bHasValue = false;
- } else {
- if (bInArgDefinition) {
- currentTag += c;
- } else {
- if (spaceCount > 0)
- argValue += new String(' ', spaceCount);
- argValue += c;
- bHasValue = true;
- }
- spaceCount = 0;
- }
- }
- argRes[currentTag] += argValue.Trim();
- return argRes;
- }
-
- }
- }
|