Skip to content

Instantly share code, notes, and snippets.

@0xF6
Created November 9, 2015 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xF6/208487f7f9711007fbd2 to your computer and use it in GitHub Desktop.
Save 0xF6/208487f7f9711007fbd2 to your computer and use it in GitHub Desktop.
Manipulate a pointer to a stream.
public struct StreamPoint
{
private Stream stream;
private long offset;
public StreamPoint(Stream s) : this(s, s.Position) { }
public StreamPoint(Stream s, long pos)
{
if (s == null)
throw new ArgumentNullException("stream");
stream = s;
offset = pos;
}
public static StreamPoint operator +(StreamPoint sp, long plus) { return new StreamPoint(sp.stream, sp.offset + plus); }
public static StreamPoint operator -(StreamPoint sp, long plus) { return new StreamPoint(sp.stream, sp.offset - plus); }
public static long operator -(StreamPoint sp, StreamPoint sp2)
{
if (sp.stream != sp2.stream)
throw new ArgumentException();
return sp.offset - sp2.offset;
}
public static explicit operator Stream(StreamPtr ptr)
{
ptr.stream.Position = ptr.offset;
return ptr.stream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment