[futurebasic] Re: [FB] Print CFString > 255

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2010 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Tue, 20 Apr 2010 20:29:01 +1200
Detlev Lang wrote:

> here is the (hopefully) clean function ,
> 
>> 

> local fn PrintCFStringFullLength( inString as CFStringRef )
> '~'1
> dim as Str255       PascalStr
> dim as CFStringRef   outString
> dim as CFIndex       inStart, inLength, length
> inStart   = 0
> inLength  = 255
> length    = fn CFStringGetLength( inString )
> do
> if ( inStart + inLength > length ) then inLength = length - inStart
> outString = fn CFStringCreateWithSubString( _kCFAllocatorDefault, inString, fn CFRangeMake( inStart, inLength ) )
> inStart += inLength
> if ( outString == 0 ) then outString = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
> fn CFStringGetPascalString( outString, @PascalStr, 256, _kCFStringEncodingMacRoman )
> CFRelease( outString )
> print PascalStr;
> until inStart >= length
> end fn

Thanks. I ran some test cases, which it handled correctly. 
This seems worthy to be an example in the next release (alongside FB_5_4_x_Examples/CoreFoundation/CFStrings in FB console).
The variables are renamed to be easier for my dull old brain to understand, and an apparently-redundant line of code removed.

'------------
include "ConsoleWindow"

/*
PrintCFStringFullLength() prints either to the console
or to a regular window. The CFString may be of any length,
including 0.

See also the demo "CFStrings in FB console".
*/

local fn PrintCFStringFullLength( inString as CFStringRef )
'~'1
dim as CFIndex       fullLength, j, chunkSize
dim as CFStringRef   chunk
dim as Str255        pascalChunk

fullLength = fn CFStringGetLength( inString )
chunkSize = 255
j = 0
do
if ( j + chunkSize > fullLength ) then chunkSize = fullLength - j // last chunk
chunk = fn CFStringCreateWithSubString( _kCFAllocatorDefault, inString, fn CFRangeMake( j, chunkSize ) )
j += chunkSize
fn CFStringGetPascalString( chunk, @pascalChunk, 256, _kCFStringEncodingMacRoman )
CFRelease( chunk )
print pascalChunk;
until ( j >= fullLength )
end fn


dim as CFMutableStringRef theString
dim as long j

fn PrintCFStringFullLength( @"0123456789" ) // 10 chars
print
fn PrintCFStringFullLength( @"" ) // 0 chars

theString = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
for j = 1 to 30
CFStringAppend( theString, @"0123456789" )
next
fn PrintCFStringFullLength( theString ) // 300 chars
CFRelease( theString )
'------------

Robert P.