[Tcladdressbook-commits] Source TclAddressBook.c,1.35,1.36
Status: Alpha
Brought to you by:
bdesgraupes
|
From: Bernard D. <bde...@us...> - 2004-08-03 09:53:48
|
Update of /cvsroot/tcladdressbook/Source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18164/Source Modified Files: TclAddressBook.c Log Message: More error checking. Inspected all CFRelease() calls. Index: TclAddressBook.c =================================================================== RCS file: /cvsroot/tcladdressbook/Source/TclAddressBook.c,v retrieving revision 1.35 retrieving revision 1.36 diff -u -d -r1.35 -r1.36 --- TclAddressBook.c 2 Aug 2004 22:49:38 -0000 1.35 +++ TclAddressBook.c 3 Aug 2004 09:53:38 -0000 1.36 @@ -1,7 +1,7 @@ /* * File : "TclAddressBook.c" * Created: 2003-11-26 12:54:15 - * Last modification: 2004-08-02 09:29:40 + * Last modification: 2004-08-03 11:45:18 * Author: Bernard Desgraupes * e-mail: <bde...@ea...> * @@ -479,7 +479,7 @@ // Get the address book ab = ABGetSharedAddressBook(); - if(lookingInGroup) { + if (lookingInGroup) { if ( !TclAB_CheckItem(interp, ab, inGroupUID, "-ingroup", &inGroupRef, rec_group) ) { return TCL_ERROR; } @@ -497,13 +497,18 @@ } } - theCount = CFArrayGetCount(theItems); - CFRelease(theItems); - objPtr = Tcl_NewIntObj(theCount); - result = Tcl_ListObjAppendElement(interp, resultPtr, objPtr); - if (result != TCL_OK) { - Tcl_DecrRefCount(objPtr); + if (!theItems) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get list of groups", (char *) NULL); return TCL_ERROR; + } else { + theCount = CFArrayGetCount(theItems); + CFRelease(theItems); + objPtr = Tcl_NewIntObj(theCount); + result = Tcl_ListObjAppendElement(interp, resultPtr, objPtr); + if (result != TCL_OK) { + Tcl_DecrRefCount(objPtr); + return TCL_ERROR; + } } return TCL_OK; @@ -911,6 +916,7 @@ ABRecordRef theRecord; CFStringRef uidRef; CFDataRef imageRef; + CONST84 char * theUID; int length, kind, result = TCL_OK; Tcl_Obj * objPtr; Boolean setIt = false; @@ -928,19 +934,12 @@ setIt = true; } - uidRef = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(objv[2], &length), NULL); // Get the address book ab = ABGetSharedAddressBook(); - // Find the record corresponding to the UID - result = TclAB_RecordFromUID(ab, uidRef, resultPtr, &theRecord); - CFRelease(uidRef); - if (!result) {return TCL_ERROR;} - - // Is it a person or a group? - kind = TclAB_TypeFromRecord(ab, theRecord); - if (kind != rec_person) { - Tcl_AppendStringsToObj(resultPtr, "Not a person record", (char *) NULL); - CFRelease(theRecord); + + // Check if second argument is a person and get the corresponding record + theUID = Tcl_GetStringFromObj(objv[2], &length); + if ( !TclAB_CheckItem(interp, ab, theUID, "personID", &theRecord, rec_person) ) { return TCL_ERROR; } @@ -1040,37 +1039,42 @@ ab = ABGetSharedAddressBook(); // Loop over all the properties and copy them the the new record allProps = ABCopyArrayOfPropertiesForRecordType(ab, kABPersonRecordType); - theCount = CFArrayGetCount(allProps); - for (i = 0; i < theCount; i++) { - // Get the property at index i - theProperty = CFArrayGetValueAtIndex(allProps, i); - if (theProperty) { - theValue = ABRecordCopyValue(importedRecord, theProperty); - if (!theValue) { - continue; + if (!allProps) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get list of properties", (char *) NULL); + result = TCL_ERROR; + } else { + theCount = CFArrayGetCount(allProps); + for (i = 0; i < theCount; i++) { + // Get the property at index i + theProperty = CFArrayGetValueAtIndex(allProps, i); + if (theProperty) { + theValue = ABRecordCopyValue(importedRecord, theProperty); + if (!theValue) { + continue; + } + ABRecordSetValue(newRecord, theProperty, theValue); + CFRelease(theValue); + } + } + CFRelease(allProps); + + // TODO: also copy the image if any + + // Add the new record to the Address Book. Don't save here: leave + // this to the Tcl level. + if ( ABAddRecord(ab, newRecord) ) { + // Retrieve the UID for the newly created record + uidRef = ABRecordCopyUniqueId(newRecord); + if (uidRef) { + CFStringGetCString(uidRef, theUID, sizeof(theUID), NULL); + CFRelease(uidRef); + Tcl_AppendStringsToObj(resultPtr, theUID, (char *) NULL); } - ABRecordSetValue(newRecord, theProperty, theValue); - CFRelease(theValue); + } else { + Tcl_AppendStringsToObj(resultPtr, "Couldn't add record to the Address Book", (char *) NULL); + result = TCL_ERROR; } } - if (allProps) CFRelease(allProps); - - // TODO: also copy the image if any - - // Add the new record to the Address Book. Don't save here: leave - // this to the Tcl level. - if ( ABAddRecord(ab, newRecord) ) { - // Retrieve the UID for the newly created record - uidRef = ABRecordCopyUniqueId(newRecord); - if (uidRef) { - CFStringGetCString(uidRef, theUID, sizeof(theUID), NULL); - CFRelease(uidRef); - Tcl_AppendStringsToObj(resultPtr, theUID, (char *) NULL); - } - } else { - Tcl_AppendStringsToObj(resultPtr, "Couldn't add record to the Address Book", (char *) NULL); - result = TCL_ERROR; - } } if (importedRecord) CFRelease(importedRecord); if (newRecord) CFRelease(newRecord); @@ -1285,6 +1289,7 @@ kind = TclAB_TypeFromRecord(ab, theRecord); if (kind == rec_undefined) { Tcl_AppendStringsToObj(resultPtr, "Unknown type for recordID argument", (char *) NULL); + CFRelease(theRecord); return TCL_ERROR; } @@ -1756,6 +1761,7 @@ // Create a CF reference for the searched value, the label and the key if (!TclAB_MakeSearchRefFromTcl(interp, theValue, thePropType, &labelRef, &keyRef, &valueRef)) { + CFRelease(propertyRef); return TCL_ERROR; } if (!valueRef) { @@ -1870,10 +1876,11 @@ CONST84 char * theProperty; CONST84 char * theUID; CONST84 char * theNewVal; - CFStringRef uidRef, propertyRef, valueRef; + CFStringRef uidRef, propertyRef; + CFTypeRef valueRef; ABRecordRef theRecord; ABPropertyType thePropType; - int kind, length, result; + int kind, length, result = TCL_OK; Boolean setIt = false; if (objc != 4 && objc != 5) { @@ -1893,7 +1900,6 @@ // Make CFString objects uidRef = CFStringCreateWithCString(NULL, theUID, NULL); - propertyRef = CFStringCreateWithCString(NULL, theProperty, kCFStringEncodingUTF8); // Get the address book ab = ABGetSharedAddressBook(); @@ -1907,43 +1913,47 @@ kind = TclAB_TypeFromRecord(ab, theRecord); // Find the property type (string, date, multivalue...) - if (kind == rec_person) { - thePropType = ABTypeOfProperty(ab, kABPersonRecordType, propertyRef); - } else { - thePropType = ABTypeOfProperty(ab, kABGroupRecordType, propertyRef); - } - if (thePropType == kABErrorInProperty) { - Tcl_AppendStringsToObj(resultPtr, "Unknown property '", theProperty, - "' for ", kind ? "Person":"Group" , " records", (char *) NULL); - CFRelease(propertyRef); - CFRelease(theRecord); - return TCL_ERROR; - } - + propertyRef = CFStringCreateWithCString(NULL, theProperty, kCFStringEncodingUTF8); if (propertyRef) { + if (kind == rec_person) { + thePropType = ABTypeOfProperty(ab, kABPersonRecordType, propertyRef); + } else { + thePropType = ABTypeOfProperty(ab, kABGroupRecordType, propertyRef); + } + if (thePropType == kABErrorInProperty) { + Tcl_AppendStringsToObj(resultPtr, "Unknown property '", theProperty, + "' for ", kind ? "Person":"Group" , " records", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + if (setIt) { if (!TclAB_SetValue(interp, theRecord, propertyRef, theNewVal, thePropType)) { Tcl_AppendStringsToObj(resultPtr, "Couldn't set value for '", theProperty, "' property", (char *) NULL); - CFRelease(propertyRef); - return TCL_ERROR; + result = TCL_ERROR; } } else { valueRef = ABRecordCopyValue(theRecord, propertyRef); if (!valueRef) { Tcl_AppendStringsToObj(resultPtr, "Couldn't get value of '", theProperty, "' for this record", (char *) NULL); - CFRelease(propertyRef); - return TCL_ERROR; - } - TclAB_GetValue(interp, ab, propertyRef, valueRef, 0, kind); - CFRelease(valueRef); + result = TCL_ERROR; + } else { + TclAB_GetValue(interp, ab, propertyRef, valueRef, 0, kind); + CFRelease(valueRef); + } } CFRelease(propertyRef); + } else { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get property ref", (char *) NULL); + result = TCL_ERROR; } +DONE: CFRelease(theRecord); - return TCL_OK; + return result; } |