<commit_before>! Copyright (C) 2007, 2009 <NAME>.
! Copyright (C) 2008 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: strings arrays hashtables assocs sequences fry macros
cocoa.messages cocoa.classes cocoa.application cocoa kernel
namespaces io.backend math cocoa.enumeration byte-arrays
combinators alien.c-types alien.data words core-foundation
quotations core-foundation.data core-foundation.utilities ;
IN: cocoa.plists

: >plist ( value -- plist ) >cf -> autorelease ;

: write-plist ( assoc path -- )
    [ >plist ] [ normalize-path <NSString> ] bi* 0 -> writeToFile:atomically:
    [ "write-plist failed" throw ] unless ;

DEFER: plist>

<PRIVATE

: (plist-NSString>) ( NSString -- string )
    -> UTF8String ;

: (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 )
    dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ 2array ] with
    NSFastEnumeration-map >hashtable ;

: (read-plist) ( NSData -- id )
    NSPropertyListSerialization swap kCFPropertyListImmutable f
    { void* }
    [ -> propertyListFromData:mutabilityOption:format:errorDescription: ]
    with-out-parameters
    [ -> release "read-plist failed" throw ] when* ;

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 [ (plist-NSString>) ] }
        { NSNumber [ (plist-NSNumber>) ] }
        { NSData [ (plist-NSData>) ] }
        { NSArray [ (plist-NSArray>) ] }
        { NSDictionary [ (plist-NSDictionary>) ] }
        { NSObject [ ] }
        [ invalid-plist-object ]
    } objc-class-case ;
<commit_msg>cocoa.plists: fix for recent change to cocoa binding: char* returns are now treated like void* and not like c-string<commit_after>! Copyright (C) 2007, 2009 <NAME>.
! Copyright (C) 2008 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: strings arrays hashtables assocs sequences fry macros
cocoa.messages cocoa.classes cocoa.application cocoa 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 ;
IN: cocoa.plists

: >plist ( value -- plist ) >cf -> autorelease ;

: write-plist ( assoc path -- )
    [ >plist ] [ normalize-path <NSString> ] bi* 0 -> writeToFile:atomically:
    [ "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 )
    dup [ [ nip ] [ -> valueForKey: ] 2bi [ plist> ] bi@ 2array ] with
    NSFastEnumeration-map >hashtable ;

: (read-plist) ( NSData -- id )
    NSPropertyListSerialization swap kCFPropertyListImmutable f
    { void* }
    [ -> propertyListFromData:mutabilityOption:format:errorDescription: ]
    with-out-parameters
    [ -> release "read-plist failed" throw ] when* ;

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 ;

{'fraction_non_alphanumeric': 0.13889552952426487, 'fraction_numerical': 0.008367200573750896, 'mean_word_length': 2.859778597785978, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1341949', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1330, 'n_words': 325}