Re: Fastest way to read and set variables from a prefs file?
Re: Fastest way to read and set variables from a prefs file?
- Subject: Re: Fastest way to read and set variables from a prefs file?
- From: has <email@hidden>
- Date: Tue, 28 May 2002 18:38:37 +0100
Jason Bourque wrote:
>
Anyone have a fast way of reading and setting variables from a text prefs
>
file?
Depends on the what sort of stuff is in the text file, whether you're
reading all or only part of it, what you want to do with the data, and
whether or not the file is meant to be human-readable/editable. It won't be
as fast as getting data from a fixed-structure or a binary file, since
there's going to be some parsing involved (whether it's label-value pairs,
XML, or whatever). If speed is the top requirement then you might want to
consider that (though it does mean trading off the flexibility you get from
a labelled value format).
>
Things to consider.
>
>
TIDS - Might be the fastest if file is deliminated with a unique string.
If everything is value-pair stuff then it could help.
>
Could multiple projects be in the same preference file?
I'm sure they could. You'd need a way of keeping each project's settings
separate, of course.
>
I currently read the text file and repeat thru each line matching it up to a
>
variable string and then setting the related variable. Each one uses an
>
offset command taking even more time.
Assuming that flexibility [plain text label-values] is more important than
speed [fixed-structure/binary file], you want to make the parsing as fast
and efficient as possible. If case-sensitivity isn't a problem then TIDs
are a best bet (since you're already using 'offset' to get labels then I'd
assume this is a safe bet, and using pure vanilla code should be faster).
[Note: both these solutions are rough code; they lack code for error
handling, resetting of tids, proper structure, etc.]
1. you could use TIDs to match each label in turn, and pull out the value:
======================================================================
property moveWithReplace : missing value
property allowRenaming : missing value
set prefsLabels to {"
Move with replacing ~ ", "
Allow renaming ~ "}
set variableRefs to {a reference to moveWithReplace, a reference to
[NO-BREAK]allowRenaming}
repeat with x from 1 to count prefsLabels
set AppleScript's text item delimiters to (get prefsLabels's item
[NO-BREAK]x)
set prefsValue to prefsText's second text item's first paragraph
set variableRefs's item x's contents to prefsValue
end repeat
======================================================================
Note that this solution expects each label to be preceded by a return. This
includes the first label-value pair in the file, which should begin on line
2 or later. (Matching a label from one unique delimiter [the start of a new
line] to another unique delimiter [" ~ "] avoids the risk of accidental
mis-matches.)
2. You can keep with your current approach of iterating through each line
of the prefs file, and use TIDs and a lookup string to calculate which item
in variableRefs to shove the resulting value into:
======================================================================
property moveWithReplace : missing value
property allowRenaming : missing value
(*
note: each label should be padded to the same length [padding shown as
periods for clarity], in this case 40 characters. This allows quick
calculation of variableRefs list offsets.
*)
set prefsLabels to "Move with replacing.....................Allow
[NO-BREAK]renaming.........................."
set variableRefs to {a reference to moveWithReplace, a reference to
[NO-BREAK]allowRenaming}
set prefsText to "Allow renaming ~ yes"
repeat with x from 1 to count prefsText's paragraphs
set AppleScript's text item delimiters to " ~ "
set theLabel to prefsText's paragraph x's first text item
set theValue to prefsText's paragraph x's last text item
set AppleScript's text item delimiters to theLabel
set listIndex to (prefsLabels's first text item's length) / 40 + 1
set variableRefs's item listIndex's contents to theValue
end repeat
======================================================================
Plenty other possible approaches, and I've not even covered the options
using a fixed-structure file. But hope that helps a bit. (Sounds like
there's a good opportunity for somebody to write some decent Preferences
libraries too.)
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.