[futurebasic] Re: [FB] continued - Problem converting Pascal strings to C strings - inside CFStringRef

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2011 : Group Archive : Group : All Groups

From: Thomas Peters <thomasg_peters@...>
Date: Fri, 30 Sep 2011 20:12:36 -0400
Cool... Where is the clang analyzer available - ahh... google search:
http://clang-analyzer.llvm.org

I am trying to adopt also...

:-)

On 9/29/11 6:10 PM, "Ken Shmidheiser" <kshmidheiser@...> wrote:

>Robert Purves suggested:
>
>> These three functions return a reference owned by the caller.
>> By convention their names should include 'Create' or 'Copy'; that helps
>>the programmer 
>> (and clang's analyzer) to see leaks elsewhere in the code.
>
>
>> > for i = 0 to fn CFArrayGetCount( keyArray ) -1
>> 
>> The 'to' expression is evaluated on every iteration. For performance
>>reasons it's better to cache it.
>> dim as CFIndex             i, n
>> n = fn CFArrayGetCount( keyArray )
>> for i = 0 to n - 1
>
>Robert,
>
>Thanks for the reminders... I'm still finding my way around clang's
>analyzer 
>(can't wait until we get that wonderful formatted output you posted
>earlier). 
>
>I will try to watch my function naming conventions going forward, but old
>habits die hard.
>
>Also thanks to Bernie for his backchannel optimization.
>
>Revised code follows.
>
>Ken
>
>/*
>
>Simple CFDictionary Demo
>showing formatted value data
>
>Ken Shmidheiser
>Sept. 29, 2011
>
>Revised Sept. 29, 2011
>
>Incorporating suggestions from
>Bernie Wylde & Robert Purves
>
>*/
>
>include "ConsoleWindow"
>include "Tlbx CFNumberFormatter.incl
>include "Tlbx CFDateFormatter.incl"
>include "Tlbx CFLocale.incl"
>
>// Use a global dictionary for this example
>begin globals
>dim as CFMutableDictionaryRef  gDict
>end globals
>
>// Create dictionary
>local fn CFDictCreate as CFMutableDictionaryRef
>'~'1
>dim as CFMutableDictionaryRef  dict
>dim as CFMutableArrayRef       keyArray, valueArray
>dim as Double                  testDouble
>dim as CFNumberRef             valueDouble
>dim as CFAbsoluteTime          absoluteTime
>dim as CFDateRef               currentDate
>dim as CFIndex                 i, count
>
>// Create array to hold dictionary keys as CFStringRefs
>// Note: These are simply labels that point to the corresponding value
>keyArray = fn CFArrayCreateMutable( _kCFAllocatorDefault, 0,
>@kCFTypeArrayCallBacks )
>CFArrayAppendValue( keyArray, @"Robert Purves"    )
>CFArrayAppendValue( keyArray, @"Brian Stevens"    )
>CFArrayAppendValue( keyArray, @"Bernie Wylde"     )
>CFArrayAppendValue( keyArray, @"Max Taylor"       )
>CFArrayAppendValue( keyArray, @"Steve Van Voorst" )
>CFArrayAppendValue( keyArray, @"Double Value"     )
>CFArrayAppendValue( keyArray, @"Today's Date"     )
>
>// Create array to hold matching dictionary values
>// Here we begin with a few CFStringRefs...
>valueArray = fn CFArrayCreateMutable( _kCFAllocatorDefault, 0,
>@kCFTypeArrayCallBacks )
>CFArrayAppendValue( valueArray, @"Code Meister"       )
>CFArrayAppendValue( valueArray, @"First to Help"      )
>CFArrayAppendValue( valueArray, @"Backchannel Rescue" )
>CFArrayAppendValue( valueArray, @"Objectifier"        )
>CFArrayAppendValue( valueArray, @"Demo King"          )
>
>// ...then we add a CFNumber to the valueArray
>// as the value of the "Double Value" key
>testDouble = 1065407.533
>valueDouble = fn CFNumberCreate( _kCFAllocatorDefault,
>_kCFNumberDoubleType, @testDouble )
>CFArrayAppendValue( valueArray, valueDouble )
>CFRelease( valueDouble )
>
>// ...and then we add today's date as a CFDate to the valueArray
>// as the value of the "Today's Date" key
>absoluteTime = fn CFAbsoluteTimeGetCurrent
>currentDate = fn CFDateCreate( _kCFAllocatorDefault, absoluteTime )
>CFArrayAppendValue( valueArray, currentDate )
>CFRelease( currentDate )
>
>// Create dictionary and load matching keys and values into it
>// We will use kCFTypeDictionaryKeyCallBacks since we are mixing types
>dict = fn CFDictionaryCreateMutable( _kCFAllocatorDefault, 0,
>@kCFTypeDictionaryKeyCallBacks, @kCFTypeDictionaryValueCallBacks )
>long if ( dict )
>count = fn CFArrayGetCount( keyArray )
>long if ( count )
>for i = 0 to count - 1
>// Set corresponding dictionary keys and values
>CFDictionarySetValue( dict, fn CFArrayGetValueAtIndex( keyArray, i ), fn
>CFArrayGetValueAtIndex( valueArray, i ) )
>next i
>end if
>end if
>// Dictionary loaded; release arrays
>CFRelease( keyArray )
>CFRelease( valueArray )
>end fn = dict     // User must release dictionary
>
>
>local fn CFNumberCreateString( number as CFNumberRef ) as CFStringRef
>'~'1
>dim as CFNumberFormatterRef  numberFormatter
>dim as CFStringRef           numberStr
>dim as CFLocaleRef           currentLocale
>
>currentLocale = fn CFLocaleCopyCurrent
>numberFormatter = fn CFNumberFormatterCreate( _kCFAllocatorDefault,
>currentLocale, _kCFNumberFormatterDecimalStyle )
>CFRelease( currentLocale )
>numberStr = fn CFNumberFormatterCreateStringWithNumber(
>_kCFAllocatorDefault, numberFormatter, number )
>CFRelease( numberFormatter )
>end fn = numberStr
>
>
>local fn CFDateCreateString( date as CFDateRef ) as CFStringRef
>'~'1
>dim as CFLocaleRef         currentLocale
>dim as CFStringRef         dateStr
>dim as CFDateFormatterRef  formatter
>
>currentLocale = fn CFLocaleCopyCurrent
>formatter = fn CFDateFormatterCreate( _kCFAllocatorDefault,
>currentLocale, _kCFDateFormatterLongStyle, _kCFDateFormatterNoStyle )
>CFRelease( currentLocale )
>dateStr = fn CFDateFormatterCreateStringWithDate( _kCFAllocatorDefault,
>formatter, date )
>CFRelease( formatter )
>end fn = dateStr
>
>
>// Simple code to find matching value to dictionary key
>dim as CFMutableArrayRef   keyArray
>dim as CFIndex             i, count
>dim as CFTypeRef           value
>dim as CFStringRef         tempStr
>dim as CFMutableStringRef  mutStr
>
>gDict = fn CFDictCreate()
>mutStr = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
>
>// Create array to hold keys
>keyArray = fn CFArrayCreateMutable( _kCFAllocatorDefault, 0,
>@kCFTypeArrayCallBacks )
>CFArrayAppendValue( keyArray, @"Robert Purves"    )
>CFArrayAppendValue( keyArray, @"Brian Stevens"    )
>CFArrayAppendValue( keyArray, @"Bernie Wylde"     )
>CFArrayAppendValue( keyArray, @"Max Taylor"       )
>CFArrayAppendValue( keyArray, @"Steve Van Voorst" )
>CFArrayAppendValue( keyArray, @"Double Value"     )
>CFArrayAppendValue( keyArray, @"Today's Date"     )
>
>// Search dictionary to find matching value for key
>count = fn CFArrayGetCount( keyArray )
>long if ( count )
>for i = 0 to count - 1
>tempStr = fn CFArrayGetValueAtIndex( keyArray, i )
>value = fn CFDictionaryGetValue( gDict, tempStr )
>
>CFStringAppend( mutStr, tempStr  )
>CFStringAppend( mutStr, @" = "   )
>
>select ( fn CFGetTypeID( value ) )// Test value for CFType
>case fn CFStringGetTypeID()// If value is CFString, process as string
>CFStringAppend( mutStr, value )
>
>case fn CFNumberGetTypeID()// If value is CFNumber, convert to string
>tempStr = fn CFNumberCreateString( value )
>CFStringAppend( mutStr, tempStr )
>CFRelease( tempStr )
>
>case fn CFDateGetTypeID()// If value is CFDate, convert to string
>tempStr = fn CFDateCreateString( value )
>CFStringAppend( mutStr, tempStr )
>CFRelease( tempStr )
>
>end select
>
>CFStringAppend( mutStr, @"\r" )
>next i
>end if
>
>// Empty trash
>CFRelease( keyArray )
>CFRelease( gDict )
>
>// Print results
>fn HIViewSetText( sConsoleHITextView, mutStr )
>CFRelease( mutStr )
>
>
>
>
>--
>To unsubscribe, send ANY message to: futurebasic-unsubscribe@...
>To access the list archives, go to:
>http://freegroups.net/groups/futurebasic/
>
>