1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! Copyright (C) 2008 Joe Groff.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: alien.c-types alien.data arrays byte-arrays cocoa
5 cocoa.application cocoa.classes cocoa.enumeration combinators
6 core-foundation.data core-foundation.strings
7 core-foundation.utilities fry io.backend kernel macros math
11 : >plist ( value -- plist ) >cf -> autorelease ;
13 : write-plist ( assoc path -- )
14 [ >plist ] [ normalize-path <NSString> ] bi* 0 -> writeToFile:atomically:
15 [ "write-plist failed" throw ] unless ;
21 : (plist-NSNumber>) ( NSNumber -- number )
22 dup -> doubleValue dup >integer =
23 [ -> longLongValue ] [ -> doubleValue ] if ;
25 : (plist-NSData>) ( NSData -- byte-array )
26 dup -> length <byte-array> [ -> getBytes: ] keep ;
28 : (plist-NSArray>) ( NSArray -- vector )
29 [ plist> ] NSFastEnumeration-map ;
31 : (plist-NSDictionary>) ( NSDictionary -- hashtable )
32 dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ ] with
33 NSFastEnumeration>hashtable ;
35 : (read-plist) ( NSData -- id )
36 NSPropertyListSerialization swap kCFPropertyListImmutable f
38 [ -> propertyListFromData:mutabilityOption:format:errorDescription: ]
40 [ -> release "read-plist failed" throw ] when* ;
42 MACRO: objc-class-case ( alist -- quot )
45 [ first2 [ '[ dup _ execute -> isKindOfClass: c-bool> ] ] dip 2array ]
51 ERROR: invalid-plist-object object ;
53 : plist> ( plist -- value )
55 { NSString [ CF>string ] }
56 { NSNumber [ (plist-NSNumber>) ] }
57 { NSData [ (plist-NSData>) ] }
58 { NSArray [ (plist-NSArray>) ] }
59 { NSDictionary [ (plist-NSDictionary>) ] }
61 [ invalid-plist-object ]
64 : read-plist ( path -- assoc )
65 normalize-path <NSString>
66 NSData swap -> dataWithContentsOfFile:
67 [ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;