[Tcladdressbook-commits] Source TclAddressBookUtils.c,1.5,1.6
Status: Alpha
Brought to you by:
bdesgraupes
|
From: <bde...@us...> - 2003-12-09 20:07:20
|
Update of /cvsroot/tcladdressbook/Source
In directory sc8-pr-cvs1:/tmp/cvs-serv7832/Source
Modified Files:
TclAddressBookUtils.c
Log Message:
TclAB_RecordAndTypeFromUID, TclAB_MultiValueFromTcl
Index: TclAddressBookUtils.c
===================================================================
RCS file: /cvsroot/tcladdressbook/Source/TclAddressBookUtils.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- TclAddressBookUtils.c 9 Dec 2003 09:06:08 -0000 1.5
+++ TclAddressBookUtils.c 9 Dec 2003 20:07:12 -0000 1.6
@@ -332,9 +332,57 @@
*----------------------------------------------------------------------
*/
-Boolean TclAB_SetValue(Tcl_Interp *interp, ABRecordRef theRecord, CFStringRef inProperty,
+Boolean TclAB_SetValue(Tcl_Interp *interp, ABRecordRef inRecord, CFStringRef inProperty,
CONST84 char * inValue, ABPropertyType inPropType)
{
+ CFStringRef theValueRef = NULL;
+ int result = true;
+
+ // Remove the old value of the property
+ result = ABRecordRemoveValue(inRecord, inProperty);
+ // Create a CF reference for the new value
+ theValueRef = TclAB_MultiValueFromTcl(interp, inValue, inPropType);
+ if (theValueRef) {
+ result = ABRecordSetValue(inRecord, inProperty, theValueRef);
+ CFRelease(theValueRef);
+ }
+ return result;
+}
+
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclAB_MultiValueFromTcl --
+ *
+ * This function is invoked to build a CF object representing a value or
+ * multivalue from a Tcl variable or list.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ *
+ *----------------------------------------------------------------------
+ */
+// case kABStringProperty:
+// case kABMultiStringProperty:
+// case kABDateProperty:
+// case kABMultiDateProperty:
+// case kABIntegerProperty:
+// case kABRealProperty:
+// case kABDictionaryProperty:
+// case kABMultiDictionaryProperty:
+// case kABMultiIntegerProperty:
+// case kABMultiRealProperty:
+
+// case kABArrayProperty:
+// case kABDataProperty:
+// case kABMultiArrayProperty:
+// case kABMultiDataProperty:
+
+CFStringRef TclAB_MultiValueFromTcl(Tcl_Interp *interp, CONST84 char * inValue, ABPropertyType inPropType)
+{
CFStringRef theValue = NULL, subLabel, subKey, subValue;
ABMutableMultiValueRef multiValue;
CFMutableDictionaryRef theDict;
@@ -345,74 +393,31 @@
Tcl_Obj *elemPtr, *subElemPtr, *objPtr, *labelPtr, *dictPtr, *keyPtr, *valuePtr;
switch (inPropType) {
-
case kABStringProperty:
theValue = CFStringCreateWithCString(NULL, inValue, theEncoding);
- if (theValue) {
- result = ABRecordSetValue(theRecord, inProperty, theValue);
- CFRelease(theValue);
- }
- break;
+ return theValue;
case kABDateProperty:
theDouble = atof(inValue);
theDouble -= kCFAbsoluteTimeIntervalSince1970;
theValue = CFDateCreate(NULL, theDouble);
- if (theValue) {
- result = ABRecordSetValue(theRecord, inProperty, theValue);
- CFRelease(theValue);
- }
- break;
+ return theValue;
case kABIntegerProperty:
+ case kABRealProperty:
theSInt32 = atoi(inValue);
theValue = CFNumberCreate(NULL, kCFNumberSInt32Type, &theSInt32);
- if (theValue) {
- result = ABRecordSetValue(theRecord, inProperty, theValue);
- CFRelease(theValue);
- }
- break;
+ return theValue;
case kABMultiStringProperty:
- // Create a mutable multiValue
- multiValue = ABMultiValueCreateMutable();
- // Remove the old value of the property
- result = ABRecordRemoveValue(theRecord, inProperty);
- // Parse the new string value as a Tcl list of two elems sublists
- objPtr = Tcl_NewStringObj(inValue, strlen(inValue));
- Tcl_IncrRefCount(objPtr);
- Tcl_ListObjLength(interp, objPtr, &listLen);
- // Loop over all the sublists
- for (j = 0; j < listLen; j++) {
- elemPtr = TclLindexList(interp, objPtr, Tcl_NewIntObj(j));
- labelPtr = TclLindexList(interp, elemPtr, Tcl_NewIntObj(0));
- valuePtr = TclLindexList(interp, elemPtr, Tcl_NewIntObj(1));
-
- // Make CFStrings out of the Tcl objects
- subValue = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(valuePtr, &length), theEncoding);
- subLabel = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(labelPtr, &length), theEncoding);
- // Add the value/label pair to the multivalue
- if (subValue && subLabel) {
- result = ABMultiValueAdd(multiValue, subValue, subLabel, NULL);
- CFRelease(subValue);
- CFRelease(subLabel);
- }
- }
- Tcl_DecrRefCount(objPtr);
- // Add the multivalue property to the record
- result = ABRecordSetValue(theRecord, inProperty, multiValue);
- CFRelease(multiValue);
- break;
-
-
case kABMultiDateProperty:
+ case kABMultiIntegerProperty:
+ case kABMultiRealProperty:
// Create a mutable multiValue
multiValue = ABMultiValueCreateMutable();
- // Remove the old value of the property
- result = ABRecordRemoveValue(theRecord, inProperty);
// Parse the new string value as a Tcl list of two elems sublists
objPtr = Tcl_NewStringObj(inValue, strlen(inValue));
Tcl_IncrRefCount(objPtr);
@@ -422,10 +427,26 @@
elemPtr = TclLindexList(interp, objPtr, Tcl_NewIntObj(j));
labelPtr = TclLindexList(interp, elemPtr, Tcl_NewIntObj(0));
valuePtr = TclLindexList(interp, elemPtr, Tcl_NewIntObj(1));
+
// Make CFStrings out of the Tcl objects
- theDouble = atof(Tcl_GetStringFromObj(valuePtr, &length));
- theDouble -= kCFAbsoluteTimeIntervalSince1970;
- subValue = CFDateCreate(NULL, theDouble);
+ switch (inPropType) {
+ case kABMultiStringProperty:
+ subValue = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(valuePtr, &length), theEncoding);
+ break;
+
+ case kABMultiDateProperty:
+ theDouble = atof(Tcl_GetStringFromObj(valuePtr, &length));
+ theDouble -= kCFAbsoluteTimeIntervalSince1970;
+ subValue = CFDateCreate(NULL, theDouble);
+ break;
+
+ case kABMultiIntegerProperty:
+ case kABMultiRealProperty:
+ theSInt32 = atoi(Tcl_GetStringFromObj(valuePtr, &length));
+ subValue = CFNumberCreate(NULL, kCFNumberSInt32Type, &theSInt32);
+ break;
+
+ }
subLabel = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(labelPtr, &length), theEncoding);
// Add the value/label pair to the multivalue
if (subValue && subLabel) {
@@ -435,17 +456,13 @@
}
}
Tcl_DecrRefCount(objPtr);
- // Add the multivalue property to the record
- result = ABRecordSetValue(theRecord, inProperty, multiValue);
- CFRelease(multiValue);
- break;
+ return multiValue;
+ case kABDictionaryProperty:
case kABMultiDictionaryProperty:
// Create a mutable multiValue
multiValue = ABMultiValueCreateMutable();
- // Remove the old value of the property
- result = ABRecordRemoveValue(theRecord, inProperty);
// Parse the new string value as a Tcl list of label/dictionary pairs
objPtr = Tcl_NewStringObj(inValue, strlen(inValue));
Tcl_IncrRefCount(objPtr);
@@ -484,18 +501,13 @@
Tcl_DecrRefCount(dictPtr);
}
Tcl_DecrRefCount(objPtr);
- // Add the multivalue property to the record
- result = ABRecordSetValue(theRecord, inProperty, multiValue);
- CFRelease(multiValue);
- break;
+ return multiValue;
case kABErrorInProperty:
default:
- CFRelease(theValue);
- break;
+ return theValue;
}
- return result;
}
@@ -549,6 +561,82 @@
/*
*----------------------------------------------------------------------
*
+ * TclAB_RecordFromUID --
+ *
+ * This function is invoked to determine the record corresponding
+ * to a particular UID.
+ *
+ * Results:
+ * A boolean (true if succeeded, false otherwise)
+ *
+ * Side effects:
+ * The caller is responsible for releasing the inUID CFStringRef.
+ *
+ *----------------------------------------------------------------------
+ */
+
+Boolean TclAB_RecordFromUID(ABAddressBookRef ab, CFStringRef inUID,
+ Tcl_Obj *resultPtr, ABRecordRef * outRecordRef)
+{
+ *outRecordRef = ABCopyRecordForUniqueId(ab, inUID);
+ if (!*outRecordRef) {
+ Tcl_AppendStringsToObj(resultPtr, "Unrecognized record ID", (char *) NULL);
+ return false;
+ }
+ return true;
+}
+
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclAB_CheckGroup --
+ *
+ * This function is invoked to check the validity of an -ingroup/-fromgroup
+ * optional argument and return the corresponding record reference.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+Boolean TclAB_CheckGroup(Tcl_Interp *interp, ABAddressBookRef ab, CONST84 char * inGroupUID,
+ CONST84 char * option, ABRecordRef * outGroupRef)
+{
+ CFStringRef groupCFStr;
+ int kind;
+ Boolean result = true;
+
+ groupCFStr = CFStringCreateWithCString(NULL, inGroupUID, GetApplicationTextEncoding());
+ // Find the record corresponding to the UID
+ *outGroupRef = ABCopyRecordForUniqueId(ab, groupCFStr);
+ CFRelease(groupCFStr);
+ if (!*outGroupRef) {
+ Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "Unrecognized record ID in ", option,
+ " argument", (char *) NULL);
+ result = false;
+ } else {
+ // Is it really a group?
+ kind = TclAB_TypeFromRecord(ab, *outGroupRef);
+ if (kind != rec_group) {
+ Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "Not a group ID in ", option,
+ " argument", (char *) NULL);
+ CFRelease(groupCFStr);
+ result = false;
+ }
+ }
+
+ return result;
+}
+
+
+/*
+ *----------------------------------------------------------------------
+ *
* TclAB_ListItems --
*
* This function is invoked by the [addressbook persons] and
@@ -727,53 +815,6 @@
}
}
}
-}
-
-
-/*
- *----------------------------------------------------------------------
- *
- * TclAB_CheckGroup --
- *
- * This function is invoked to check the validity of an -ingroup/-fromgroup
- * optional argument and return the corresponding record reference.
- *
- * Results:
- * None.
- *
- * Side effects:
- * None.
- *
- *----------------------------------------------------------------------
- */
-
-Boolean TclAB_CheckGroup(Tcl_Interp *interp, ABAddressBookRef ab, CONST84 char * inGroupUID,
- CONST84 char * option, ABRecordRef * outGroupRef)
-{
- CFStringRef groupCFStr;
- int kind;
- Boolean result = true;
-
- groupCFStr = CFStringCreateWithCString(NULL, inGroupUID, GetApplicationTextEncoding());
- // Find the record corresponding to the UID
- *outGroupRef = ABCopyRecordForUniqueId(ab, groupCFStr);
- CFRelease(groupCFStr);
- if (!*outGroupRef) {
- Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "Unrecognized record ID in ", option,
- " argument", (char *) NULL);
- result = false;
- } else {
- // Is it really a group?
- kind = TclAB_TypeFromRecord(ab, *outGroupRef);
- if (kind != rec_group) {
- Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "Not a group ID in ", option,
- " argument", (char *) NULL);
- CFRelease(groupCFStr);
- result = false;
- }
- }
-
- return result;
}
|