factor/basis/cocoa/plists/plists.factor

69 lines
2.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2009 Slava Pestov.
! Copyright (C) 2008 Joe Groff.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: strings arrays hashtables assocs sequences fry macros
2012-06-22 16:47:17 -04:00
cocoa cocoa.messages cocoa.classes cocoa.application
kernel namespaces io.backend math cocoa.enumeration byte-arrays
combinators alien.c-types alien.data words quotations
core-foundation core-foundation.data core-foundation.strings
core-foundation.utilities ;
2008-02-26 19:40:32 -05:00
IN: cocoa.plists
2007-09-20 18:09:08 -04:00
: >plist ( value -- plist ) >cf -> autorelease ;
2007-09-20 18:09:08 -04:00
2008-04-05 09:30:02 -04:00
: write-plist ( assoc path -- )
[ >plist ] [ normalize-path <NSString> ] bi* 0 -> writeToFile:atomically:
2008-04-05 09:30:02 -04:00
[ "write-plist failed" throw ] unless ;
DEFER: plist>
<PRIVATE
: (plist-NSNumber>) ( NSNumber -- number )
dup -> doubleValue dup >integer =
[ -> longLongValue ] [ -> doubleValue ] if ;
: (plist-NSData>) ( NSData -- byte-array )
dup -> length <byte-array> [ -> getBytes: ] keep ;
: (plist-NSArray>) ( NSArray -- vector )
[ plist> ] NSFastEnumeration-map ;
: (plist-NSDictionary>) ( NSDictionary -- hashtable )
2009-02-02 17:11:16 -05:00
dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ 2array ] with
NSFastEnumeration-map >hashtable ;
2008-07-19 18:17:12 -04:00
: (read-plist) ( NSData -- id )
NSPropertyListSerialization swap kCFPropertyListImmutable f
{ void* }
[ -> propertyListFromData:mutabilityOption:format:errorDescription: ]
with-out-parameters
[ -> release "read-plist failed" throw ] when* ;
2008-07-19 18:17:12 -04:00
MACRO: objc-class-case ( alist -- quot )
[
dup callable?
[ first2 [ '[ dup _ execute -> isKindOfClass: c-bool> ] ] dip 2array ]
unless
] map '[ _ cond ] ;
PRIVATE>
ERROR: invalid-plist-object object ;
: plist> ( plist -- value )
{
{ NSString [ CF>string ] }
{ NSNumber [ (plist-NSNumber>) ] }
{ NSData [ (plist-NSData>) ] }
{ NSArray [ (plist-NSArray>) ] }
{ NSDictionary [ (plist-NSDictionary>) ] }
{ NSObject [ ] }
[ invalid-plist-object ]
} objc-class-case ;
2008-07-19 18:17:12 -04:00
: read-plist ( path -- assoc )
normalize-path <NSString>
NSData swap -> dataWithContentsOfFile:
[ (read-plist) plist> ] [ "read-plist failed" throw ] if* ;