Revision: 28507
          http://sourceforge.net/p/bibdesk/svn/28507
Author:   hofman
Date:     2024-01-01 22:41:28 +0000 (Mon, 01 Jan 2024)
Log Message:
-----------
replace CFSetApplyFunction and CFArrayApplyFunction by fast enumerations

Modified Paths:
--------------
    trunk/bibdesk/BibDocument_UI.m
    trunk/bibdesk/BibItem.m
    trunk/bibdesk/NSAttributedString_BDSKExtensions.m

Modified: trunk/bibdesk/BibDocument_UI.m
===================================================================
--- trunk/bibdesk/BibDocument_UI.m      2024-01-01 19:33:53 UTC (rev 28506)
+++ trunk/bibdesk/BibDocument_UI.m      2024-01-01 22:41:28 UTC (rev 28507)
@@ -268,31 +268,6 @@
 
 #pragma mark FVFileView
 
-typedef struct _fileViewObjectContext {
-    CFMutableArrayRef array;
-    NSString *title;
-} fileViewObjectContext;
-
-static void addFileViewObjectForURLToArray(const void *value, void *context)
-{
-    fileViewObjectContext *ctxt = context;
-    // value is BDSKLinkedFile *
-    BDSKFileViewObject *obj = [[BDSKFileViewObject alloc] 
initWithURL:[(BDSKLinkedFile *)value displayURL] string:ctxt->title];
-    CFArrayAppendValue(ctxt->array, obj);
-    [obj release];
-}
-
-static void addAllFileViewObjectsForItemToArray(const void *value, void 
*context)
-{
-    CFArrayRef allURLs = (CFArrayRef)[(BibItem *)value files];
-    if (CFArrayGetCount(allURLs)) {
-        fileViewObjectContext ctxt;
-        ctxt.array = context;
-        ctxt.title = [(BibItem *)value displayTitle];
-        CFArrayApplyFunction(allURLs, CFRangeMake(0, 
CFArrayGetCount(allURLs)), addFileViewObjectForURLToArray, &ctxt);
-    }
-}
-
 - (NSArray *)shownFiles {
     if (shownFiles == nil) {
         if ([self displaysControlView:BDSKControlViewFileSearch]) {
@@ -315,7 +290,14 @@
                         [obj release];
                     }
                 } else {
-                    CFArrayApplyFunction((CFArrayRef)selPubs, CFRangeMake(0, 
[selPubs count]), addAllFileViewObjectsForItemToArray, shownFiles);
+                    for (BibItem *pub in selPubs) {
+                        NSString *title = [pub displayTitle];
+                        for (BDSKLinkedFile *file in [pub files]) {
+                            BDSKFileViewObject *obj = [[BDSKFileViewObject 
alloc] initWithURL:[file displayURL] string:title];
+                            [shownFiles addObject:obj];
+                            [obj release];
+                        }
+                    }
                 }
             }
         }

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2024-01-01 19:33:53 UTC (rev 28506)
+++ trunk/bibdesk/BibItem.m     2024-01-01 22:41:28 UTC (rev 28507)
@@ -1516,16 +1516,6 @@
 
 #pragma mark Search support
 
