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
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 [ tuck -> valueForKey: [ 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 )
44 [ [ '[ dup _ execute -> isKindOfClass: c-bool> ] ] dip ] assoc-map '[ _ cond ] ;
48 : plist> ( plist -- value )
50 { NSString [ (plist-NSString>) ] }
51 { NSNumber [ (plist-NSNumber>) ] }
52 { NSData [ (plist-NSData>) ] }
53 { NSArray [ (plist-NSArray>) ] }
54 { NSDictionary [ (plist-NSDictionary>) ] }
58 : read-plist ( path -- assoc )
59 normalize-path <NSString>
60 NSData swap -> dataWithContentsOfFile:
61 [ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;