[futurebasic] Re: Writing to a text file

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2003 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Tue, 15 Jul 2003 23:48:14 -0400
Scott asked:

>Is anyone else getting an error when the program 
>writes to disk?  I'm getting an "Unknown File or 
>Folder" and then "File not Open" from the Subs 
>Compiler.incl file.
>
>Ken, what OS and FB3 version are you using?


Scott,

I'm using Jaguar 10.2.6 and the latest beta: FB^3 7.0b14.

A suggestion: If you're using code off the 
Associate server, double check to see if you have 
lost a constant underscore somewhere. I have 
learned that some servers are set to strip code 
of underscores to avoid hacker attacks. Also 
check  for e-mail line breaks. I amazes me that 
some code gets through intact, and underscores 
are stripped from others.

See if you have problems with this abbreviated 
example which I have rewritten from the earlier 
post to include Alain's suggestions:

Ken

p.s. Watch for e-mail line breaks and lost 
constant underscores on the Associate server.


/*

     Demo of FB^3 array buffering and printing

     By Ken Shmidheiser
     Somerset, KY
     7-13-03

*/

gFBUseNavServices = _zTrue

begin globals
dim as container gC
dim dynamic splitArray( _maxInt ) as str255
dim dynamic    myArray( _maxInt ) as str31
end globals


// Fill container with text for demo
gC  = "'Twas brillig, and the slithy toves" + chr$(13)
gC += "Did gyre and gimble in the wabe;" + chr$(13)
gC += "All mimsy were the borogoves," + chr$(13)
gC += "And the mome raths outgrabe." + chr$(13)
gC += "'Beware the Jabberwock, my son!" + chr$(13)
gC += "The jaws that bite, the claws that catch!" + chr$(13)
gC += "Beware the Jubjub bird, and shun" + chr$(13)
gC += "The frumious Bandersnatch!'" + chr$(13)
gC += "He took his vorpal sword in hand:" + chr$(13)
gC += "Long time the manxome foe he sought--" + chr$(13)
gC += "So rested he by the Tumtum tree," + chr$(13)
gC += "And stood awhile in thought." + chr$(13)
gC += "And as in uffish thought he stood," + chr$(13)
gC += "The Jabberwock, with eyes of flame," + chr$(13)
gC += "Came whiffling through the tulgey wood," + chr$(13)
gC += "And burbled as it came!" + chr$(13)
gC += "One, two!  One, two!  And through and through" + chr$(13)
gC += "The vorpal blade went snicker-snack!" + chr$(13)
gC += "He left it dead, and with its head" + chr$(13)
gC += "He went galumphing back." + chr$(13)
gC += "'And has thou slain the Jabberwock?" + chr$(13)
gC += "Come to my arms, my beamish boy!" + chr$(13)
gC += "O frabjous day!  Callooh!  Callay!'" + chr$(13)
gC += "He chortled in his joy." + chr$(13)
gC += "'Twas brillig, and the slithy toves" + chr$(13)
gC += "Did gyre and gimble in the wabe;" + chr$(13)
gC += "All mimsy were the borogoves," + chr$(13)
gC += "And the mome raths outgrabe.


local fn BuildWindow
dim as rect r

setrect( r, 0, 0, 350, 480)
appearance Window -1, "", @r,¬
_kDocumentWindowClass, _kWindowStandardFloatingAttributes

def SetWindowBackground( _kThemeActiveDialogBackgroundBrush,_zTrue)

edit = 4
setrect( r, 24, 24, 326, 426 )
edit field -1,"",@r,_framed,_leftJust
setrect( r, 330, 20, 346, 430 )
scroll button -1,0,0,0,0, @r, _scrollOther

setrect( r, 270, 440, 330, 460 )
button 2,1,"Quit",@r,_shadow

setrect( r, 110, 440, 260, 460 )
button 3,1,"Press to parse...",@r, push

def windowreposition( 1, 0, _kWindowAlertPositionOnMainScreen )

edit$( 1 ) = "JABBERWOCKY" + chr$(13)
edit$( 1, _maxInt, _maxInt ) = "By Lewis Carroll" + chr$(13) + chr$(13)
edit$( 1, _maxInt, _maxInt ) = #gC
edit$( 1, _maxInt, _maxInt ) =  + chr$(13) + chr$(13)+¬
"Click button to index Jabberwocky words..."

edit field 0

window 1

end fn

// This function splits individual words in a container into an array
local fn Split( @CPtr as ptr, splitChar as str15 )
dim as pointer startOfDataPtr, endOfDataPtr, itemPtr
dim as pointer p1, p2
dim as long    size, splits

if CPtr.nil& = _nil then exit fn

startOfDataPtr = [CPtr.nil&]
endOfDataPtr   = startOfDataPtr + Fn GetHandleSize(CPtr.nil&)
Long If endOfDataPtr > startOfDataPtr
splits = 0
splitArray(5000) = ""

for p1 = startOfDataPtr to endOfDataPtr
itemPtr = @splitArray(splits)
p2      = itemPtr
while p1.0`` != splitChar[1] and p1 < endOfDataPtr
p2++
p2.0`` = p1.0``
p1++
wend
size =  p2 - itemPtr
long if size < sizeof(splitArray(0))
itemPtr.0`` = size
xelse
itemPtr.0`` = sizeof(splitArray(0)) - 1
end if
splits++
next

compress dynamic splitArray
end if

end fn = splits


// This function is used to replace on string with another in a container
local fn Replace( @CPtr as ptr,¬
             oldSubStr as str255,¬
             newSubStr as str255 )
dim as long found

