[futurebasic] Re: [FB] Total items count for folder

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2013 : Group Archive : Group : All Groups

From: Brian S <fblistserve@...>
Date: Thu, 27 Jun 2013 08:29:35 -0700
On Jun 27, 2013, at 7:04 AM, Bernie <fblist.bw@...> wrote:

> 
> rc wrote:
> 
>> Thanks. No handy one liners for this I guess. :) 
> 
>  Could be used with NSTask or popen() to avoid pascal strings.
Here is a popen function. Let me know if you need code calling it or using the result.

Note: Mixing CF coding and pascal strings typically creates extra conversion code that is otherwise not needed if using CFStrings. Bernie’s example takes a URL that is usable as is in CF code and does a conversion to pascal to enable use of 'open unix'. IMO it is easier for the programmer if the code is either (1) all pascal strings  OR (2) all CFStrings with no mixing.

'-----------
local fn PipeOpen( cmdString as CFStringRef ) as pointer
'~'1
dim as pointer        buffer, unixPipe : unixPipe = 0
dim as CFIndex        length
dim type.2

length = fn CFStringGetMaximumSizeForEncoding( fn CFStringGetLength( cmdString ), _kCFStringEncodingUTF8 ) + 1
buffer = fn malloc( length )

type.0`` = _"r" : type.1`` = 0 // null-terminated C string

long if ( buffer ) 
long if ( fn CFStringGetCString( cmdString, #buffer, length, _kCFStringEncodingUTF8 ) )
unixPipe = fn popen( #buffer, type )
end if
end if

free( buffer )

end fn = unixPipe
'-----------

Brian S.



> '---------------
> include "ConsoleWindow"
> 
> local fn FileCountInDirectory( url as CFURLRef, recurse as Boolean )
> '~'1
> dim as Str255 s, cmd, reply
> dim as CFStringRef path
> 
> path = fn CFURLCopyPath( url )
> fn CFStringGetPascalString( path, @s, 256, _kCFStringEncodingMacRoman )
> CFRelease( path )
> cmd = "find " + s
> if ( recurse == _false ) then cmd += " -depth 1"
> cmd += " -type f | wc -l"
> open "UNIX", 222, cmd
> input #222, reply
> close 222
> end fn = val( reply )
> 
> dim as CFURLRef dirURL
> 
> if ( files$( _CFURLRefFolder, "Select slides folder",, @dirURL ) )
> print fn FileCountInDirectory( dirURL, _true )
> CFRelease( dirURL )
> end if
> '---------------