[Tcladdressbook-commits] Source TclAddressBookUtils.c,1.22,1.23
Status: Alpha
Brought to you by:
bdesgraupes
|
From: Bernard D. <bde...@us...> - 2004-08-25 10:06:46
|
Update of /cvsroot/tcladdressbook/Source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1373/Source Modified Files: TclAddressBookUtils.c Log Message: No need to work on a copy of the CF string, retains are ok. Index: TclAddressBookUtils.c =================================================================== RCS file: /cvsroot/tcladdressbook/Source/TclAddressBookUtils.c,v retrieving revision 1.22 retrieving revision 1.23 diff -u -d -r1.22 -r1.23 --- TclAddressBookUtils.c 8 Aug 2004 20:32:44 -0000 1.22 +++ TclAddressBookUtils.c 25 Aug 2004 10:06:35 -0000 1.23 @@ -1,7 +1,7 @@ /* * File : "TclAddressBookUtils.c" * Created: 2003-12-05 10:08:03 - * Last modification: 2004-08-07 21:19:06 + * Last modification: 2004-08-11 08:28:25 * Author: Bernard Desgraupes * e-mail: <bde...@ea...> * @@ -123,7 +123,6 @@ Boolean allValues, int inRecordType) { CFStringRef subLabel, subValue, localizedStr; - CFTypeRef theValue; CFAbsoluteTime theAbsTime; ABPropertyType thePropType; ABMutableMultiValueRef multiValue; @@ -143,24 +142,20 @@ } else { thePropType = ABTypeOfProperty(ab, kABGroupRecordType, inProperty); } - // Work on a local "copy" of the CFStringRef. Not sure this is - // necessary since inValue already has a retain count equal to 1. - theValue = CFRetain(inValue); - - if (theValue) { + + if (inValue) { switch (thePropType) { case kABStringProperty: // Get the entry for the string property - if (CFStringGetCString(theValue, theStr, sizeof(theStr), NULL)) { + if (CFStringGetCString(inValue, theStr, sizeof(theStr), NULL)) { TclAB_PutValueInResult(interp, thePropName, theStr, allValues); } - CFRelease(theValue); break; case kABDateProperty: - theAbsTime = CFDateGetAbsoluteTime(theValue); + theAbsTime = CFDateGetAbsoluteTime(inValue); // Offset the value by 31 years: 1 jan 1970 is the reference date for // Unix, while it is 1 jan 2001 for the CFDate functions. This will // make the returned value suitable for use with [clock format]: @@ -169,21 +164,19 @@ theAbsTime += kCFAbsoluteTimeIntervalSince1970; sprintf(theStr, "%.0f", theAbsTime); TclAB_PutValueInResult(interp, thePropName, theStr, allValues); - CFRelease(theValue); break; case kABIntegerProperty: - if (CFNumberGetValue(theValue, kCFNumberSInt32Type, &theSInt32)) { + if (CFNumberGetValue(inValue, kCFNumberSInt32Type, &theSInt32)) { sprintf(theStr, "%d", theSInt32); TclAB_PutValueInResult(interp, thePropName, theStr, allValues); } - CFRelease(theValue); break; case kABMultiStringProperty: - multiValue = (ABMutableMultiValueRef) theValue; + multiValue = (ABMutableMultiValueRef) inValue; mvCount = ABMultiValueCount(multiValue); Tcl_DStringInit(&ds); @@ -206,12 +199,11 @@ } TclAB_PutValueInResult(interp, thePropName, ds.string, allValues); Tcl_DStringFree(&ds); - CFRelease(multiValue); break; case kABMultiDateProperty: - multiValue = (ABMutableMultiValueRef) theValue; + multiValue = (ABMutableMultiValueRef) inValue; mvCount = ABMultiValueCount(multiValue); Tcl_DStringInit(&ds); @@ -237,12 +229,11 @@ } TclAB_PutValueInResult(interp, thePropName, ds.string, allValues); Tcl_DStringFree(&ds); - CFRelease(multiValue); break; case kABMultiDictionaryProperty: - multiValue = (ABMutableMultiValueRef) theValue; + multiValue = (ABMutableMultiValueRef) inValue; mvCount = ABMultiValueCount(multiValue); Tcl_DStringInit(&ds); @@ -263,37 +254,39 @@ if (CFStringGetCString(localizedStr, theLabelStr, sizeof(theLabelStr), NULL)) { // Get the dict theDict = ABMultiValueCopyValueAtIndex(multiValue, j); - dictCount = CFDictionaryGetCount(theDict); - // Allocate memory to store the keys and values - theKeys = (CFStringRef*) NewPtrClear(sizeof(CFStringRef*) * dictCount); - theVals = (CFStringRef*) NewPtrClear(sizeof(CFStringRef*) * dictCount); - if ((theKeys != NULL) && (theVals != NULL)) { - Tcl_DStringStartSublist(&ds); - Tcl_DStringAppendElement(&ds, theLabelStr); - Tcl_DStringStartSublist(&ds); - // Fill the keys and values arrays from this dictionary - CFDictionaryGetKeysAndValues(theDict, theKeys, theVals); - // Loop over all the entries - for (k = 0; k < dictCount; k++) { - if (theKeys[k] && theVals[k] ) { - // Make a two items list for each key/value pair - if (CFStringGetCString(theKeys[k], theStr, sizeof(theStr), NULL) - && CFStringGetCString(theVals[k], theSubStr, sizeof(theSubStr), NULL)) { - Tcl_DStringStartSublist(&ds); - Tcl_DStringAppendElement(&ds, theStr); - Tcl_DStringAppendElement(&ds, theSubStr); - Tcl_DStringEndSublist(&ds); + if (theDict) { + dictCount = CFDictionaryGetCount(theDict); + // Allocate memory to store the keys and values + theKeys = (CFStringRef*) NewPtrClear(sizeof(CFStringRef) * dictCount); + theVals = (CFStringRef*) NewPtrClear(sizeof(CFStringRef) * dictCount); + if ((theKeys != NULL) && (theVals != NULL)) { + Tcl_DStringStartSublist(&ds); + Tcl_DStringAppendElement(&ds, theLabelStr); + Tcl_DStringStartSublist(&ds); + // Fill the keys and values arrays from this dictionary + CFDictionaryGetKeysAndValues(theDict, theKeys, theVals); + // Loop over all the entries + for (k = 0; k < dictCount; k++) { + if (theKeys[k] && theVals[k] ) { + // Make a two items list for each key/value pair + if (CFStringGetCString(theKeys[k], theStr, sizeof(theStr), NULL) + && CFStringGetCString(theVals[k], theSubStr, sizeof(theSubStr), NULL)) { + Tcl_DStringStartSublist(&ds); + Tcl_DStringAppendElement(&ds, theStr); + Tcl_DStringAppendElement(&ds, theSubStr); + Tcl_DStringEndSublist(&ds); + } } - } + } + Tcl_DStringEndSublist(&ds); + Tcl_DStringEndSublist(&ds); + } else { + // "Could not allocate memory for dictionary" } - Tcl_DStringEndSublist(&ds); - Tcl_DStringEndSublist(&ds); - } else { - // "Could not allocate memory for dictionary" - } - if (theKeys) DisposePtr(theKeys); - if (theVals) DisposePtr(theVals); - if (theDict) CFRelease(theDict); + if (theKeys) DisposePtr(theKeys); + if (theVals) DisposePtr(theVals); + CFRelease(theDict); + } } CFRelease(localizedStr); } @@ -301,14 +294,11 @@ TclAB_PutValueInResult(interp, thePropName, ds.string, allValues); Tcl_DStringFree(&ds); - // Don't CFRelease the multiValue: each dict is released one after the other - CFRelease(multiValue); break; case kABErrorInProperty: default: - CFRelease(theValue); break; } } @@ -514,8 +504,8 @@ Tcl_ListObjLength(interp, dictPtr, &subListLen); // Allocate memory to store the keys and values - theKeys = (CFStringRef*) NewPtrClear(sizeof(CFStringRef*) * subListLen); - theVals = (CFStringRef*) NewPtrClear(sizeof(CFStringRef*) * subListLen); + theKeys = (CFStringRef*) NewPtrClear(sizeof(CFStringRef) * subListLen); + theVals = (CFStringRef*) NewPtrClear(sizeof(CFStringRef) * subListLen); if ((theKeys != NULL) && (theVals != NULL)) { // Loop over all the elements of the flat dict list for (k = 0; k < subListLen; k++) { @@ -665,15 +655,15 @@ CONST84 char * option, ABRecordRef * outItemRef, int inRecordType) { - CFStringRef theCFStr; + CFStringRef uidRef; int kind; Boolean result = true; - theCFStr = CFStringCreateWithCString(NULL, inUID, NULL); + uidRef = CFStringCreateWithCString(NULL, inUID, NULL); // Find the record corresponding to the UID - *outItemRef = ABCopyRecordForUniqueId(ab, theCFStr); - CFRelease(theCFStr); + *outItemRef = ABCopyRecordForUniqueId(ab, uidRef); + CFRelease(uidRef); if (!*outItemRef) { Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "Unrecognized record ID in ", option, @@ -861,7 +851,7 @@ int inRecordType) { CFIndex theCount; - ABGroupRef theGroup; + ABRecordRef theRecord; CFStringRef text, uniqueId; char theName[256], theUID[256]; Tcl_Obj * objPtr; @@ -877,9 +867,9 @@ Tcl_Obj * listPtr; // Get the record at index i - theGroup = (ABGroupRef) CFArrayGetValueAtIndex(allElements,i); + theRecord = (ABRecordRef) CFArrayGetValueAtIndex(allElements,i); // Get the unique id for this record - uniqueId = ABRecordCopyUniqueId(theGroup); + uniqueId = ABRecordCopyUniqueId(theRecord); CFStringGetCString(uniqueId, theUID, sizeof(theUID), NULL); CFRelease(uniqueId); @@ -888,9 +878,9 @@ } // Try to get the entry for the kABGroupNameProperty. If that fails, just list the UID if (inRecordType == rec_group) { - text = ABRecordCopyValue((ABGroupRef) theGroup, kABGroupNameProperty); + text = ABRecordCopyValue(theRecord, kABGroupNameProperty); } else if (inRecordType == rec_person) { - text = ABRecordCopyValue((ABGroupRef) theGroup, kABLastNameProperty); + text = ABRecordCopyValue(theRecord, kABLastNameProperty); } if (text) { @@ -1182,27 +1172,32 @@ ab = ABGetSharedAddressBook(); // Get the CFArray of properties theItems = ABCopyArrayOfPropertiesForRecordType(ab, inRecordType ? kABPersonRecordType : kABGroupRecordType); - theCount = CFArrayGetCount(theItems); - - for (i = 0; i < theCount; i++) { - // Get the record at index i - theProperty = CFArrayGetValueAtIndex(theItems,i); + if (theItems) { + theCount = CFArrayGetCount(theItems); - if (theProperty && CFStringGetCString(theProperty, theName, sizeof(theName), NULL)) { - Tcl_DString ds; + for (i = 0; i < theCount; i++) { + // Get the record at index i + theProperty = CFArrayGetValueAtIndex(theItems,i); - Tcl_DStringInit(&ds); - Tcl_ExternalToUtfDString(gMacRomanEnc, theName, strlen(theName), &ds); - objPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)); - Tcl_DStringFree(&ds); - result = Tcl_ListObjAppendElement(interp, resultPtr, objPtr); - if (result != TCL_OK) { - Tcl_DecrRefCount(objPtr); - break; - } - } + if (theProperty && CFStringGetCString(theProperty, theName, sizeof(theName), NULL)) { + Tcl_DString ds; + + Tcl_DStringInit(&ds); + Tcl_ExternalToUtfDString(gMacRomanEnc, theName, strlen(theName), &ds); + objPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)); + Tcl_DStringFree(&ds); + result = Tcl_ListObjAppendElement(interp, resultPtr, objPtr); + if (result != TCL_OK) { + Tcl_DecrRefCount(objPtr); + break; + } + } + } + CFRelease(theItems); + } else { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get array of properties for record type", (char *) NULL); + return TCL_ERROR; } - CFRelease(theItems); return TCL_OK; } @@ -1238,7 +1233,7 @@ int inRecordType) { ABAddressBookRef ab; - CFStringRef theProp; + CFStringRef propRef; CFMutableArrayRef theArray; int length, num, result = TCL_OK; char * propName; @@ -1252,43 +1247,58 @@ Tcl_IncrRefCount(objv[4]); propName = Tcl_GetStringFromObj(objv[4], &length); - theProp = CFStringCreateWithCString(NULL, propName, kCFStringEncodingUTF8); + propRef = CFStringCreateWithCString(NULL, propName, kCFStringEncodingUTF8); - if (theProp) { + if (propRef) { // Get the address book ab = ABGetSharedAddressBook(); if (inRecordType == rec_undefined) { // The third argument was a record ID CONST84 char * theUID; - CFStringRef theCFStr; + CFStringRef uidRef; ABRecordRef theRecord; theUID = Tcl_GetStringFromObj(objv[3], &length); - theCFStr = CFStringCreateWithCString(NULL, theUID, NULL); + uidRef = CFStringCreateWithCString(NULL, theUID, NULL); - // Find the record corresponding to the UID - theRecord = ABCopyRecordForUniqueId(ab, theCFStr); - CFRelease(theCFStr); - if (!ABRecordRemoveValue(theRecord, theProp)) { - Tcl_AppendStringsToObj(resultPtr, "Couldn't remove property '", propName, - "' from the record", (char *) NULL); + if (uidRef) { + // Find the record corresponding to the UID + theRecord = ABCopyRecordForUniqueId(ab, uidRef); + CFRelease(uidRef); + if (theRecord) { + if (!ABRecordRemoveValue(theRecord, propRef)) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't remove property '", propName, + "' from the record", (char *) NULL); + result = TCL_ERROR; + } + CFRelease(theRecord); + } else { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get record for ID", (char *) NULL); + result = TCL_ERROR; + } + } else { + Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for UID", (char *) NULL); result = TCL_ERROR; - } - CFRelease(theRecord); + } } else { // The third argument was -groups or -persons. // Create a CFArray to store the property. - theArray = CFArrayCreateMutable(NULL, 0, NULL); - CFArrayAppendValue(theArray, theProp); - CFRelease(theProp); - - // Remove the property from all the records of this type in the Address Book - num = ABRemoveProperties(ab, inRecordType ? kABPersonRecordType : kABGroupRecordType, theArray); - CFRelease(theArray); - Tcl_SetObjResult(interp, Tcl_NewIntObj(num)); - result = TCL_OK; + theArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); + if (theArray) { + CFArrayAppendValue(theArray, propRef); + + // Remove the property from all the records of this type in the Address Book + num = ABRemoveProperties(ab, inRecordType ? kABPersonRecordType : kABGroupRecordType, theArray); + CFRelease(theArray); + Tcl_SetObjResult(interp, Tcl_NewIntObj(num)); + result = TCL_OK; + } else { + Tcl_AppendStringsToObj(resultPtr, "Problem with CFArrayCreateMutable", (char *) NULL); + result = TCL_ERROR; + } } + CFRelease(propRef); } else { Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for property", (char *) NULL); result = TCL_ERROR; |