System.IO Extension Source

Post here basically any programming material that will help other programmers out.
Post Reply
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

System.IO Extension Source

Post by XZodia »

This is a little something I whipped up, since I've never been satisfied with existing forms of reading/writing data to a file...

The main class in this library is the DataStream which is a wrapper which combines the binary reader and binary writer with a few extra features:
  • Bookmark Stack -
    • IMO this is one of the best ideas I've ever come up with, its also very simple. If you are at a point in a file but you want to go to another part of the file and then return (eg for a reflexive), simply ".PushBookmark" to add a return point and ".PopBookmark" to return. An understanding of the stack structure is good to understand how useful this is.
    3 Different String Read/Write Methods -
    • When reading or writing a string you can specify how to do so. There are 3 options, Characters Only (When reading this requires the user to specify the character count), Null Terminated and System IO Standard.
      Reverse bool: If true the string is automatically reversed after read / before write.
    Memory Stream Read/Write -
    • Just a simple wrapper that returns a MemoryStream instead of a byte array.
    Flags Read/Write - Ability to read/write the included Flags class.
    IDatatizable Read/Write - Ability to read/write any user defined class/struct via the included IDatatizable interface.
    Extendible Design - The Binary Reader and Binary Writer are exposed to inherited class to allow you to easily add your own read/write functions.
The IDatatizable interface adds a Write(DataStream) void to your class/struct where you can write whatever info you like to write your class. To read your class/struct from a DataStream add a constructor with just a DataStream variable and read whatever info you like.

The Flags class is an abstact class with 4 ready made inherited classes (8,16,32,64 bits). To see how to expand on these look at the source. The Flags class uses a compact algorithm which I came up with to read/write bits, people who hate messy code should like it :P

Bare in mind I haven't tested any of this, so if you find any bugs let me know (its possible the flags write in reverse for halo standards but its easy to fix if this is the case)
Attachments
System.IO.zip
(45.88 KiB) Downloaded 445 times
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: System.IO Extension Source

Post by OwnZ joO »

The bookmark stack seems like a pretty good idea.
Post Reply