[futurebasic] Re: [FB] Note storage examples by Jay

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 2004 : Group Archive : Group : All Groups

From: Jay Reeve <jayreeve@...>
Date: Sun, 8 Aug 2004 01:58:16 -0500
Brian,

Sorry to leave you hanging. I've been away for a week, and just got 
back. You may have already figured out all of this, but here are 
answers to your questions.

On Sunday, August 1, 2004, at 11:36  AM, Brian Stevens wrote:

> Jay,
>
> With the weekend comes some free time to code and consider how to use 
> these snippets for my specific use. After looking at your example FNs, 
> I have a few questions that I'm hoping you will address. Hopefully, 
> these questions aren't too boring for you or the list.
>
> 1. The reference manual warns against using a dynamic array to store 
> handles (second page for DYNAMIC in my copy of ref man). Since the 
> dynamic array is, in fact, a handle that is sized based on usage, the 
> warning seems to have some validity. I have not looked at the dynamic 
> subs include in the headers in a long time, so I suppose there might 
> have been changes. Your take?
The caveat about dynamic handle arrays is simply that you cannot treat 
the array element directly as a handle:
myArrayH..myfield(index)

For simply storing the handles to use in another context there is 
absolutely no danger--providing of course that you properly dispose of 
them before trashing the array.

> 2. FN StoreNote:   Need a quick clarification on the operation here. 
> Is this intended to be used with new notes and existing notes or just 
> new notes?
Should work for both.

>  If this is called with a non-negative index it disposes the existing 
> handle, gets a new handle to the edit field and assuming a positive 
> result from gethandlesize stashes the handle in the array. Maybe I'm 
> answering my own question here but it seems this FN would be called 
> with a positive index whenever the note content changes?
It should have read <= 0. If your note field of a record has no entry, 
it will contain a 0, which means no index has yet been assigned, so a 
new index will automatically be created. Anytime you call FN StoreNote, 
you should use something like:

myRecordArray.noteID(elem) = fn StoreNote(myRecordArray.noteID(elem))

That way, you don't have to worry about whether a note already exists 
or not. It will be handled regardless.

> Also, if the FN is called with a negative index the disposeH seems 
> unnecessary.
Just a safety precaution, making sure we never abandon an active 
handle. disposeH is fairly fast if there is nothing to dispose of, so 
it's not a bad idea to make sure.

> 3. FN ClearNotes:  Assume KILL DYNAMIC could be substituted for the 
> final POKE in this FN?
You're right. Doing it my way just avoids having to reinitialize the 
array when you add the first note. It reuses the existing handle. Given 
that the existing handle could be unreasonably large, maybe KILL would 
be a better choice.

> 4. FN WriteNotes:  seems to make logical sense to do a FN ClearNotes 
> after the WHILE loop to dispose of handles. Maybe I missed something 
> here.
If you are finished with the notes after writing them, you can call FN 
ClearNotes, but if you're just doing a Save, there's no reason to throw 
them all away. If you want to clear them, just call FN ClearNotes after 
calling FN WriteNotes.

> 5. Seems to be an implicit assumption that the index values are 
> tracked elsewhere by the program in order to pass the index that 
> matches the note (I'm assuming multiple notes ---in fact hundreds---). 
> Am I overlooking something here?
Yes, I assumed that you would have a record structure that would 
include a NoteID field. Any time that record (or that note) is 
accessed, you would call edit$(_noteField) = 
&myRecordArray.NoteID(index). If you need more than one note per 
record, you would have to store multiple indices, or use some sort of 
linked list.

> The other challenge is where to store(memory not disk)  the 
> identifying information for each note (i.e. date and a flag setting). 
> Currently, I stash this in the first 9 bytes of the note (not shown to 
> user). The date is also used to sort the notes chronologically. 
> Somehow adding a second array just to track this "header" information 
> seems inefficient. Using my current method (i.e. embedding the 
> date/flag at the beginning of the note text) would work but I need to 
> learn how to remove/add/access this data via the handle before the 
> user sees it in the edit field.  Short code snippet showing how to do 
> the access would be welcome.
Your idea is a good one, but not very practical when using a ZTXT 
handle. What I would do is keep that info with the handle in a header 
record in the notesH array. Here again is the code I sent, updated to 
use this new structure:

hth,
  e-e
  =J= a  y
   "

'=======================
begin globals

begin record notesHdrRecord
dim H          as long ' ZTXT handle
dim created    as long ' Created secs
dim modified   as long ' Modified secs
dim flag       as byte
end record
_dateFlagSz = sizeof(notesHdrRecord) - offsetof(created in 
notesHdrRecord)

dynamic notes(_maxLong) as notesHdrRecord
end globals

local fn newNoteIndex
' Find first unused noteH
dim index&
index = 1' Leave 0 empty for No Note
while notes.H(index) ' Search for first zero entry
index ++
wend
end fn = index

local fn storeNote(index&)
dim @note&,t&
' Saves handle from edit field _noteField into noteH(index)
' If index <= 0, chooses and returns new index
' if EF is empty, clears noteH(index) and returns 0
getDateTime(t)
long if index < 0
index = fn newNoteIndex
notes.created(index) = t ' New note, so set creation date
end if
notes.modified(index) = t ' Set modification date
def disposeH(notes.H(index))' Discard old handle
get field note, _noteField ' Get new ZTXT handle
long if fn gethandlesize(note)' See if any text
notes.H(index) = note ' Put note into array
xelse' No text...
index = 0 ' Return _NIL
end if
end fn = index

local fn clearNotes
' Dispose of all handles in noteH array
dim count&
count = [@notes + _autoXREFCurr]
while count
count --
def disposeh(notes.H(count))
wend
& @notes + _autoXREFCurr, 0
end fn

local fn writeNotes(dev%)
' Writes note array to file dev.
' File dev must be opened for writing before calling Fn
'   and closed after return.
dim index&, @count&
count = [@notes + _autoXREFCurr]
write dev, count
while count
count --
' Write dates and flags
write file dev, @notes.created(count), _dateFlagSz
' Write note text
write field dev, notes.H(count)
wend
end fn

local fn readNotes(dev%)
' Reads note array from file dev.
' File dev must be opened for reading before calling Fn
'   and closed after return.
dim index&, @count
fn clearNotes
read dev, count
while count
count --
' Read dates and flags
read file dev, @notes.created(count), _dateFlagSz
' Read note text
read field dev, notes.H(count)
wend
end fn

'=======================

--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@...