void WriteWithoutExtract(TextWriter writer, int indent) { CodeGenHelper.WriteIndent(writer, indent); writer.Write("static "); writer.Write(_sizeStrategy.Keyword); writer.Write("[] Expand("); writer.Write(_sizeStrategy.Keyword); writer.WriteLine("[] collapsed)"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("int i = 0;"); WriteCore(writer, indent + 1, "collapsed[i++]", "i < collapsed.Length"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine('}'); }
void WriteWithExtract(TextWriter writer, int indent) { CodeGenHelper.WriteIndent(writer, indent); writer.Write("static "); writer.Write(_sizeStrategy.Keyword); writer.WriteLine("[] Expand(string resourceName)"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("using (System.IO.BinaryReader reader = new System.IO.BinaryReader(stream))"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("{"); WriteCore(writer, indent + 2, "reader." + _sizeStrategy.BinaryReaderMember + "()", "stream.Position < stream.Length"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("}"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine('}'); }
void WriteWithoutExtract(TextWriter writer, int indent) { const string readLine = "tmp = collapsed[read++];"; const string notDoneCheck = "read < collapsed.Length"; CodeGenHelper.WriteIndent(writer, indent); writer.Write("static "); writer.Write(_sizeStrategy.Keyword); writer.WriteLine("[] Expand(byte[] collapsed)"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("int read = 0;"); writer.WriteLine(); WriteCore(writer, indent + 1, readLine, notDoneCheck); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("return result;"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("}"); }
void WriteWithExtract(TextWriter writer, int indent) { const string readLine = "tmp = unchecked((byte)stream.ReadByte());"; const string notDoneCheck = "stream.Position < stream.Length"; CodeGenHelper.WriteIndent(writer, indent); writer.Write("static "); writer.Write(_sizeStrategy.Keyword); writer.WriteLine("[] Expand(string resourceName)"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("{"); WriteCore(writer, indent + 2, readLine, notDoneCheck); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("return result;"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("}"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("}"); }
void WriteCore(TextWriter writer, int indent, string readValue, string notDoneCheck) { CodeGenHelper.WriteIndent(writer, indent); writer.Write(_sizeStrategy.Keyword); writer.Write("[] result = new "); writer.Write(_sizeStrategy.Keyword); writer.Write('['); writer.Write(readValue); writer.WriteLine("];"); CodeGenHelper.WriteIndent(writer, indent); writer.Write(_sizeStrategy.Keyword); writer.Write(" escape = "); writer.Write(readValue); writer.WriteLine(";"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("int w = 0;"); CodeGenHelper.WriteIndent(writer, indent); writer.Write("while ("); writer.Write(notDoneCheck); writer.WriteLine(")"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 1); writer.Write(_sizeStrategy.Keyword); writer.Write(" value = "); writer.Write(readValue); writer.WriteLine(";"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("if (value == escape)"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 2); writer.Write(_sizeStrategy.Keyword); writer.Write(" count = "); writer.Write(readValue); writer.WriteLine(";"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.Write("value = "); writer.Write(readValue); writer.WriteLine(";"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while (count > 0)"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("result[w++] = value;"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("count--;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine('}'); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine('}'); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("else"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("result[w++] = value;"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine('}'); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine('}'); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("return result;"); }
void WriteCore(TextWriter writer, int indent, string readLine, string notDoneCheck) { CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("const byte FOLLOW = 0x80;"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("const byte REPEAT = 0x40;"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("const byte FIRSTBODY = 0x3F;"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("const byte SUBBODY = 0x7F;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("int value;"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("byte tmp;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("value = tmp & FIRSTBODY;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("while ((tmp & FOLLOW) != 0)"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("value = (value << 7) | (tmp & SUBBODY);"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("}"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("int write = 0;"); CodeGenHelper.WriteIndent(writer, indent); writer.Write(_sizeStrategy.Keyword); writer.Write("[] result = new "); writer.Write(_sizeStrategy.Keyword); writer.WriteLine("[value];"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent); writer.Write("while ("); writer.Write(notDoneCheck); writer.WriteLine(")"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("if ((tmp & REPEAT) == 0)"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("value = tmp & FIRSTBODY;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while ((tmp & FOLLOW) != 0)"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("value = (value << 7) | (tmp & SUBBODY);"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("}"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.Write("result[write++] = unchecked(("); writer.Write(_sizeStrategy.Keyword); writer.WriteLine(")value);"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("}"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("else"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("int count = tmp & FIRSTBODY;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while ((tmp & FOLLOW) != 0)"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("count = (count << 7) | (tmp & SUBBODY);"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("}"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("value = tmp & FIRSTBODY;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while ((tmp & FOLLOW) != 0)"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine(readLine); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("value = (value << 7) | (tmp & SUBBODY);"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("}"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while (count > 0)"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.Write("result[write++] = unchecked(("); writer.Write(_sizeStrategy.Keyword); writer.WriteLine(")value);"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("count--;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("}"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("}"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("}"); }
public override void Write(TextWriter writer, int indent) { CodeGenHelper.WriteIndent(writer, indent); writer.Write("static "); writer.Write(_sizeStrategy.Keyword); writer.WriteLine("[] Extract(string resourceName)"); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine("using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))"); CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine('{'); if (_sizeStrategy == U8SizeStrategy.Instance) { CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("byte[] result = new byte[stream.Length];"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("int offset = 0;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("int read;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("do"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine('{'); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("read = stream.Read(result, offset, result.Length - offset);"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("offset += read;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine('}'); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while (offset < result.Length && read > 0);"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("return result;"); } else { CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("int len = (int)stream.Length;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.Write(_sizeStrategy.Keyword); writer.Write("[] result = new "); writer.Write(_sizeStrategy.Keyword); writer.Write("[len >> "); writer.Write(MeasureShift(_sizeStrategy.Size(1))); writer.WriteLine("];"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("byte[] buffer = new byte[Math.Min(stream.Length, 512)];"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("int offset = 0;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("int read;"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("do"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("{"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("read = stream.Read(buffer, 0, buffer.Length);"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("Buffer.BlockCopy(buffer, 0, result, offset, read);"); CodeGenHelper.WriteIndent(writer, indent + 3); writer.WriteLine("offset += read;"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("}"); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("while (offset < len && read > 0);"); writer.WriteLine(); CodeGenHelper.WriteIndent(writer, indent + 2); writer.WriteLine("return result;"); } CodeGenHelper.WriteIndent(writer, indent + 1); writer.WriteLine('}'); CodeGenHelper.WriteIndent(writer, indent); writer.WriteLine("}"); }