private StructuredDataElement GetArrayIndex(StructuredDataElement item, string name) { var index = int.Parse(name); if (index >= item.Length) { throw new IndexOutOfRangeException("Index is outside of the indexedarr's bounds."); } var childSize = item.ChildSize; item.Type = item.ChildType; item.ResetData(); if (item.Type == "bool") { item.Offset += (index / 8); // should be floored item.Bit = (index % 8); } else { item.Offset += (index * childSize); } return(item); }
private StructuredDataElement Trace(string[] path) { var element = new StructuredDataElement(); element.Type = "playerdata"; element.Offset = 0; foreach (var name in path) { switch (GetActualType(element.Type)) { case "struct": element = GetStructChild(element, name); break; case "indexedarr": element = GetArrayIndex(element, name); break; case "enumarr": element = GetArrayEnum(element, name); break; } } return(element); }
internal void GetStructuredDataString(StructuredDataElement sd, StringBuilder sb) { string idName = null; if (sd == null) { return; } if (sd.ID == null) { idName = "DATA"; } else { idName = sd.ID; } sb.Append("["); sb.Append(GetStructuredDataID(idName)); foreach (string key in sd.Properties.AllKeys) { foreach (var value in sd.Properties.GetValues(key)) { sb.Append(" "); sb.AppendFormat("{0}=\"{1}\"", GetStructuredDataID(key), GetStructuredDataValue(value)); } } sb.Append("]"); }
public FontProperty(int indicator, int priority, StructuredDataElement record) { Indicator = indicator; Name = GetName(indicator); Priority = priority; Record = record; }
private StructuredDataValue ReadItem(StructuredDataElement element) { var offset = element.Offset;// +4; object item = null; switch (GetActualType(element.Type)) { case "int": item = ReadInt32(offset); break; case "short": item = ReadInt16(offset); break; case "byte": item = ReadInt8(offset); break; case "float": item = ReadFloat(offset); break; case "enum": var value = ReadInt16(offset); foreach (var enumItem in _enums[element.Type]) { if (enumItem.Value == value) { item = enumItem.Key; break; } } break; case "string": item = ReadString(offset, element.Length); break; case "bool": var bvalue = ReadInt8(offset); if (element.Bit > 0) { bvalue >>= element.Bit; bvalue &= 1; } item = (bvalue == 1) ? true : false; break; } return(new StructuredDataValue(item)); }
public void StructuredDataIdWillAlwaysBePrependedWithAPrivateEnterpriseNumber() { var parameters = new Dictionary <string, string> { { "key1", "aaa" } }; var sut = new StructuredDataElement("SomeSdId", parameters); Assert.Equal("SomeSdId@" + StructuredDataElement.DefaultPrivateEnterpriseNumber, sut.SdId); }
public void StructuredDataIdWillRetainProvidedPrivateEnterpriseNumber() { var parameters = new Dictionary <string, string> { { "key1", "aaa" } }; var sut = new StructuredDataElement("SomeSdId@12345", parameters); Assert.Equal("SomeSdId@12345", sut.SdId); }
public void StructuredDataIdWillAlwaysBePrependedWithAPrivateEnterpriseNumber() { var parameters = new Dictionary<string, string> { { "key1", "aaa" } }; var sut = new StructuredDataElement("SomeSdId", parameters); Assert.Equal("SomeSdId@" + StructuredDataElement.DefaultPrivateEnterpriseNumber, sut.SdId); }
public void StructuredDataIdWillRetainProvidedPrivateEnterpriseNumber() { var parameters = new Dictionary<string, string> { { "key1", "aaa" } }; var sut = new StructuredDataElement("SomeSdId@12345", parameters); Assert.Equal("SomeSdId@12345", sut.SdId); }
private StructuredDataElement GetStructChild(StructuredDataElement item, string name) { var found = false; var structure = _structs[item.Type]; foreach (var element in structure) { if (element.Key == name) { item.Type = element.Value.Type; item.Offset += element.Value.Offset; item.ResetData(); if (element.Value.Length > 0) { item.Length = element.Value.Length; } if (element.Value.ChildType != null) { item.ChildType = element.Value.ChildType; item.ChildSize = element.Value.ChildSize; } found = true; } } if (!found) { throw new KeyNotFoundException("Could not find any such key in the specified struct."); } return(item); }
private StructuredDataElement GetArrayEnum(StructuredDataElement item, string name) { var found = false; var enumArray = _enumArrays[item.Type]; var enumeration = _enums[enumArray.Enum]; foreach (var enumItem in enumeration) { if (enumItem.Key == name) { var index = enumItem.Value; item.ResetData(); item.Type = enumArray.Type; if (item.Type == "bool") { item.Offset += (index / 8); // should be floored item.Bit = (index % 8); } else { item.Offset += (index * enumArray.Size); } found = true; } } if (!found) { throw new KeyNotFoundException("Could not find any such key in the specified enum."); } return(item); }
public IList <StructuredDataElement> ParseStructuredData(string sd) { List <StructuredDataElement> results = new List <StructuredDataElement>(); sd = sd.Trim(); StructuredDataElement current = null; //normalize structured data using (StringReader sr = new StringReader(sd)) { char c = (char)sr.Read(); //first char should be start of sd element if (c == '[') { current = new StructuredDataElement(); } else { throw new FormatException(); //not in format } current = new StructuredDataElement(); //we have the start of thing, read the name current.ID = sr.ReadUntilSpace(); while (sr.Peek() != -1) { //now read property names string key = sr.ReadUntilCharAndThenConsume('='); //next char should be quote if (sr.Read() != '"') { throw new FormatException(); //not in format } //now read the value string value = sr.ReadUntilCharAndThenConsume('"'); //if value ends in escpae char, then while (value.EndsWith("\\")) { string addition = "\"" + sr.ReadUntilCharAndThenConsume('"'); value += addition; } value = SyslogUtil.UnescapeStructuredDataValue(value); current.Properties.Add(key, value); //end of key/value pair, read another one sr.MaybeConsumeSpaces(); if (sr.Peek() == ']') //check to see if we are end of sd { sr.Read(); results.Add(current); current = null; } if (sr.Peek() == '[') //check to see if we are new start { //consume start char sr.Read(); current = new StructuredDataElement(); //we have the start of thing, read the name string name = sr.ReadUntilSpace(); current.ID = name; } } } return(results); }