-static void appendNormalizedNames(const void *value, void *context)
-{
-    BibAuthor *person = (BibAuthor *)value;
-    NSMutableString *names = (NSMutableString *)context;
-    if ([names isEqualToString:@""] == NO)
-        [names appendFormat:@"%C", (unichar)0x1E];
-    // just remove curly braces from names
-    [names appendString:[[person normalizedName] stringByRemovingCurlyBraces]];
-}
-
 - (NSString *)searchStringForField:(NSString *)field {
     if ([field isEqualToString:BDSKAllFieldsString]) {
         NSMutableString *result = [NSMutableString string];
@@ -1543,7 +1533,12 @@
             for (NSString *key in allFields) {
                 if ([key isPersonField]) {
                     NSArray *persons = [self peopleArrayForField:key];
-                    CFArrayApplyFunction((CFArrayRef)persons, CFRangeMake(0, 
[persons count]), appendNormalizedNames, result);
+                    for (BibAuthor *person in persons) {
+                        if ([result isEqualToString:@""] == NO)
+                            [result appendFormat:@"%C", (unichar)0x1E];
+                        // just remove curly braces from names
+                        [result appendString:[[person normalizedName] 
stringByRemovingCurlyBraces]];
+                    }
                 } else if ([key isIntegerField] == NO && [key isURLField] == 
NO) {
                     NSString *value = [self valueOfField:key inherit:([key 
isNoteField] == NO)];
                     if ([NSString isEmptyString:value] == NO) {
@@ -1576,7 +1571,12 @@
         return [result stringByRemovingCurlyBraces];
     } else if ([field isEqualToString:BDSKPersonString]) {
         NSMutableString *names = [NSMutableString string];
-        CFSetApplyFunction((CFSetRef)[self allPeople], appendNormalizedNames, 
names);
+        for (BibAuthor *person in [self allPeople]) {
+            if ([names isEqualToString:@""] == NO)
+                [names appendFormat:@"%C", (unichar)0x1E];
+            // just remove curly braces from names
+            [names appendString:[[person normalizedName] 
stringByRemovingCurlyBraces]];
+        }
         return names;
     } else if ([field isEqualToString:BDSKTitleString]) {
         return [[self title] stringByRemovingTeX];
@@ -1584,8 +1584,12 @@
         return [[self container] stringByRemovingTeX];
     } else if ([field isPersonField]) {
         NSMutableString *names = [NSMutableString string];
-        NSArray *persons = [self peopleArrayForField:field];
-        CFArrayApplyFunction((CFArrayRef)persons, CFRangeMake(0, [persons 
count]), appendNormalizedNames, names);
+        for (BibAuthor *person in [self peopleArrayForField:field]) {
+            if ([names isEqualToString:@""] == NO)
+                [names appendFormat:@"%C", (unichar)0x1E];
+            // just remove curly braces from names
+            [names appendString:[[person normalizedName] 
stringByRemovingCurlyBraces]];
+        }
         return names;
     } else if ([field isSingleValuedField]) {
         return [[self valueOfField:field inherit:[field isNoteField] == NO] 
stringByRemovingCurlyBraces];
@@ -2681,38 +2685,30 @@
     return attrString;
 }
 
-typedef struct _fileContext {
-    CFMutableArrayRef array;
-    BOOL isFile;
-    BOOL includeAll;
-} fileContext;
-
-static void addFilesToArray(const void *value, void *context)
-{
-    fileContext *ctxt = context;
-    BDSKLinkedFile *file = (BDSKLinkedFile *)value;
-    if ([file isFile] == ctxt->isFile && (ctxt->includeAll || [file URL] != 
nil))
-        CFArrayAppendValue(ctxt->array, value);
-}
-
 - (NSArray *)localFiles {
     NSMutableArray *localFiles = [NSMutableArray array];
-    fileContext ctxt = {(CFMutableArrayRef)localFiles, YES, YES};
-    CFArrayApplyFunction((CFArrayRef)files, CFRangeMake(0, [files count]), 
addFilesToArray, &ctxt);
+    for (BDSKLinkedFile *file in files) {
+        if ([file isFile])
+            [localFiles addObject:file];
+    }
     return localFiles;
 }
 
 - (NSArray *)existingLocalFiles {
     NSMutableArray *localFiles = [NSMutableArray array];
-    fileContext ctxt = {(CFMutableArrayRef)localFiles, YES, NO};
-    CFArrayApplyFunction((CFArrayRef)files, CFRangeMake(0, [files count]), 
addFilesToArray, &ctxt);
+    for (BDSKLinkedFile *file in files) {
+        if ([file isFile] && [file URL] != nil)
+            [localFiles addObject:file];
+    }
     return localFiles;
 }
 
 - (NSArray *)remoteURLs {
     NSMutableArray *remoteURLs = [NSMutableArray array];
-    fileContext ctxt = {(CFMutableArrayRef)remoteURLs, NO, YES};
-    CFArrayApplyFunction((CFArrayRef)files, CFRangeMake(0, [files count]), 
addFilesToArray, &ctxt);
+    for (BDSKLinkedFile *file in files) {
+        if ([file isFile] == NO)
+            [remoteURLs addObject:file];
+    }
     return remoteURLs;
 }
 
@@ -3079,27 +3075,16 @@
 
 #pragma mark File conversion
 
-typedef struct _conversionContext {
-    BibItem *publication;
-    BOOL removeField;
-    NSMutableArray *messages;
-    NSInteger numberOfAddedFiles;
-    NSInteger numberOfRemovedFields;
-} conversionContext;
-
-static void addURLForFieldToArrayIfNotNil(const void *key, void *context)
-{
-    conversionContext *ctxt = (conversionContext *)context;
-    BibItem *self = ctxt->publication;
-    
+- (NSInteger)addLinkedFilesForField:(NSString *)field messages:(NSMutableArray 
*)messages removeField:(BOOL)removeField numberOfRemovedFields:(NSInteger 
*)numberOfRemovedFields {
     // this function is called for all local & remote URL fields, whether or 
not they have a value
-    NSURL *urlValue = [self URLForField:(id)key];
+    NSUInteger numberOfAddedFiles = 0;
+    NSURL *urlValue = [self URLForField:field];
     if (urlValue) {
         // see if this file was converted previously to avoid duplication
         NSArray *currentURLs = [self 
valueForKeyPath:@"files.URL.absoluteString.lowercaseString"];
         NSString *urlString = [urlValue absoluteString];
         BOOL converted = NO;
-        for (BDSKLinkedFile *file in self->files) {
+        for (BDSKLinkedFile *file in files) {
             if ([urlValue isFileURL] == [file isFile] && [[[file URL] 
absoluteString] isCaseInsensitiveEqual:urlString]) {
                 converted = YES;
                 break;
@@ -3111,30 +3096,31 @@
             if (fileURL == nil) {
                 // @@ this error message is lame
                 NSDictionary *message = [[NSDictionary alloc] 
initWithObjectsAndKeys:urlValue, @"URL", NSLocalizedString(@"File or URL not 
found", @""), @"error", nil];
-                [ctxt->messages addObject:message];
+                [messages addObject:message];
                 [message release];
             } else if ([currentURLs containsObject:fileURL] == NO) {
                 // checked again for containment, as fileURL may not be 
exactly the same as urlValue, e.g. an extra slash at the end for a folder
-                [self->files addObject:file];
+                [files addObject:file];
                 converted = YES;
-                (ctxt->numberOfAddedFiles)++;
+                numberOfAddedFiles++;
             }
             [file release];
         }
         
         // clear the old URL field if the file was converted (now or 
previously)
-        if (ctxt->removeField && converted) {
-            [self setField:(id)key toValue:nil];
-            (ctxt->numberOfRemovedFields)++;
+        if (removeField && converted) {
+            [self setField:field toValue:nil];
+            (*numberOfRemovedFields)++;
         }
     } else {
-        NSString *stringValue = [self valueOfField:(id)key inherit:NO];
+        NSString *stringValue = [self valueOfField:field inherit:NO];
         if (NO == [NSString isEmptyString:stringValue]) {
             NSDictionary *message = [[NSDictionary alloc] 
initWithObjectsAndKeys:[NSString stringWithFormat:NSLocalizedString(@"URL 
\"%@\" is invalid", @""), stringValue], @"error", nil];
-            [ctxt->messages addObject:message];
+            [messages addObject:message];
             [message release];
         }
     }
+    return numberOfAddedFiles;
 }
 
 - (BOOL)migrateFilesWithRemoveOptions:(BDSKRemoveFieldsOption)removeMask 
numberOfAddedFiles:(NSInteger *)numberOfAddedFiles 
numberOfRemovedFields:(NSInteger *)numberOfRemovedFields error:(NSError 
**)outError
@@ -3141,21 +3127,19 @@
 {
     NSInteger addedLocalFiles = 0;
     NSMutableArray *messages = [NSMutableArray new];
-    conversionContext context;
-    context.publication = self;
-    context.messages = messages;
-    context.numberOfAddedFiles = 0;
-    context.numberOfRemovedFields = 0;
-    
     NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
-    CFArrayRef fieldsArray = (CFArrayRef)[sud 
stringArrayForKey:BDSKLocalFileFieldsKey];
-    context.removeField = (removeMask & BDSKRemoveLocalFileFields) != 0;
-    CFArrayApplyFunction(fieldsArray, CFRangeMake(0, 
CFArrayGetCount(fieldsArray)), addURLForFieldToArrayIfNotNil, &context);
-    addedLocalFiles = context.numberOfAddedFiles;
+    BOOL removeField = (removeMask & BDSKRemoveLocalFileFields) != 0;
+    NSInteger addedFiles = 0;
+    NSInteger removedFields = 0;
+    for (NSString *field in [sud stringArrayForKey:BDSKLocalFileFieldsKey]) {
+        addedFiles += [self addLinkedFilesForField:field messages:messages 
removeField:removeField numberOfRemovedFields:&removedFields];
+    }
+    addedLocalFiles = addedFiles;
     
-    context.removeField = (removeMask & BDSKRemoveRemoteURLFields) != 0;
-    fieldsArray = (CFArrayRef)[sud stringArrayForKey:BDSKRemoteURLFieldsKey];
-    CFArrayApplyFunction(fieldsArray, CFRangeMake(0, 
CFArrayGetCount(fieldsArray)), addURLForFieldToArrayIfNotNil, &context);
+    removeField = (removeMask & BDSKRemoveRemoteURLFields) != 0;
+    for (NSString *field in [sud stringArrayForKey:BDSKRemoteURLFieldsKey]) {
+        addedFiles += [self addLinkedFilesForField:field messages:messages 
removeField:removeField numberOfRemovedFields:&removedFields];
+    }
     
     NSUInteger failureCount = [messages count];
 
@@ -3168,13 +3152,13 @@
     
     // Cause the file content search index (if any) to update, since we 
bypassed the normal insert mechanism where this is typically handled.  The 
date-modified will only be set if fields are removed, since the applier 
function calls setField:toValue:.  
     // @@ Calling 
migrateFilesWithRemoveOptions:numberOfAddedFiles:numberOfRemovedFields:error: 
from -createFiles will also cause date-modified to be set.
-    if (context.numberOfAddedFiles > 0)
+    if (addedFiles > 0)
         [self noteFilesChanged:addedLocalFiles > 0];
     
     if (numberOfAddedFiles)
-        *numberOfAddedFiles = context.numberOfAddedFiles;
+        *numberOfAddedFiles = addedFiles;
     if (numberOfRemovedFields)
-        *numberOfRemovedFields = context.numberOfRemovedFields;
+        *numberOfRemovedFields = removedFields;
     
     return 0 == failureCount;
 }

Modified: trunk/bibdesk/NSAttributedString_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSAttributedString_BDSKExtensions.m   2024-01-01 19:33:53 UTC 
(rev 28506)
+++ trunk/bibdesk/NSAttributedString_BDSKExtensions.m   2024-01-01 22:41:28 UTC 
(rev 28507)
@@ -134,14 +134,7 @@
     *rangePtr = range;
 }
 
-static void BDSKApplyAttributesToString(const void *value, void *context)
-{
-    NSDictionary *dict = (void *)value;
-    NSMutableAttributedString *mas = context;
-    [mas addAttributes:dict range:[[dict objectForKey:BDSKRangeKey] 
rangeValue]];    
-}
 
-
 @implementation NSAttributedString (BDSKExtensions)
 
 - (id)initWithString:(NSString *)string attributeName:(NSString 
*)attributeName attributeValue:(id)attributeValue{
@@ -176,7 +169,8 @@
         mas = [[NSMutableAttributedString alloc] initWithString:mutableString 
attributes:attributes]; 
 
         // now apply the previously determined attributes and ranges to the 
attributed string
-        CFArrayApplyFunction((CFArrayRef)attributeDictionaries, CFRangeMake(0, 
numberOfDictionaries), BDSKApplyAttributesToString, mas);
+        for (NSDictionary *dict in attributeDictionaries)
+            [mas addAttributes:dict range:[[dict objectForKey:BDSKRangeKey] 
rangeValue]];
         
         // not all of the braces were deleted when parsing the commands
         [[mas mutableString] deleteCharactersInCharacterSet:[NSCharacterSet 
curlyBraceCharacterSet]];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to