Memory file utility -oop class

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Memory file utility -oop class

Post by srod »

Hi,

in the midst of an application requiring to effectively create and utilise a binary file in memory, and wishing to avoid Peek()'s and Poke()'s etc, I created this little utility which wraps the whole job lot up in a convenient oop class.

Basically the class allows you to treat a dynamic chunk of memory like a random access file offering 'read' and 'write' operations without the hassle of continually reallocating memory (or allocating vastly more memory than is required) and utilising a 'file-pointer' so that we do not need to keep manually advancing a pointer every time we write some data etc.

With this class I don't have to worry about continually reallocating memory as I write more and more data as that is all handled by the class itself. I no longer have to keep a track of the 'read/write' position either as again that is all sorted out 'behind the scenes'.

Features :
  • Cross platform
  • Mimicks the PB file library, but with a memory-based file
  • Allocated memory 'grows' as required
  • Read/write bytes, word, longs, floats,... etc.
  • Error checking for individual 'write' operations or, more conveniently, after a 'batch' of write operations. A failed 'write' will not cause a crash, thus allowing you to perform a single error check after 'writing' a large amount of data
  • Read / write strings (in any PB supported format)
  • Easy access to the underlying memory buffer for use with regular PB commands etc.
There are a couple of things to note :
  1. Whilst you can request automatic 'mutex protection' if a single memory file is to be utilised by multiple threads, the use of an internal 'file-pointer' makes such access potentially damaging (similar to a linked list) as the file-pointer is a 'shared resource'. My advice in such circumstances would be for each thread to use it's own 'local' memory pointer for read operations.
  2. Strings are written with the appropriate null terminator (depending on the format; utf-8, unicode etc.)
    They are not terminated with CR/LF characters.
  3. This is not the same as a 'memory mapped file'!
The download contains 3 files : the main source file, a residents file (which contains a description of each method exposed by the class where appropriate) and a demo program.

Download


Regards.
I may look like a mule, but I'm not a complete ass.
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

seems interesting
Another "well done srod" I guess
I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

What we need is a streams library for PB, an official one. Perhaps if Fred cares to read about them :? ... There is nothing more "pure" than having streams in a language, means you don't have to waste time writing around limitations of the existent libraries but rather, use an one-for-all system that allows you to manage this sort of things so you can focus on the task at hand.

:lol: should I bash the keyboard and give up?
:?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I don't see any particular advantage with streams aside from abstracting various 'systems'. I've encountered them in Delphi and VB but was never that impressed with them, certainly not enough to think that PB is lacking in some way by not having them.

And when some kind of 'memory streaming' is required, for example, well, as I've shown here, it doesn't take long to knock up some code. :)
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

WoW! I was just thinking about something like this for another little project i have in mind, how timely.

Thank You srod :)
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Post by Poshu »

Thx Srod, as always, your code become handy when I'm in need.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Glad it's useful. As always, I wrote it out of need! 8)
I may look like a mule, but I'm not a complete ass.
Post Reply