if oldSubStr[0] = _nil or CPtr.nil& = _nil then exit fn

found = fn Munger( CPtr.nil&, 0, @oldSubStr[1], ¬
oldSubStr[0], @newSubStr[1], newSubStr[0] )

end fn


// QuickSort function for strings in splitArray
local fn QuickSort( loIn as long, hiIn as long )
dim as long   lo, hi
dim as str255 mid, t

lo = loIn : hi = hiIn
long if( hiIn > loIn)
mid = splitArray( (loIn + hiIn) /2 )
while  ( lo <= hi  )
while (( lo < hiIn ) and ( splitArray(lo) < mid) )  : lo++ : wend
while (( hi > loIn ) and ( splitArray(hi) > mid) )  : hi-- : wend
if  ( lo <= hi  ) then swap splitArray(lo), splitArray(hi) : lo++ : hi--
wend
if (loIn <  hi) then fn QuickSort( loIn, hi )
if (lo <  hiIn) then fn QuickSort( lo, hiIn )
end If

end fn


local fn CreateWordIndex( lastElement as long )
dim as long    i, y, counter, increment
dim as str255  aStr, bStr, cStr, tabStr
dim as boolean start

aStr = "" : bStr = "" : cStr = ""
y = 0 : counter = 1

for i = 0 to lastElement

aStr = splitArray( i )
bStr = splitArray( i + 1 )

if aStr = "" then goto "next"

long if aStr = bStr
counter++
goto "next"
xelse
long if counter < 10
tabStr = chr$(9) + chr$(9)
xelse
tabStr = chr$(9)
end if
cStr = str$( counter ) + "x" + tabStr + splitArray( i )
counter = 1
end if

myArray(y) = cStr
y++

"next"

next i

kill dynamic splitArray
compress dynamic myArray

end fn = y


local fn ParseContainer
dim as handle repH
dim as int    count

repH = Fn NewHandle(0)
Long If repH

edit$( 1, _maxInt, _maxInt ) = "Parsing punctuation..." + chr$(13)

// kill all periods in container
count = fn ReplaceText( [@gC], repH, "." )
// kill all commas in container
count = fn ReplaceText( [@gC], repH, "," )
// kill all colons in container
count = fn ReplaceText( [@gC], repH, ":" )
// kill all semicolons in container
count = fn ReplaceText( [@gC], repH, ";" )
// kill all exclamation points in container
count = fn ReplaceText( [@gC], repH, "!" )
// kill all question marks in container
count = fn ReplaceText( [@gC], repH, "?" )
// kill all single quotes in container
count = fn ReplaceText( [@gC], repH, "'" )
// kill all double dashes in container
count = fn ReplaceText( [@gC], repH, "--" )

edit$( 1, _maxInt, _maxInt ) = "Converting tabs,¬
feeds and returns to spaces..." + chr$(13)

SetHandleSize( repH, 1 )
Long If Fn MemError  = _noErr
repH..0`` = _" "
// convert tabs to spaces
count = fn ReplaceText( [@gC], repH, chr$( 9) )
// convert all line feeds to spaces
count = fn ReplaceText( [@gC], repH, chr$(10) )
// convert carriage returns to spaces
count = fn ReplaceText( [@gC], repH, chr$(13) )
// Find and replace any occurences of multiple spaces
count = fn ReplaceText( [@gC], repH, "  " )
End If

DisposeHandle( repH )
end if
end fn


// Write the final array to disk
local fn WriteArrayToDisk( lastElement as long )
dim as str255 fStr
dim as long   i

route _toBuffer + 2

for i = 0 to lastElement
print myArray( i )
next i

route _toScreen

kill dynamic myArray

long if gFBBuffer(2)
// Save for opening in BBEdit
def open "TEXTR*ch"
fStr = files$( _fSave, "Save As...",¬
"Word index file", defaultVol )
hlock( gFBBuffer(2) )
open "O", 1, fStr, 1, defaultVol
write file 1, [gFBBuffer(2)], fn GetHandleSize( gFBBuffer(2) )
close 1
hunlock( gFBBuffer(2) )
def disposeh( gFBBuffer(2) )
end if

end fn


local fn SplitAndSortArray
dim as long lastElement

// Build array of words with space as delimiter
edit$( 1, _maxInt, _maxInt ) = "Creating array of individual¬
   words and/or expressions..." + chr$(13)
lastElement = FN Split( gC, " " )

// Empty container, no longer needed
gC = ""

// Sort the split array
edit$( 1, _maxInt, _maxInt ) = "Sorting and¬
indexing word list..." + chr$(13)
fn QuickSort( 0, lastElement )

// Index the words
edit$( 1, _maxInt, _maxInt ) = "Sorted and indexed¬
words are:" + chr$(13) + chr$(13)
lastElement = fn CreateWordIndex( lastElement )

// Print sorted list for demo
for i = 0 to lastElement
edit$(1, _maxInt, _maxInt) = myArray(i) + chr$(13)
next i

// Prepare to save text file to disk
edit$( 1, _maxInt, _maxInt ) = "Parsed word index ¬
file ready to save..." + chr$(13) + chr$(13)
fn WriteArrayToDisk( lastElement )

Button 3, _grayBtn
edit$( 1, _maxInt, _maxInt ) = "File contained"+¬
str$(lastElement) + " individual words and/or expressions."¬
+ chr$(13) + "Click quit to end."

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 2 : gFBQuit = _zTrue
case 3
fn ParseContainer
fn SplitAndSortArray
end select
end select

end fn

on dialog fn DoDialog

fn BuildWindow

do
handleevents
until gFBQuit
end