示例#1
0
        public static void Pack(BinaryReader input, BinaryWriter output)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            CgtReader reader      = new CgtReader(input);
            CgtWriter writer      = new CgtWriter(output);
            string    header      = reader.ReadHeaderString();
            Match     headerMatch = FileHeader.Match(header);

            if (headerMatch.Groups["version"].Value != "1.0")
            {
                throw new FileLoadException("The File Header is invalid or of an unsupported version for packing, only uncompressed V1.0 Compiled Grammar Files are supported");
            }
            writer.WriteHeaderString(header);
            while (reader.HasMoreData())
            {
                CgtRecordType recordType = reader.ReadNextRecord();
                switch (recordType)
                {
                case CgtRecordType.Charsets:
                    int        index   = reader.ReadIntegerEntry();
                    string     chars   = reader.ReadStringEntry();
                    DfaCharset charset = new DfaCharset(null, index, chars);
                    if (charset.SequenceLength >= 4)
                    {
                        writer.WriteNextRecord(CgtRecordType.PackedCharsets, 4);
                        writer.WriteIntegerEntry(index);
                        writer.WriteIntegerEntry(charset.SequenceStart);
                        writer.WriteIntegerEntry(charset.SequenceEnd);
                        writer.WriteStringEntry(new string(charset.CharactersExcludingSequence));
                    }
                    else
                    {
                        writer.WriteNextRecord(CgtRecordType.Charsets, 2);
                        writer.WriteIntegerEntry(index);
                        writer.WriteStringEntry(chars);
                    }
                    break;

                default:
                    writer.WriteNextRecord(recordType, reader.EntryCount);
                    while (reader.EntryCount > 0)
                    {
                        reader.CopyEntry(writer);
                    }
                    break;
                }
            }
            output.Flush();
        }
示例#2
0
		public static void Pack(BinaryReader input, BinaryWriter output) {
			if (input == null) {
				throw new ArgumentNullException("input");
			}
			if (output == null) {
				throw new ArgumentNullException("output");
			}
			CgtReader reader = new CgtReader(input);
			CgtWriter writer = new CgtWriter(output);
			string header = reader.ReadHeaderString();
			Match headerMatch = FileHeader.Match(header);
			if (headerMatch.Groups["version"].Value != "1.0") {
				throw new FileLoadException("The File Header is invalid or of an unsupported version for packing, only uncompressed V1.0 Compiled Grammar Files are supported");
			}
			writer.WriteHeaderString(header);
			while (reader.HasMoreData()) {
				CgtRecordType recordType = reader.ReadNextRecord();
				switch (recordType) {
				case CgtRecordType.Charsets:
					int index = reader.ReadIntegerEntry();
					string chars = reader.ReadStringEntry();
					DfaCharset charset = new DfaCharset(null, index, chars);
					if (charset.SequenceLength >= 4) {
						writer.WriteNextRecord(CgtRecordType.PackedCharsets, 4);
						writer.WriteIntegerEntry(index);
						writer.WriteIntegerEntry(charset.SequenceStart);
						writer.WriteIntegerEntry(charset.SequenceEnd);
						writer.WriteStringEntry(new string(charset.CharactersExcludingSequence));
					} else {
						writer.WriteNextRecord(CgtRecordType.Charsets, 2);
						writer.WriteIntegerEntry(index);
						writer.WriteStringEntry(chars);
					}
					break;
				default:
					writer.WriteNextRecord(recordType, reader.EntryCount);
					while (reader.EntryCount > 0) {
						reader.CopyEntry(writer);
					}
					break;
				}
			}
			output.Flush();
		}