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

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

From: Thomas Peters <thomasg_peters@...>
Date: Fri, 30 Sep 2011 19:57:51 -0400
This example is extremely helpful Ken, thanks...


>I am assuming you want to pass the corresponding values as C strings to
>MySQL functions. 
>If so, and you want to use a CFDictionary to store the data, you will
>need to enter the values
>formatted as either strings, numbers or dates. Since the keys are simply
>descriptive labels
>that point to the values, they can be strings.

Yes...

>
>Once you have the values stored in the proper format, you can easily
>locate 
>each respective value in the dictionary by calling its key. You can then
>test the value 
>returned for it's CFTypeID. If it's a CFStringRef, you can simply convert
>the CFStringRef 
>into a C String and pass that to your MySQL function.

Yes...  That's what I am doing Ken, cool.  But when I get the strings from
the dictionary, I need then as C strings.

>
>If the CFTypeID test determines the value is a CFNumber or a CFDate,
>you need to take an extra step and convert that value into a CFStringRef,
>which you can, in turn, convert into a C String and pass that to your
>MySQL function.

I am also doing that...
 Thanks...

>
>The data will remain in your dictionary and can be used over and over
>again.

That, I also expected...  Yap...

>
>(There are, of course, many different Core Foundation types, but the
>conversion 
>process to CFStringRefs is similar for each type.)
>
>Since I don't know exactly what format you want for your keys, I have put
>together 
>a little demo showing how to load CFStringRefs, a CFNumberRef (a double),
>and a CFDateRef holding the current date, into a dictionary, each
>properly formatted
>with a corresponding key as a descriptive label.
>
>When the key is used to call the corresponding value, the code tests the
>CFTypeID 
>of the value, and processes it according to its type.
>
>It appears you have mastered converting Pascal strings back and forth
>between CFStringRefs, so this code bypasses that.
>
>Ken
>
>
>/*
>
>Simple CFDictionary Demo
>showing formatted value data
>
>Ken Shmidheiser
>Sept. 29, 2011
>
>*/
>
>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 LoadCFDictionary 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
>
>// 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 )
>for i = 0 to fn CFArrayGetCount( keyArray ) -1
>// Set corresponding dictionary keys and values
>CFDictionarySetValue( dict, fn CFArrayGetValueAtIndex( keyArray, i ), fn
>CFArrayGetValueAtIndex( valueArray, i ) )
>next i
>end if
>// Dictionary loaded; release arrays
>CFRelease( keyArray )
>CFRelease( valueArray )
>end fn = dict     // User must release dictionary
>
>
>local fn ConvertCFNumberAsDoubleToCFString( 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 ConvertCFDateToCFString( 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
>dim as CFTypeRef           value
>dim as CFTypeID            typeID
>dim as CFStringRef         tempStr
>dim as CFMutableStringRef  mutStr
>
>gDict = fn LoadCFDictionary
>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
>for i = 0 to fn CFArrayGetCount( keyArray ) -1
>
>tempStr = fn CFArrayGetValueAtIndex( keyArray, i )
>value = fn CFDictionaryGetValue( gDict, tempStr )
>
>CFStringAppend( mutStr, tempStr  )
>CFStringAppend( mutStr, @" = "   )
>
>// Test value for CFType
>typeID = fn CFGetTypeID( value )
>
>// If value is CFString, process as string
>long if ( typeID == fn CFStringGetTypeID )
>CFStringAppend( mutStr, value )
>end if
>
>// If value is CFNumber, convert to string
>long if ( typeID == fn CFNumberGetTypeID )
>tempStr = fn ConvertCFNumberAsDoubleToCFString( value )
>CFStringAppend( mutStr, tempStr )
>end if
>
>// If value is CFDate, convert to string
>long if ( typeID == fn CFDateGetTypeID )
>tempStr = fn ConvertCFDateToCFString( value )
>CFStringAppend( mutStr, tempStr )
>end if
>
>CFStringAppend( mutStr, @"\r" )
>
>next i
>// 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/
>
>