Save

angerangel edited this page Jan 7, 2013 · 1 revision
Clone this wiki locally

USAGE

   SAVE where value /header header-data /all /length /compress method

DESCRIPTION

Saves a value, block, or other data to a file, URL, binary, or string.

SAVE is a function value.

ARGUMENTS

  • where -- Where to save (suffix determines encoding) (file! url! binary! string! none!)
  • value -- Value(s) to save

REFINEMENTS

  • /header -- Provide a REBOL header block (or output non-code datatypes)
    • header-data -- Header block, object, or TRUE (header is in value) (block! object! logic!)
  • /all -- Save in serialized format
  • /length -- Save the length of the script content in the header
  • /compress -- Save in a compressed format or not
    • method -- true = compressed, false = not, 'script = encoded string (logic! word!)

#SOURCE

save: make function! [  [
    {Saves a value, block, or other data to a file, URL, binary, or string.}
    where [file! url! binary! string! none!] "Where to save (suffix determines encoding)"
    value "Value(s) to save"
    /header {Provide a REBOL header block (or output non-code datatypes)}
    header-data [block! object! logic!] "Header block, object, or TRUE (header is in value)"
    /all "Save in serialized format"
    /length {Save the length of the script content in the header}
    /compress "Save in a compressed format or not"
    method [logic! word!] {true = compressed, false = not, 'script = encoded string}
    /local type data tmp
][
    if lib/all [
        not header
        any [file? where url? where]
        type: file-type? where
    ] [
        return write where encode type :value
    ]
    if any [length method] [
        header: true
        header-data: any [header-data [] ]
    ]
    if header-data [
        if header-data = true [
            header-data: any [
                lib/all [
                    block? :value
                    first+ value
                ]
                []
            ]
        ]
        header-data: either object? :header-data [
            trim :header-data
        ] [
            construct :header-data
        ]
        if compress [
            case [
                not method [remove find select header-data 'options 'compress]
                not block? select header-data 'options [
                    repend header-data ['options copy [compress] ]
                ]
                not find header-data/options 'compress [
                    append header-data/options 'compress
                ]
            ]
        ]
        if length [
            append header-data [length: true]
        ]
        unless compress: true? find select header-data 'options 'compress [method: none]
        length: true? select header-data 'length
        header-data: body-of header-data
    ]
    data: either all [mold/all/only :value] [mold/only :value]
    append data newline
    case/all [
        tmp: find header-data 'checksum [change next tmp checksum/secure data: to-binary data]
        compress [data: lib/compress data]
        method = 'script [data: mold64 data]
        not binary? data [data: to-binary data]
        length [change find/tail header-data 'length length? data]
        header-data [insert data ajoin ['REBOL #" " mold header-data newline] ]
    ]
    case [
        file? where [write where data]
        url? where [write where data]
        none? where [data]
        'else [insert tail where data]
    ]
] ]