[futurebasic] Re: [FB] CFString

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2008 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Sat, 19 Jan 2008 16:48:07 -0500
Steve asked:

> Hey Ken,
>
> While you are playing, whip up some code that scans a folder and  
> puts the filenames in a CFIndex array :-)



Steve,

Your wish is my command.

Ken


/*

Scan Folder Contents into CFIndex

Sloppily coded at the command
of Steve Crossman

Ken Shmidheiser
Janaury 19, 2008

*/

include "Tlbx MoreFilesX.incl"
include "CFIndex.incl"
include "Tlbx CFString.incl"
include "Tlbx CFBundle.incl"

begin globals
dim as CFIndex gFolderItemsCFIndex
end globals

gFolderItemsCFIndex = 0

fn CFIndexClear( gFolderItemsCFIndex  )

_quitBtn    = 1
_iterateBtn = 2
_mainEF     = 10

local fn BuildWindow
dim as rect           r
dim as WindowAttributes wa

wa = _kWindowCloseBoxAttribute

setrect( r, 0, 0, 400, 550 )
appearance window -1, "", @r, _kDocumentWindowClass, wa
def SetWindowBackground(_kThemeActiveDialogBackgroundBrush,_zTrue)

text _courier, 12
setrect( r, 20, 20, 370, 490 )
edit field _mainEF, "", @r, _framedNoCR_noDrawFocus, _leftJust
setrect( r, 370, 20, 386, 490 )
scroll button -_mainEF,0,0,0,0,@r, _scrollOther

setrect( r, 310, 510, 380, 530 )
button _quitBtn, _activeBtn, "Quit", @r, _push

setrect( r, 130, 510, 300, 530 )
button _iterateBtn, _activeBtn, "Show Folder Contents", @r, _push

window 1

end fn

clear local mode
local fn IterateFolder( @inFolderRef as ^FSRef )
dim as OSStatus      err, ignore
dim as UInt32        index
dim as FSIterator  @ iterator
dim as long      @ actualCount
dim as str255      name, s

Xref catInfoArray(_maxInt) as FSCatalogInfo
Xref FSSpecArray (_maxInt) as FSSpec
Xref FSRefArray  (_maxInt) as FSRef

_kCatalogInfoBitmap =¬
_kFSCatInfoNodeFlags _kFSCatInfoFinderInfo

_kRequestCountPerIteration =¬
(( 4096 * 4 ) / sizeof( FSCatalogInfo ))
_kQuery = _kRequestCountPerIteration

err = fn FSOpenIterator( #inFolderRef, _kFSIterateFlat, iterator )
if err then exit fn

catInfoArray = fn NewPtr( sizeof( FSCatalogInfo) * _kQuery )
FSRefArray   = fn NewPtr( sizeof( FSRef)         * _kQuery )
FSSpecArray  = fn NewPtr( sizeof( FSSpec)        * _kQuery )

long if ( catInfoArray == _nil¬
         or FSRefArray == _nil¬
        or FSSpecArray == _nil )
err = _memFullErr
xelse
while ( err == _noErr )
err = Fn FSGetCatalogInfoBulk( iterator, ¬
               _kRequestCountPerIteration, ¬
                              actualCount, ¬
                                    #_nil, ¬
                      _kCatalogInfoBitmap, ¬
                            #catInfoArray, ¬
                              #FSRefArray, ¬
                             #FSSpecArray, ¬
                                     #_nil )

long if ( err == _noErr or err == _errFSNoMoreItems )
long if ( actualCount )
for index = 0 to actualCount - 1

// Get file names
s = string$( 1, 9 ) +¬
             "File: " +¬
FSSpecArray.name( index )
fn CFIndex( gFolderItemsCFIndex, index, s )

long if ( catInfoArray.nodeFlags( index )¬
and _kFSNodeIsDirectoryMask == _kFSNodeIsDirectoryMask )

// Get subfolder names
s = string$( 5, 9 ) +¬
        "Subfolder: " +¬
FSSpecArray.name( index )
fn CFIndex( gFolderItemsCFIndex, index, s )

end if
next
end if
end if
wend
if err == _errFSNoMoreItems Then err = _noErr

end if

if catInfoArray then DisposePtr( catInfoArray )
if FSRefArray   then DisposePtr( FSRefArray   )
if FSSpecArray  then DisposePtr( FSSpecArray  )

ignore = fn FSCloseIterator( iterator )

end fn = err

local fn DoFolderSearch
dim as FSSpec     fs
dim as FSRef    @ folderRef
dim as str255    folderStr
dim as long      i
dim as str255    s
dim as OSStatus   err

fn CFIndexClear( gFolderItemsCFIndex  )
edit$( 10 ) = ""

folderStr = files$( _FSSpecFolder, "Choose a folder",,fs )
long if ( folderStr[0] )
err = fn FSpMakeFSRef( fs, folderRef )
if err == _noErr then err = fn IterateFolder( folderRef )
s = chr$(13) + "Files and subfolders inside in the folder "
s += folderStr + " are:" + string$( 2, 13 )
edit$( _mainEf ) = s
xelse
edit$( _mainEf ) = "User canceled folder iteration"
exit fn
end if

for i = 0 to fn CFIndexGetCount( gFolderItemsCFIndex ) - 1
s = fn CFIndex$( gFolderItemsCFIndex, i ) + chr$(13)
edit$( _mainEf, _maxInt ) = s
next i
setselect 0, 0

end fn

local fn DoDialog
dim as long evnt, id

evnt = dialog(0)
id = dialog(evnt)

select case( evnt )
case _wndClose : end
case _btnClick
select case (id )
case 1 : end
case 2 : fn DoFolderSearch
end select
end select

end fn

on dialog fn DoDialog
fn BuildWindow

do
handleevents
until gFBQuit