[Tcladdressbook-commits] Source TclAddressBookUtils.c,1.6,1.7
Status: Alpha
Brought to you by:
bdesgraupes
|
From: <bde...@us...> - 2003-12-13 13:23:17
|
Update of /cvsroot/tcladdressbook/Source
In directory sc8-pr-cvs1:/tmp/cvs-serv25280/Source
Modified Files:
TclAddressBookUtils.c
Log Message:
Return localized labels. Better checking for NULLs.
Index: TclAddressBookUtils.c
===================================================================
RCS file: /cvsroot/tcladdressbook/Source/TclAddressBookUtils.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- TclAddressBookUtils.c 9 Dec 2003 20:07:12 -0000 1.6
+++ TclAddressBookUtils.c 13 Dec 2003 13:23:13 -0000 1.7
@@ -1,7 +1,7 @@
/*
* File : "TclAddressBookUtils.c"
* Created: 2003-12-05 10:08:03
- * Last modification: 2003-12-09 09:19:04
+ * Last modification: 2003-12-13 07:44:50
* Author: Bernard Desgraupes
* e-mail: <bde...@ea...>
*
@@ -39,7 +39,8 @@
*----------------------------------------------------------------------
*/
-void TclAB_GetAllValues(Tcl_Interp *interp, ABAddressBookRef ab,
+void
+TclAB_GetAllValues(Tcl_Interp *interp, ABAddressBookRef ab,
ABRecordRef inRecord, CONST84 char * var,
int inRecordType)
{
@@ -89,12 +90,18 @@
*
*----------------------------------------------------------------------
*/
+// Multi dictionary
+// { {label1 { {key11 value11} {key12 value12} {key13 value13} } } {label2 { {key21 value2} {key22 value22} {key23 value23} } } }
+// Multi string / multi date
+// {label1 value1} {label2 value2} {label3 value3}
-void TclAB_GetValue(Tcl_Interp *interp, ABAddressBookRef ab,
+void
+TclAB_GetValue(Tcl_Interp *interp, ABAddressBookRef ab,
CFStringRef inProperty, CFStringRef inValue,
CONST84 char * var, int inRecordType)
{
CFStringRef theValue, subValue, subLabel;
+ CFStringRef localizedStr;
CFAbsoluteTime theAbsTime;
ABPropertyType thePropType;
ABMutableMultiValueRef multiValue;
@@ -108,6 +115,7 @@
Tcl_DString ds;
TextEncoding theEncoding = GetApplicationTextEncoding();
+
if (CFStringGetCString(inProperty, thePropName, sizeof(thePropName), theEncoding)) {
if (inRecordType == rec_person) {
@@ -154,30 +162,26 @@
case kABMultiStringProperty:
- // Create a multiValue and populate it with the items in the property
- multiValue = ABMultiValueCreateMutable();
multiValue = (ABMutableMultiValueRef) theValue;
-
mvCount = ABMultiValueCount(multiValue);
Tcl_DStringInit(&ds);
for (j = 0; j < mvCount; j++) {
subLabel = ABMultiValueCopyLabelAtIndex(multiValue, j);
subValue = ABMultiValueCopyValueAtIndex(multiValue, j);
- if (subValue
- && CFStringGetCString(subLabel, theLabelStr, sizeof(theLabelStr), theEncoding)
+ if (subLabel && subValue) {
+ localizedStr = ABCopyLocalizedPropertyOrLabel(subLabel);
+ if ( CFStringGetCString(localizedStr, theLabelStr, sizeof(theLabelStr), theEncoding)
&& CFStringGetCString(subValue, theSubStr, sizeof(theSubStr), theEncoding)) {
- if (mvCount > 1) {
Tcl_DStringStartSublist(&ds);
- }
- Tcl_DStringAppendElement(&ds, theLabelStr);
- Tcl_DStringAppendElement(&ds, theSubStr);
- CFRelease(subValue);
- CFRelease(subLabel);
- if (mvCount > 1) {
+ Tcl_DStringAppendElement(&ds, theLabelStr);
+ Tcl_DStringAppendElement(&ds, theSubStr);
Tcl_DStringEndSublist(&ds);
- }
+ }
+ CFRelease(localizedStr);
}
+ if (subLabel) CFRelease(subLabel);
+ if (subValue) CFRelease(subValue);
}
TclAB_SetArrayOrResult(interp, var, thePropName, ds.string);
Tcl_DStringFree(&ds);
@@ -186,33 +190,29 @@
case kABMultiDateProperty:
- // Create a multiValue and populate it with the items in the property
- multiValue = ABMultiValueCreateMutable();
multiValue = (ABMutableMultiValueRef) theValue;
-
mvCount = ABMultiValueCount(multiValue);
Tcl_DStringInit(&ds);
for (j = 0; j < mvCount; j++) {
subLabel = ABMultiValueCopyLabelAtIndex(multiValue, j);
subValue = ABMultiValueCopyValueAtIndex(multiValue, j);
- if (subValue
- && CFStringGetCString(subLabel, theLabelStr, sizeof(theLabelStr), theEncoding)) {
- if (mvCount > 1) {
+ if (subLabel && subValue) {
+ localizedStr = ABCopyLocalizedPropertyOrLabel(subLabel);
+ if (CFStringGetCString(localizedStr, theLabelStr, sizeof(theLabelStr), theEncoding)) {
Tcl_DStringStartSublist(&ds);
- }
- theAbsTime = CFDateGetAbsoluteTime(subValue);
- // See comment in kABDateProperty case above
- theAbsTime += kCFAbsoluteTimeIntervalSince1970;
- sprintf(theSubStr, "%.0f", theAbsTime);
- Tcl_DStringAppendElement(&ds, theLabelStr);
- Tcl_DStringAppendElement(&ds, theSubStr);
- CFRelease(subValue);
- CFRelease(subLabel);
- if (mvCount > 1) {
+ theAbsTime = CFDateGetAbsoluteTime(subValue);
+ // See comment in kABDateProperty case above
+ theAbsTime += kCFAbsoluteTimeIntervalSince1970;
+ sprintf(theSubStr, "%.0f", theAbsTime);
+ Tcl_DStringAppendElement(&ds, theLabelStr);
+ Tcl_DStringAppendElement(&ds, theSubStr);
Tcl_DStringEndSublist(&ds);
- }
+ }
+ CFRelease(localizedStr);
}
+ if (subLabel) CFRelease(subLabel);
+ if (subValue) CFRelease(subValue);
}
TclAB_SetArrayOrResult(interp, var, thePropName, ds.string);
Tcl_DStringFree(&ds);
@@ -221,16 +221,12 @@
case kABMultiDictionaryProperty:
- // Each item is made of a label and a CFDictionaryRef: the
- // dictionary is represented as a list of two elems lists (key/value).
-
- // Create a multiValue
- multiValue = ABMultiValueCreateMutable();
multiValue = (ABMutableMultiValueRef) theValue;
-
mvCount = ABMultiValueCount(multiValue);
Tcl_DStringInit(&ds);
+ // Each item is made of a label and a CFDictionaryRef: the
+ // dictionary is represented as a list of two elems lists (key/value).
for (j = 0; j < mvCount; j++) {
// Foreach CFDictionaryRef
CFDictionaryRef theDict;
@@ -238,45 +234,54 @@
CFStringRef * theKeys;
CFStringRef * theVals;
+// Tcl_DStringStartSublist(&ds);
// Get the label
subLabel = ABMultiValueCopyLabelAtIndex(multiValue, j);
- CFStringGetCString(subLabel, theLabelStr, sizeof(theLabelStr), theEncoding);
- // 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 ((NULL != theKeys) && (NULL != theVals)) {
- Tcl_DStringStartSublist(&ds);
- Tcl_DStringAppendElement(&ds, theLabelStr);
- Tcl_DStringStartSublist(&ds);
- // Fill the keys and values arrays from this dictionary
- CFDictionaryGetKeysAndValues(theDict, theKeys, theVals);
-
- for (k = 0; k < dictCount; k++) {
- // Make a two items list for each key/value pair
- if (CFStringGetCString(theKeys[k], theStr, sizeof(theStr), theEncoding)
- && CFStringGetCString(theVals[k], theSubStr, sizeof(theSubStr), theEncoding)) {
+ if (subLabel) {
+ localizedStr = ABCopyLocalizedPropertyOrLabel(subLabel);
+ CFRelease(subLabel);
+ if (CFStringGetCString(localizedStr, theLabelStr, sizeof(theLabelStr), theEncoding)) {
+ // 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, theStr);
- Tcl_DStringAppendElement(&ds, theSubStr);
+ 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++) {
+ // Make a two items list for each key/value pair
+ if (CFStringGetCString(theKeys[k], theStr, sizeof(theStr), theEncoding)
+ && CFStringGetCString(theVals[k], theSubStr, sizeof(theSubStr), theEncoding)) {
+ Tcl_DStringStartSublist(&ds);
+ Tcl_DStringAppendElement(&ds, theStr);
+ Tcl_DStringAppendElement(&ds, theSubStr);
+ Tcl_DStringEndSublist(&ds);
+ }
+ }
Tcl_DStringEndSublist(&ds);
- }
- }
- Tcl_DStringEndSublist(&ds);
- Tcl_DStringEndSublist(&ds);
- DisposePtr(theKeys);
- DisposePtr(theVals);
- }
- CFRelease(theDict);
- CFRelease(subLabel);
+ Tcl_DStringEndSublist(&ds);
+ DisposePtr(theKeys);
+ DisposePtr(theVals);
+ } else {
+ // "Could not allocate memory for dictionary"
+ }
+ if (theDict) CFRelease(theDict);
+ }
+ CFRelease(localizedStr);
+ }
+// Tcl_DStringEndSublist(&ds);
}
TclAB_SetArrayOrResult(interp, var, thePropName, ds.string);
Tcl_DStringFree(&ds);
- // Don't CFRelease: each dict is released one after the other
- // CFRelease(multiValue);
+ // Don't CFRelease the multiValue: each dict is released one after the other
+ CFRelease(multiValue);
break;
@@ -307,7 +312,8 @@
*----------------------------------------------------------------------
*/
-void TclAB_SetArrayOrResult(Tcl_Interp *interp, CONST84 char * inVar, char * inPropName, char * inStr)
+void
+TclAB_SetArrayOrResult(Tcl_Interp *interp, CONST84 char * inVar, char * inPropName, char * inStr)
{
if (inVar) {
Tcl_SetVar2(interp, inVar, inPropName, inStr, 0);
@@ -332,7 +338,8 @@
*----------------------------------------------------------------------
*/
-Boolean TclAB_SetValue(Tcl_Interp *interp, ABRecordRef inRecord, CFStringRef inProperty,
+Boolean
+TclAB_SetValue(Tcl_Interp *interp, ABRecordRef inRecord, CFStringRef inProperty,
CONST84 char * inValue, ABPropertyType inPropType)
{
CFStringRef theValueRef = NULL;
@@ -381,7 +388,8 @@
// case kABMultiArrayProperty:
// case kABMultiDataProperty:
-CFStringRef TclAB_MultiValueFromTcl(Tcl_Interp *interp, CONST84 char * inValue, ABPropertyType inPropType)
+CFTypeRef
+TclAB_MultiValueFromTcl(Tcl_Interp *interp, CONST84 char * inValue, ABPropertyType inPropType)
{
CFStringRef theValue = NULL, subLabel, subKey, subValue;
ABMutableMultiValueRef multiValue;
@@ -451,9 +459,9 @@
// Add the value/label pair to the multivalue
if (subValue && subLabel) {
result = ABMultiValueAdd(multiValue, subValue, subLabel, NULL);
- CFRelease(subValue);
- CFRelease(subLabel);
}
+ if (subValue) CFRelease(subValue);
+ if (subLabel) CFRelease(subLabel);
}
Tcl_DecrRefCount(objPtr);
return multiValue;
@@ -479,25 +487,29 @@
Tcl_ListObjLength(interp, dictPtr, &subListLen);
// Create and populate a CFDictionary with the key/value pairs
theDict = CFDictionaryCreateMutable(NULL, subListLen, NULL, NULL);
-
- for (k = 0; k < subListLen; k++) {
- subElemPtr = TclLindexList(interp, dictPtr, Tcl_NewIntObj(k));
- keyPtr = TclLindexList(interp, subElemPtr, Tcl_NewIntObj(0));
- valuePtr = TclLindexList(interp, subElemPtr, Tcl_NewIntObj(1));
- // Make CFStrings out of the Tcl objects
- subKey = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(keyPtr, &length), theEncoding);
- subValue = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(valuePtr, &length), theEncoding);
- // Add the value/label pair to the dictionary
- if (subKey && subValue) {
- CFDictionaryAddValue(theDict, subKey, subValue);
- CFRelease(subKey);
- CFRelease(subValue);
+ if (theDict) {
+ // Loop over all the elements of the flat dict list
+ for (k = 0; k < subListLen; k++) {
+ subElemPtr = TclLindexList(interp, dictPtr, Tcl_NewIntObj(k));
+ keyPtr = TclLindexList(interp, subElemPtr, Tcl_NewIntObj(0));
+ valuePtr = TclLindexList(interp, subElemPtr, Tcl_NewIntObj(1));
+ // Make CFStrings out of the Tcl objects
+ subKey = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(keyPtr, &length), theEncoding);
+ subValue = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(valuePtr, &length), theEncoding);
+ // Add the value/label pair to the dictionary
+ if (subKey && subValue) {
+ CFDictionaryAddValue(theDict, subKey, subValue);
+ }
+ if (subKey) CFRelease(subKey);
+ if (subValue) CFRelease(subValue);
+ }
+ subLabel = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(labelPtr, &length), theEncoding);
+ if (subLabel) {
+ result = ABMultiValueAdd(multiValue, theDict, subLabel, NULL);
+ CFRelease(subLabel);
}
- }
- subLabel = CFStringCreateWithCString(NULL, Tcl_GetStringFromObj(labelPtr, &length), theEncoding);
- result = ABMultiValueAdd(multiValue, theDict, subLabel, NULL);
- CFRelease(theDict);
- CFRelease(subLabel);
+ CFRelease(theDict);
+ }
Tcl_DecrRefCount(dictPtr);
}
Tcl_DecrRefCount(objPtr);
@@ -528,7 +540,8 @@
*----------------------------------------------------------------------
*/
-int TclAB_TypeFromRecord(ABAddressBookRef ab, ABRecordRef inRecord)
+int
+TclAB_TypeFromRecord(ABAddressBookRef ab, ABRecordRef inRecord)
{
CFStringRef typeRef;
int kind;
@@ -575,7 +588,8 @@
*----------------------------------------------------------------------
*/
-Boolean TclAB_RecordFromUID(ABAddressBookRef ab, CFStringRef inUID,
+Boolean
+TclAB_RecordFromUID(ABAddressBookRef ab, CFStringRef inUID,
Tcl_Obj *resultPtr, ABRecordRef * outRecordRef)
{
*outRecordRef = ABCopyRecordForUniqueId(ab, inUID);
@@ -604,7 +618,8 @@
*----------------------------------------------------------------------
*/
-Boolean TclAB_CheckGroup(Tcl_Interp *interp, ABAddressBookRef ab, CONST84 char * inGroupUID,
+Boolean
+TclAB_CheckGroup(Tcl_Interp *interp, ABAddressBookRef ab, CONST84 char * inGroupUID,
CONST84 char * option, ABRecordRef * outGroupRef)
{
CFStringRef groupCFStr;
@@ -752,7 +767,8 @@
*----------------------------------------------------------------------
*/
-void TclAB_ResultListFromCFArray(Tcl_Interp *interp, Tcl_Obj *resultPtr,
+void
+TclAB_ResultListFromCFArray(Tcl_Interp *interp, Tcl_Obj *resultPtr,
CFArrayRef allElements, Boolean onlyID,
int inRecordType)
{
|