/// <summary> /// Reads an automaton from a byte stream. /// </summary> /// <param name="stream">The stream to read.</param> internal CFSA2(Stream stream) { using (DataInputStream input = new DataInputStream(stream, true)) { // Read flags. ushort flagBits = (ushort)input.ReadInt16(); flags = 0; flags = (FSAFlags)flagBits; if (flagBits != (ushort)flags) { throw new IOException($"Unrecognized flags: 0x{((int)flagBits).ToHexString()}"); } this.hasNumbers = (flags & FSAFlags.Numbers) != 0; /* * Read mapping dictionary. */ int labelMappingSize = input.ReadByte() & 0xff; LabelMapping = new byte[labelMappingSize]; input.ReadFully(LabelMapping); /* * Read arcs' data. */ Arcs = ReadRemaining(input); } }
/// <summary> /// Creates a new automaton, reading it from a file in FSA format, version 5. /// </summary> internal CFSA(Stream stream) { using (DataInputStream input = new DataInputStream(stream, true)) { // Skip legacy header fields. input.ReadByte(); // filler input.ReadByte(); // annotation byte hgtl = (byte)input.ReadByte(); /* * Determine if the automaton was compiled with NUMBERS. If so, modify * ctl and goto fields accordingly. */ flags = FSAFlags.Flexible | FSAFlags.StopBit | FSAFlags.NextBit; if ((hgtl & 0xf0) != 0) { this.NodeDataLength = (hgtl.TripleShift(4)) & 0x0f; this.GoToLength = hgtl & 0x0f; flags |= FSAFlags.Numbers; } else { this.NodeDataLength = 0; this.GoToLength = hgtl & 0x0f; } /* * Read mapping dictionary. */ LabelMapping = new byte[1 << 5]; input.ReadFully(LabelMapping); /* * Read arcs' data. */ Arcs = ReadRemaining(input); } }
/// <summary> /// Read and wrap a binary automaton in FSA version 5. /// </summary> internal FSA5(Stream stream) { DataInputStream input = new DataInputStream(stream); this.Filler = (byte)input.ReadByte(); this.Annotation = (byte)input.ReadByte(); byte hgtl = (byte)input.ReadByte(); /* * Determine if the automaton was compiled with NUMBERS. If so, modify * ctl and goto fields accordingly. */ flags = FSAFlags.Flexible | FSAFlags.StopBit | FSAFlags.NextBit; if ((hgtl & 0xf0) != 0) { flags |= FSAFlags.Numbers; } this.NodeDataLength = (hgtl.TripleShift(4)) & 0x0f; this.GoToLength = hgtl & 0x0f; Arcs = ReadRemaining(input); }