protected virtual ControlCode ProcessChar(BinaryStream stream, StringBuilder sb, bool isSaturn, ref int count, out ContextString contextString) { contextString = null; int chPosition = stream.Position; short ch = stream.ReadShort(); count++; ControlCode code = ControlCodes.FirstOrDefault(cc => cc.Code == ch); var charLookup = isSaturn ? SaturnLookup : CharLookup; if (code != null) { if (code.Code != -1) { sb.Append('['); if (code.Tag != null) { sb.Append(code.Tag); } else { sb.Append(((ushort)ch).ToString("X4")); } for (int i = 0; i < code.Arguments; i++) { ch = stream.ReadShort(); count++; sb.Append(' '); sb.Append(((ushort)ch).ToString("X4")); } sb.Append(']'); } } else { if (charLookup.ContainsKey(ch)) { contextString = charLookup[ch]; sb.Append(contextString.String); } else { sb.Append('['); sb.Append(((ushort)ch).ToString("X4")); sb.Append(']'); //logger.Debug($"Unrecognized character: [{ch:X4}] at 0x{chPosition:X}"); } } return(code); }
public ReverseLookup(IEnumerable <KeyValuePair <short, ContextString> > charLookup) { var unique = charLookup.Where(cs => cs.Value.String.Length > 0) .GroupBy(cs => cs.Value) .Select(g => g.First()); foreach (var kv in unique) { short value = kv.Key; ContextString cs = kv.Value; char first = cs.String[0]; LookupNode root; if (tree.ContainsKey(first)) { root = tree[first]; } else { root = new LookupNode(); tree.Add(cs.String[0], root); } root.Depth = 1; int currentIndex = 1; while (currentIndex < cs.String.Length) { char currentChar = cs.String[currentIndex]; LookupNode node; if (root.Children.ContainsKey(currentChar)) { node = root.Children[currentChar]; } else { node = new LookupNode(); root.Children.Add(currentChar, node); } node.Parent = root; root = node; currentIndex++; root.Depth = currentIndex; } root.Values.Add(cs.Context, value); } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { ContextString str = value as ContextString; if (str.Context == Context.None) { serializer.Serialize(writer, str.String); } else { serializer.Serialize(writer, str); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.String) { return(new ContextString((string)reader.Value)); } else if (reader.TokenType == JsonToken.StartObject) { JObject jObject = JObject.Load(reader); ContextString str = new ContextString(); serializer.Populate(jObject.CreateReader(), str); return(str); } throw new JsonReaderException("Could not convert to ContextString"); }