]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/plists/plists.factor
Fix conflict in images vocab
[factor.git] / basis / cocoa / plists / plists.factor
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 ;
9 IN: cocoa.plists
10
11 : >plist ( value -- plist ) >cf -> autorelease ;
12
13 : write-plist ( assoc path -- )
14     [ >plist ] [ normalize-path <NSString> ] bi* 0 -> writeToFile:atomically:
15     [ "write-plist failed" throw ] unless ;
16
17 DEFER: plist>
18
19 <PRIVATE
20
21 : (plist-NSString>) ( NSString -- string )
22     -> UTF8String ;
23
24 : (plist-NSNumber>) ( NSNumber -- number )
25     dup -> doubleValue dup >integer =
26     [ -> longLongValue ] [ -> doubleValue ] if ;
27
28 : (plist-NSData>) ( NSData -- byte-array )
29     dup -> length <byte-array> [ -> getBytes: ] keep ;
30
31 : (plist-NSArray>) ( NSArray -- vector )
32     [ plist> ] NSFastEnumeration-map ;
33
34 : (plist-NSDictionary>) ( NSDictionary -- hashtable )
35     dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ 2array ] with
36     NSFastEnumeration-map >hashtable ;
37
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* ;
42
43 MACRO: objc-class-case ( alist -- quot )
44     [ [ '[ dup _ execute -> isKindOfClass: c-bool> ] ] dip ] assoc-map '[ _ cond ] ;
45
46 PRIVATE>
47
48 : plist> ( plist -- value )
49     {
50         { NSString [ (plist-NSString>) ] }
51         { NSNumber [ (plist-NSNumber>) ] }
52         { NSData [ (plist-NSData>) ] }
53         { NSArray [ (plist-NSArray>) ] }
54         { NSDictionary [ (plist-NSDictionary>) ] }
55         { NSObject [ ] }
56     } objc-class-case ;
57
58 : read-plist ( path -- assoc )
59     normalize-path <NSString>
60     NSData swap -> dataWithContentsOfFile:
61     [ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;