1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: strings arrays hashtables assocs sequences
4 cocoa.messages cocoa.classes cocoa.application cocoa kernel
5 namespaces io.backend math cocoa.enumeration byte-arrays
6 combinators alien.c-types core-foundation core-foundation.data ;
9 GENERIC: >plist ( value -- plist )
22 [ [ >plist ] bi@ ] assoc-map <NSDictionary> ;
24 [ >plist ] map <NSArray> ;
26 : write-plist ( assoc path -- )
27 [ >plist ] [ normalize-path <NSString> ] bi* 0
28 -> writeToFile:atomically:
29 [ "write-plist failed" throw ] unless ;
33 : (plist-NSString>) ( NSString -- string )
36 : (plist-NSNumber>) ( NSNumber -- number )
37 dup -> doubleValue dup >integer =
39 [ -> doubleValue ] if ;
41 : (plist-NSData>) ( NSData -- byte-array )
42 dup -> length <byte-array> [ -> getBytes: ] keep ;
44 : (plist-NSArray>) ( NSArray -- vector )
45 [ plist> ] NSFastEnumeration-map ;
47 : (plist-NSDictionary>) ( NSDictionary -- hashtable )
48 dup [ [ -> valueForKey: ] keep swap [ plist> ] bi@ 2array ] with
49 NSFastEnumeration-map >hashtable ;
51 : plist> ( plist -- value )
53 { [ dup NSString -> isKindOfClass: c-bool> ] [ (plist-NSString>) ] }
54 { [ dup NSNumber -> isKindOfClass: c-bool> ] [ (plist-NSNumber>) ] }
55 { [ dup NSData -> isKindOfClass: c-bool> ] [ (plist-NSData>) ] }
56 { [ dup NSArray -> isKindOfClass: c-bool> ] [ (plist-NSArray>) ] }
57 { [ dup NSDictionary -> isKindOfClass: c-bool> ] [ (plist-NSDictionary>) ] }
61 : (read-plist) ( NSData -- id )
62 NSPropertyListSerialization swap kCFPropertyListImmutable f f <void*>
63 [ -> propertyListFromData:mutabilityOption:format:errorDescription: ] keep
64 *void* [ -> release "read-plist failed" throw ] when* ;
66 : read-plist ( path -- assoc )
67 normalize-path <NSString>
68 NSData swap -> dataWithContentsOfFile:
69 [ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;