Using Structures for Application Files?

Just starting out? Need help? Post your questions and find answers here.
berklee
User
User
Posts: 36
Joined: Wed Jul 28, 2004 3:45 pm

Using Structures for Application Files?

Post by berklee »

Hi, this may seem silly, but after 4 years of programming (in VB, so I guess the proper term would be 'scripting' :wink: ), I realized that I've never created my own application file format, instead always opting to use two-dimensional XML or something equally huge.

Since XML doesn't seem to be well-managed in PB just yet (without getting into MSXML or something else), I'd like to investigate using multiple structures to write out a project document. The help file seems to hint at it.

Can anyone offer any examples? I particularly interested in how one goes about 'knowing' which structure type is to be loaded next..
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

The best thing to do is look at other binary formats and see how they handle it all.

The Java Class file format is a good example:
http://java.sun.com/docs/books/vmspec/2 ... e.doc.html

Lots of examples formats can be found here:
http://www.wotsit.org/

Examples always use C, but converting these into PB is easy.
berklee
User
User
Posts: 36
Joined: Wed Jul 28, 2004 3:45 pm

Post by berklee »

Thanks for those.

I have no problem with assembling a file format in general.... my concern is more the 'how do I load stuff from my file format into my pre-defined structures'?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi berklee.

I am not sure that I understand what you are asking, so this may be a pointless post.

To write and read info from file into a structure you would use:

Code: Select all

WriteData( @Struct, SizeOf(struct) )
  ; later ...
ReadData( @struct, SizeOf(struct) )
Note: If you are writing/reading structures with string elements, this is a problem as the structure contains a pointer to the string and therefore when later read in it will probably be an invalid pointer or memory address.

If you are writing and later reading XML or any markup-type text, you can just use strings to write/read.

Code: Select all

WriteStringN( strVar )
  ; later ...
ReadString( strVar )
And handle your own tag management (openers/closers)

If you are creating your own record definitions and access, you also create your own rules, so reading and writing are done your way.

If your requirements are simple, eg your records are fixed length and files are small and not updated just written from scratch, this fairly straightforward. Otherwise it gets complex with variable length records and free-space management, etc.

With this you would have, say, a vli in the first word or long of each record, defining the size of the record, perhaps followed by a type indicator to define what fields followed, and perhaps even vli for each field (eg string). You would need to read each of these, and each would determine action taken for rest of record.

If you are doing sequential access, ok, if not you have to create your own ISAM or DB, something I personally feel is non-trivial - you might be better off using a DB like mySQL.

If the above is useless to you, sorry, but at least my post-count went up. :)
@}--`--,-- A rose by any other name ..
berklee
User
User
Posts: 36
Joined: Wed Jul 28, 2004 3:45 pm

Post by berklee »

actually, I believe that code snippet you did there is just about perfect.

Thanks, man. That's what I believe I needed to know!
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Glad to hear that. :)

Also, sorry but I lied with the ReadString( strVar) - it should be strVar = ReadString() - but you knew that.
@}--`--,-- A rose by any other name ..
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

I never knew you could do that with pb structures... thanks! At least it works for nonstrings :D
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi Shannara. :)

For records holding fixed-length strings this sort of approach may help:

Code: Select all

Structure notMuch
  myWord.w
  fixStr.b[8]
  soLong.l
EndStructure

blockOut.notMuch
blockIn.notMuch

wrk.s = "ABCDEFGHijklmnopqrstuvwxyz"

blockOut\myWord=32767
CopyMemory(@wrk, @blockOut + OffsetOf(notMuch, fixStr), 8)
blockOut\soLong=-1
Debug PeekS(@blockOut + OffsetOf(notMuch, fixStr), 8)
Debug "----------------------"

fIDout = OpenFile(#PB_Any, "JUNK")
UseFile(fIDout)
WriteData(@blockOut, SizeOf(notMuch))
CloseFile(fIDout)

fIDin = OpenFile(#PB_Any, "JUNK")
UseFile(fIDin)
ReadData(@blockIn, SizeOf(notMuch))
Debug blockIn\myWord
Debug PeekS(@blockIn + OffsetOf(notMuch, fixStr), 8)
Debug blockIn\soLong
CloseFile(fIDin)
Aside to Fred - If you could see your way clear to providing fixed length strings that would be really great - and also strings capable of holding binary zero so numerics could be packed in (eg, VB-like MKI$ and CVI.)
@}--`--,-- A rose by any other name ..
Post Reply