I am attempting to build a DATA resource ID'ed 1001 to contain an
array for a spell checking project I've been working on. Somewhere I
am going wrong and I can't write the array to the resource.
Here is a brief overview of the code:
Using OS X's built-in dictionary words, I create an array which
includes the four-character Soundex identifier for each word in the
dictionary immediately followed by the word. After building the
array, I can test it and it is being successfully built.
To handle the process, I build a small application-- Array Builder--
which contains the DATA 1001 resource. But no matter how hard I try,
after I run the following code and examining the DATA 100 resource in
Array Builder, the resource remains empty.
Does anyone have a clue what I am doing wrong?
The following code is self-running and assumes you have in the folder
with the code a resource named "myArrayResource.rsrc" which contains
one empty _"DATA" resource with an ID of 1001.
I have now hit the wall of frustration.
Any suggestions are welcome.
Ken
Please watch for lost underscores and e-mail line breaks.
#if ( CarbonLib == 0 ) or ( ndef _appearanceRuntime )
compile shutdown "Must be compiled as Appearance Compliant with Carbon"
#endif
dim system 20000000
// Contains one empty _"DATA" resource ID 1001
resources "myArrayResource.rsrc"
output file "Array Builder"
DEF FN UBound(@P AS PTR) = [P + _AutoXREFCurr] - 1
begin globals
dim dynamic dictStrArray( _maxLong ) as str63
dim as integer gResRef
dim as long resElements
end globals
/*
Initialize gResRef
This is equal to the application's resource ref
*/
gResRef = SYSTEM(_aplRes)
local fn BuildWindow
dim as rect r
setrect( r, 0, 0, 400, 250)
appearance Window -1, "", @r,¬
_kDocumentWindowClass, _kWindowStandardFloatingAttributes
def SetWindowBackground( _kThemeActiveDialogBackgroundBrush,_zTrue)
setrect( r, 20, 20, 364, 190)
edit field -10,"",@r,_statframed,_leftJust
setrect( r, 364, 20, 380, 190)
scroll button -10,0,0,0,0, @r, _scrollOther
setrect( r, 260, 210, 380, 230)
button 1,1,"Build array",@r,_shadow
window 1
end fn
LOCAL MODE
_soundexNil$ = "0000"
LOCAL FN Soundex$( codeWord AS STR255 )
DIM AS LONG i,u
DIM AS UNSIGNED CHAR charCode,lastCode
DIM AS STR31 outputStr
outputStr = _soundexNil$
IF codeWord[0] = _nil THEN EXIT FN
UppercaseStripDiacritics(@codeWord[1],¬
codeWord[0],_smCurrentScript)
outputStr[1] = codeWord[1]
charCode = outputStr[1] : GOSUB "getSoundexCode"
lastCode = charCode
i = 1 : u = 1
WHILE i <= codeWord[0]
i++ : charCode = codeWord[i] : GOSUB "getSoundexCode"
LONG IF charCode > 0 AND lastCode <> charCode
u++ : outputStr[u] = charCode
IF u = 4 THEN EXIT WHILE
END IF
lastCode = charCode
WEND
EXIT FN
"getSoundexCode"
SELECT charCode
CASE _"B", _"F", _"P", _"V"
charCode = _"1"
CASE _"C", _"G", _"J", _"K", _"Q", _"S", _"X", _"Z"
charCode = _"2"
CASE _"D", _"T"
charCode = _"3"
CASE _"L"
charCode = _"4"
CASE _"M", _"N"
charCode = _"5"
CASE _"R"
charCode = _"6"
CASE ELSE
charCode = 0
END SELECT
RETURN
END FN = outputStr
local fn BuildDictionaryArray
dim as str255 wStr, soundexStr
dim as integer i
wStr = "cat " + " /usr/share/dict/words"
i = 0
OPEN "UNIX", 2, wStr
DO
LINE INPUT #2, wStr
soundexStr = fn Soundex$( wStr)
dictStrArray(i) = soundexStr + wStr
i++
UNTIL EOF (2)
CLOSE 2
compress dynamic dictStrArray
end fn
/*
This takes data from an array and saves it as a resource.
@firstRecPtr&: First record to save
@lastRecPtr&: Last record to save + 1 <-- important!!
rType: save as this res type (your choice)
rID: save as this res ID (your choice)
rRef: what file do we put it in (I use gSaveRef)
EXAMPLE:
FN ary2Res( gArray(0), gArray(999), _"DATA", 1001, gRef )
*/
local fn ArrayToResource( @firstRecPtr&, @lastRecPtr&, rType&, rID, rRef )
dim as long size
dim as handle resH
size = lastRecPtr& - firstRecPtr&
resH = fn newhandle( size )
long if resH
BLOCKMOVE firstRecPtr&, [resH], size
resH = USR REPLACERESOURCE( resH, rType&, rID, "", rRef )
END IF
END FN
local fn BuildResourceArray
dim as integer i, arraySize
edit$(10, _maxInt, _maxInt ) = "Building Soundex Dictionary array..."¬
+ chr$(13) + "It takes several minutes to index 230,000-plus words"¬
+ chr$(13) + chr$(13)
fn BuildDictionaryArray
arraySize = fn UBound( dictStrArray )
edit$(10, _maxInt, _maxInt ) =¬
"The array has:" + str$(arraySize) + " elements." + chr$(13)¬
+ chr$(13) + "The first 50 elements are:" + chr$(13)
for i = 1 to 50
edit$(10, _maxInt, _maxInt ) =¬
dictStrArray(i) + chr$(13)
next i
edit$(10, _maxInt, _maxInt ) = chr$(13)¬
+ "The last 50 elements are:" + chr$(13)
for i = arraySize - 50 to arraySize
edit$(10, _maxInt, _maxInt ) = dictStrArray(i) + chr$(13)
next i
edit$(10, _maxInt, _maxInt ) = chr$(13)¬
+ "Building DATA resource array..." + chr$(13)
resElements = fn UBound( dictStrArray )
fn ArrayToResource( @dictStrArray(0),¬
@dictStrArray(resElements + 1), _"DATA", 1001, gResRef )
kill dynamic dictStrArray
edit$(10, _maxInt, _maxInt ) = chr$(13)¬
+ "DATA array build complete." + chr$(13)¬
+ chr$(13) + "Close window or Quit to end."¬
+ chr$(13) + chr$(13) + chr$(13) + chr$(13)
end fn
local fn DoDialog
dim as long evnt, id
evnt = dialog(0)
id = dialog(evnt)
select case( evnt )
case _wndClose
select( id )
case 1 : gFBQuit = _zTrue
end select
case _btnClick
select( id )
case 1 : fn BuildResourceArray
end select
end select
end fn
on dialog fn DoDialog
fn BuildWindow
do
handleevents
until gFBQuit
end