Created
November 9, 2015 11:11
-
-
Save 0xF6/208487f7f9711007fbd2 to your computer and use it in GitHub Desktop.
Manipulate a pointer to a stream.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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