private string ReadCodedStringInternal(BinaryStream stream, int?maxLength)
        {
            StringBuilder sb         = new StringBuilder();
            int           count      = 0;
            bool          saturnMode = false;

            for (count = 0; (!maxLength.HasValue) ||
                 (maxLength.HasValue && count < maxLength);)
            {
                ContextString contextString;
                ControlCode   code = ProcessChar(stream, sb, saturnMode, ref count, out contextString);

                if (code != null)
                {
                    if (code.Flags.HasFlag(ControlCodeFlags.AlternateFont))
                    {
                        saturnMode = !saturnMode;
                    }

                    if (code.Flags.HasFlag(ControlCodeFlags.Terminate))
                    {
                        break;
                    }
                }
            }

            // Advance the position past the end of the string if applicable
            while (maxLength.HasValue && count++ < maxLength)
            {
                stream.ReadShort();
            }

            return(sb.ToString());
        }
Esempio n. 2
0
        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);
        }