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.
- 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.
- Strings are written with the appropriate null terminator (depending on the format; utf-8, unicode etc.)
They are not terminated with CR/LF characters. - This is not the same as a 'memory mapped file'!
Download
Regards.