| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  | ! 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. | 
					
						
							| 
									
										
										
										
											2014-12-13 19:25:48 -05:00
										 |  |  | USING: alien.c-types alien.data arrays byte-arrays cocoa | 
					
						
							|  |  |  | cocoa.application cocoa.classes cocoa.enumeration combinators | 
					
						
							|  |  |  | core-foundation.data core-foundation.strings | 
					
						
							|  |  |  | core-foundation.utilities fry io.backend kernel macros math | 
					
						
							|  |  |  | quotations sequences ;
 | 
					
						
							| 
									
										
										
										
											2008-02-26 19:40:32 -05:00
										 |  |  | IN: cocoa.plists | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05: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 -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  |     [ >plist ] [ normalize-path <NSString> ] bi* 0 -> writeToFile:atomically: | 
					
						
							| 
									
										
										
										
											2008-04-05 09:30:02 -04:00
										 |  |  |     [ "write-plist failed" throw ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-07-13 15:18:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEFER: plist> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-13 15:18:35 -04:00
										 |  |  | : (plist-NSNumber>) ( NSNumber -- number )
 | 
					
						
							|  |  |  |     dup -> doubleValue dup >integer =
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  |     [ -> longLongValue ] [ -> doubleValue ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-07-13 15:18:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (plist-NSData>) ( NSData -- byte-array )
 | 
					
						
							|  |  |  |     dup -> length <byte-array> [ -> getBytes: ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (plist-NSArray>) ( NSArray -- vector )
 | 
					
						
							| 
									
										
										
										
											2009-01-21 20:34:42 -05:00
										 |  |  |     [ plist> ] NSFastEnumeration-map ;
 | 
					
						
							| 
									
										
										
										
											2008-07-13 15:18:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (plist-NSDictionary>) ( NSDictionary -- hashtable )
 | 
					
						
							| 
									
										
										
										
											2014-07-08 19:12:41 -04:00
										 |  |  |     dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ ] with
 | 
					
						
							|  |  |  |     NSFastEnumeration>hashtable ;
 | 
					
						
							| 
									
										
										
										
											2008-07-13 15:18:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-19 18:17:12 -04:00
										 |  |  | : (read-plist) ( NSData -- id )
 | 
					
						
							| 
									
										
										
										
											2010-05-23 03:07:47 -04:00
										 |  |  |     NSPropertyListSerialization swap kCFPropertyListImmutable f
 | 
					
						
							|  |  |  |     { void* } | 
					
						
							| 
									
										
										
										
											2010-07-16 17:32:05 -04:00
										 |  |  |     [ -> propertyListFromData:mutabilityOption:format:errorDescription: ] | 
					
						
							| 
									
										
										
										
											2010-05-23 03:07:47 -04:00
										 |  |  |     with-out-parameters | 
					
						
							|  |  |  |     [ -> release "read-plist failed" throw ] when* ;
 | 
					
						
							| 
									
										
										
										
											2008-07-19 18:17:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  | MACRO: objc-class-case ( alist -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-05-08 19:47:44 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         dup callable? | 
					
						
							|  |  |  |         [ first2 [ '[ dup _ execute -> isKindOfClass: c-bool> ] ] dip 2array ] | 
					
						
							|  |  |  |         unless
 | 
					
						
							|  |  |  |     ] map '[ _ cond ] ;
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-08 19:47:44 -04:00
										 |  |  | ERROR: invalid-plist-object object ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  | : plist> ( plist -- value )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-08-29 01:19:50 -04:00
										 |  |  |         { NSString [ CF>string ] } | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  |         { NSNumber [ (plist-NSNumber>) ] } | 
					
						
							|  |  |  |         { NSData [ (plist-NSData>) ] } | 
					
						
							|  |  |  |         { NSArray [ (plist-NSArray>) ] } | 
					
						
							|  |  |  |         { NSDictionary [ (plist-NSDictionary>) ] } | 
					
						
							|  |  |  |         { NSObject [ ] } | 
					
						
							| 
									
										
										
										
											2009-05-08 19:47:44 -04:00
										 |  |  |         [ invalid-plist-object ] | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  |     } 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* ;
 |