RSExport RSTypeRef RSDictionaryGetValueForKeys(RSDictionaryRef dictionary, RSArrayRef keys) { if (nil == dictionary || !keys || !RSArrayGetCount(keys)) return nil; RSTypeRef rst = nil; RSClassRef dictClass = RSClassGetWithUTF8String("RSDictionary"); RSUInteger cnt = RSArrayGetCount(keys); RSDictionaryRef tmp = dictionary; for (RSUInteger idx = 0; idx < cnt; idx++) { if (RSInstanceIsMemberOfClass(tmp, dictClass)) { tmp = RSDictionaryGetValue(tmp, RSArrayObjectAtIndex(keys, idx)); } else { return nil; } } return rst = tmp; }
static void __RSFileManagerContentContextUpdatePrefix(__RSFileManagerContentsContext* context) { RSMutableStringRef fullObj = nil; if (context->currentPrefix) RSRelease(context->currentPrefix); context->currentPrefix = nil; if (likely(context->currentPrefix == nil)) { RSIndex cnt = 0; if (likely(context->dirStack) && (cnt = RSArrayGetCount(context->dirStack))) { BOOL prefix = NO, suffix = NO; RSTypeRef objInArray = RSArrayObjectAtIndex(context->dirStack, 0); fullObj = RSMutableCopy(RSAllocatorSystemDefault, objInArray); for (RSIndex idx = 1; idx < cnt; idx++) { __RSRuntimeMemoryBarrier(); if (NO == RSStringHasSuffix(fullObj, RSSTR("/"))) suffix = YES; objInArray = RSArrayObjectAtIndex(context->dirStack, idx); if (NO == RSStringHasPrefix(fullObj, RSSTR("/"))) prefix = YES; if (suffix || prefix) RSStringAppendCString(fullObj, "/", RSStringEncodingASCII); RSStringAppendString(fullObj, objInArray); suffix = prefix = NO; } } context->currentPrefix = fullObj; } }
RSExport RSIndex RSQueueGetCount(RSQueueRef queue) { if (queue == nil) return 0; __RSGenericValidInstance(queue, _RSQueueTypeID); if (isAtom(queue)) RSSpinLockLock(&queue->_lock); RSIndex cnt = RSArrayGetCount(queue->_queueCore); if (isAtom(queue)) RSSpinLockUnlock(&queue->_lock); return cnt; }
static BOOL __RSProcessInfoUpdateKeys(RSProcessInfoRef processInfo, RSArrayRef lookupKeys, RSArrayRef description, RSStringRef key) { if (key == nil || lookupKeys == nil) return NO; if (description == nil) description = lookupKeys; const RSUInteger keyCount = RSArrayGetCount(lookupKeys); if (keyCount > RSArrayGetCount(description)) return NO; RSUInteger successCount = 0; RSMutableDictionaryRef dict = RSDictionaryCreateMutable(RSAllocatorSystemDefault, 0, RSDictionaryRSTypeContext); if (dict) { for (RSUInteger idx = 0; idx < keyCount; idx++) { successCount += __RSProcessInfoUpdate(dict, RSArrayObjectAtIndex(lookupKeys, idx), RSArrayObjectAtIndex(description, idx)); } __RSProcessInfoSetObjectForKey(processInfo, key, dict); RSRelease(dict); } return successCount == keyCount; }
static RSTypeRef __RSErrorRSCoreFoundationCallBack(RSErrorRef err, RSStringRef key) { if (!RSEqual(key, RSErrorDescriptionKey) && !RSEqual(key, RSErrorLocalizedFailureReasonKey)) return nil; RSStringRef errStr = nil; struct __RSErrorPrivateFormatTable cFormat = __RSErrorDomainRSCoreFoundationGetCStringWithCode(RSErrorGetCode(err)); if (cFormat.argsCnt) { RSStringRef format = RSStringCreateWithCString(RSAllocatorSystemDefault, cFormat.format, RSStringEncodingUTF8); RSArrayRef objects = RSDictionaryGetValue(__RSErrorGetUserInfo(err), RSErrorTargetKey); RSTypeRef object[3] = {nil}; RSIndex minCnt = 0; if (objects) { minCnt = min(cFormat.argsCnt, RSArrayGetCount(objects)); switch (minCnt) { case 1: object[0] = RSArrayObjectAtIndex(objects, 0); errStr = RSStringCreateWithFormat(RSAllocatorSystemDefault, format, object[0]); break; case 2: object[0] = RSArrayObjectAtIndex(objects, 0); object[1] = RSArrayObjectAtIndex(objects, 1); errStr = RSStringCreateWithFormat(RSAllocatorSystemDefault, format, object[0], object[1]); break; case 3: object[0] = RSArrayObjectAtIndex(objects, 0); object[1] = RSArrayObjectAtIndex(objects, 1); object[2] = RSArrayObjectAtIndex(objects, 2); errStr = RSStringCreateWithFormat(RSAllocatorSystemDefault, format, object[0], object[1], object[2]); break; default: HALTWithError(RSInvalidArgumentException, "the formate is too long to support"); break; } } for (RSIndex idx = 0; idx < minCnt; idx++) { RSRelease(object[idx]); } RSRelease(format); } else { errStr = RSStringCreateWithCString(RSAllocatorSystemDefault, cFormat.format, RSStringEncodingUTF8); } return errStr; }
RSExport void RSDictionarySetValueForKeys(RSMutableDictionaryRef dictionary, RSArrayRef keys, RSTypeRef value) { if (nil == dictionary || nil == keys || !RSArrayGetCount(keys)) return; RSClassRef dictClass = RSClassGetWithUTF8String("RSDictionary"); RSUInteger cnt = RSArrayGetCount(keys); if (cnt < 2) { return RSDictionarySetValue(dictionary, RSArrayObjectAtIndex(keys, 0), value); } cnt --; RSMutableDictionaryRef tmp = dictionary, tmp1 = tmp; for (RSUInteger idx = 0; idx < cnt; idx++) { if (RSInstanceIsMemberOfClass(tmp, dictClass)) { tmp1 = (RSMutableDictionaryRef)RSDictionaryGetValue(tmp, RSArrayObjectAtIndex(keys, idx)); if (!tmp1) { tmp1 = RSDictionaryCreateMutable(RSAllocatorSystemDefault, 0, RSDictionaryRSTypeContext); RSDictionarySetValue(tmp, RSArrayObjectAtIndex(keys, idx), tmp1); RSRelease(tmp1); } tmp = tmp1; } else { return ; } } return RSDictionarySetValue(tmp, RSArrayLastObject(keys), value); }
RSInline void __RSQueueAddToObject(RSQueueRef queue, RSTypeRef obj) { BOOL shouldLocked = isAtom(queue); if (shouldLocked) RSSpinLockLock(&queue->_lock); if (queue->_capacity == 0) { RSArrayAddObject(queue->_queueCore, obj); if (shouldLocked) RSSpinLockUnlock(&queue->_lock); return; } RSIndex cnt = RSArrayGetCount(queue->_queueCore); if (cnt < queue->_capacity) RSArrayAddObject(queue->_queueCore, obj); else __RSCLog(RSLogLevelNotice, "RSQueue %p is full!\n", queue); if (shouldLocked) RSSpinLockUnlock(&queue->_lock); }
RSInline RSTypeRef __RSQueueGetObjectFromQueue(RSQueueRef queue, BOOL remove) { BOOL shouldLocked = isAtom(queue); if (shouldLocked) RSSpinLockLock(&queue->_lock); RSIndex cnt = RSArrayGetCount(queue->_queueCore); if (cnt == 0) { if (shouldLocked) RSSpinLockUnlock(&queue->_lock); return nil; } RSTypeRef obj = RSArrayObjectAtIndex(queue->_queueCore, 0); if (remove) { RSRetain(obj); RSArrayRemoveObjectAtIndex(queue->_queueCore, 0); } if (shouldLocked) RSSpinLockUnlock(&queue->_lock); return obj; }
// RSCoreFoundation // // Created by closure on 1/29/14. // Copyright (c) 2014 RetVal. All rights reserved. // #include <RSCoreFoundation/RSFunctional.h> #include <RSCoreFoundation/RSKVBucket.h> #include <RSCoreFoundation/RSRuntime.h> #pragma mark - #pragma mark Apply API Group static RSTypeRef __RSApplyArray(RSArrayRef coll, RSRange range, void (^fn)(RSTypeRef obj)) { if (range.location == -1) { range = RSMakeRange(0, RSArrayGetCount(coll)); } RSArrayApplyBlock(coll, range, ^(const void *value, RSUInteger idx, BOOL *isStop) { fn(value); }); return nil; } static RSTypeRef __RSApplyList(RSListRef coll, RSRange range, void (^fn)(RSTypeRef obj)) { if (range.location == -1) { range = RSMakeRange(0, RSListGetCount(coll)); } RSListApplyBlock(coll, range, ^(RSTypeRef value, BOOL *stop) { fn(value); }); return nil;