private static ExternalConnection ParseExternalConnection(string line) { if (string.IsNullOrEmpty(line) || !line.EndsWith("*)")) { return(null); } string name = string.Empty; string dataType = string.Empty; ExternalConnection variable = null; int nameStart; int typeStart; int typeEnd; if (line.StartsWith(sInvalidGlobVarPrefix)) { //非法 nameStart = sInvalidGlobVarPrefix.Length; typeStart = line.IndexOf(':', nameStart) + 1; if (typeStart <= nameStart) { return(null); } name = line.Substring(nameStart, typeStart - nameStart - 1); typeEnd = line.IndexOf(';', typeStart); if (typeEnd <= typeStart) { return(null); } dataType = line.Substring(typeStart, typeEnd - typeStart); variable = new ExternalConnection(name, ViDataType.GetDataType(dataType)); variable.ValidState = false; } else { // 合法 nameStart = 0; typeStart = line.IndexOf(':') + 1; if (typeStart <= nameStart) { return(null); } name = line.Substring(nameStart, typeStart - nameStart - 1); typeEnd = line.IndexOf(';', typeStart); if (typeEnd <= typeStart) { return(null); } dataType = line.Substring(typeStart, typeEnd - typeStart); variable = new ExternalConnection(name, ViDataType.GetDataType(dataType)); variable.ValidState = true; } return(variable); }
/// <summary> /// 将文本行描述的变量定义,解析为管脚类型对象。 /// </summary> /// <param name="decl">管脚的文本行描述</param> /// <param name="typeCreation">自动创建管脚数据类型的方式</param> /// <param name="creator">ViConnType 的创建函数</param> /// <returns>管脚类型对象,null 表示解析失败</returns> public static ViConnType Parse(string decl, ViTypeCreation typeCreation, Func <string, ViDataType, ViConnType> creator) { string name = null, type = null, comment = null; string[] addiInfo = null; if (!SeperateNameType(decl.Trim(), ref name, ref type, ref addiInfo, ref comment)) { return(null); } ViDataType dataType = null; if (!string.IsNullOrEmpty(type)) { dataType = ViDataType.GetDataType(type, typeCreation); if (dataType == null) { return(null); } } ViConnType connType = creator(name, dataType); if (connType == null) { return(null); } // 备注 connType.Comment = comment; if (addiInfo != null) { // 特性 foreach (string info in addiInfo) { if (info.Equals("F_EDGE", StringComparison.OrdinalIgnoreCase)) { connType.Attributes |= ViConnAttributes.F_EDGE; } else if (info.Equals("R_EDGE", StringComparison.OrdinalIgnoreCase)) { connType.Attributes |= ViConnAttributes.R_EDGE; } } // 缺省值 if (addiInfo.Count() >= 2 && addiInfo[addiInfo.Count() - 2] == ":=") { connType.DefaultValue = addiInfo[addiInfo.Count() - 1]; } } return(connType); }
public bool DeserializeFrom(ViBlur blur, System.Xml.XmlReader reader) { string name = reader.GetAttribute(blur, "name"); string connectable = reader.GetAttribute("connectable"); string fast = reader.GetAttribute("fast"); string attributes = reader.GetAttribute("attributes"); string defaultValue = reader.GetAttribute("defaultValue"); string connected = reader.GetAttribute("connected"); //Name if (!string.IsNullOrEmpty(name)) { this.Name = name; } //Connectable if (!string.IsNullOrEmpty(connectable)) { this.Connectable = bool.Parse(connectable); } //Fast if (!string.IsNullOrEmpty(fast)) { this.Fast = bool.Parse(fast); } //Attributes if (!string.IsNullOrEmpty(attributes)) { if (string.Compare(attributes, ViConnAttributes.F_EDGE.ToString(), true) == 0) { this.Attributes = ViConnAttributes.F_EDGE; } else if (string.Compare(attributes, ViConnAttributes.R_EDGE.ToString(), true) == 0) { this.Attributes = ViConnAttributes.R_EDGE; } else { this.Attributes = ViConnAttributes.None; } } if (!string.IsNullOrEmpty(defaultValue)) { this.DefaultValue = defaultValue; } while (!reader.EOF) { if (!reader.Read()) { break; } if (reader.NodeType == System.Xml.XmlNodeType.Whitespace) { continue; } if (reader.NodeType == System.Xml.XmlNodeType.Element) { if (string.Compare(reader.Name, "dataType", true) == 0) { string typeName = reader.GetAttribute("typeName"); //读取Value if (!reader.IsEmptyElement && string.IsNullOrEmpty(typeName)) { if (!reader.Read()) { break; } typeName = reader.Value; } ViDataType dataType = ViDataType.GetDataType(typeName); this.Type = dataType; } } else if (reader.NodeType == System.Xml.XmlNodeType.EndElement) { if (string.Compare(reader.Name, ViConnType.TAG, true) == 0) { return(true); } } } return(false); }