Page 1 of 1

Memory file utility -oop class

Posted: Sat Aug 02, 2008 3:37 pm
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.

Posted: Wed Aug 06, 2008 2:40 pm
by kinglestat
seems interesting
Another "well done srod" I guess

Posted: Wed Aug 06, 2008 3:12 pm
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.

Posted: Wed Aug 06, 2008 5:13 pm
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. :)

Posted: Wed Aug 06, 2008 6:49 pm
by SFSxOI
WoW! I was just thinking about something like this for another little project i have in mind, how timely.

Thank You srod :)

Posted: Thu Aug 07, 2008 8:30 pm
by Poshu
Thx Srod, as always, your code become handy when I'm in need.

Posted: Thu Aug 07, 2008 9:35 pm
by srod
Glad it's useful. As always, I wrote it out of need! 8)