! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: strings arrays hashtables assocs sequences cocoa.messages cocoa.classes cocoa.application cocoa kernel namespaces io.backend math cocoa.enumeration byte-arrays combinators alien.c-types core-foundation core-foundation.data ; IN: cocoa.plists GENERIC: >plist ( value -- plist ) M: number >plist ; M: t >plist ; M: f >plist ; M: string >plist ; M: byte-array >plist ; M: hashtable >plist [ [ >plist ] bi@ ] assoc-map ; M: sequence >plist [ >plist ] map ; : write-plist ( assoc path -- ) [ >plist ] [ normalize-path ] bi* 0 -> writeToFile:atomically: [ "write-plist failed" throw ] unless ; DEFER: plist> : (plist-NSString>) ( NSString -- string ) -> UTF8String ; : (plist-NSNumber>) ( NSNumber -- number ) dup -> doubleValue dup >integer = [ -> longLongValue ] [ -> doubleValue ] if ; : (plist-NSData>) ( NSData -- byte-array ) dup -> length [ -> getBytes: ] keep ; : (plist-NSArray>) ( NSArray -- vector ) [ plist> ] NSFastEnumeration-map ; : (plist-NSDictionary>) ( NSDictionary -- hashtable ) dup [ [ -> valueForKey: ] keep swap [ plist> ] bi@ 2array ] with NSFastEnumeration-map >hashtable ; : plist> ( plist -- value ) { { [ dup NSString -> isKindOfClass: c-bool> ] [ (plist-NSString>) ] } { [ dup NSNumber -> isKindOfClass: c-bool> ] [ (plist-NSNumber>) ] } { [ dup NSData -> isKindOfClass: c-bool> ] [ (plist-NSData>) ] } { [ dup NSArray -> isKindOfClass: c-bool> ] [ (plist-NSArray>) ] } { [ dup NSDictionary -> isKindOfClass: c-bool> ] [ (plist-NSDictionary>) ] } [ ] } cond ; : (read-plist) ( NSData -- id ) NSPropertyListSerialization swap kCFPropertyListImmutable f f [ -> propertyListFromData:mutabilityOption:format:errorDescription: ] keep *void* [ -> release "read-plist failed" throw ] when* ; : read-plist ( path -- assoc ) normalize-path NSData swap -> dataWithContentsOfFile: [ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;