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: strings arrays hashtables assocs sequences fry macros
5 cocoa.messages cocoa.classes cocoa.application cocoa kernel
6 namespaces io.backend math cocoa.enumeration byte-arrays
7 combinators alien.c-types words core-foundation quotations
8 core-foundation.data core-foundation.utilities ;
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-NSString>) ( NSString -- string )
24 : (plist-NSNumber>) ( NSNumber -- number )
25 dup -> doubleValue dup >integer =
26 [ -> longLongValue ] [ -> doubleValue ] if ;
28 : (plist-NSData>) ( NSData -- byte-array )
29 dup -> length <byte-array> [ -> getBytes: ] keep ;
31 : (plist-NSArray>) ( NSArray -- vector )
32 [ plist> ] NSFastEnumeration-map ;
34 : (plist-NSDictionary>) ( NSDictionary -- hashtable )
35 dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ 2array ] with
36 NSFastEnumeration-map >hashtable ;
38 : (read-plist) ( NSData -- id )
39 NSPropertyListSerialization swap kCFPropertyListImmutable f f <void*>
40 [ -> propertyListFromData:mutabilityOption:format:errorDescription: ] keep
41 *void* [ -> release "read-plist failed" throw ] when* ;
43 MACRO: objc-class-case ( alist -- quot )
46 [ first2 [ '[ dup _ execute -> isKindOfClass: c-bool> ] ] dip 2array ]
52 ERROR: invalid-plist-object object ;
54 : plist> ( plist -- value )
56 { NSString [ (plist-NSString>) ] }
57 { NSNumber [ (plist-NSNumber>) ] }
58 { NSData [ (plist-NSData>) ] }
59 { NSArray [ (plist-NSArray>) ] }
60 { NSDictionary [ (plist-NSDictionary>) ] }
62 [ invalid-plist-object ]
65 : read-plist ( path -- assoc )
66 normalize-path <NSString>
67 NSData swap -> dataWithContentsOfFile:
68 [ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;