Skip to content

Commit d79f41c

Browse files
committed
FEAT: added a way how to create not native codecs (keeping the original codecs infrastructure)
1 parent f8ccd15 commit d79f41c

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/boot/sysobj.r

+10
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ script: context [
150150

151151
standard: context [
152152

153+
codec: context [
154+
name: ;word!
155+
title: ;string!
156+
suffixes: ;block!
157+
decode: ;[any-function! none!]
158+
encode: ;[any-function! none!]
159+
identify: ;[any-function! none!]
160+
none
161+
]
162+
153163
error: context [ ; Template used for all errors:
154164
code: 0
155165
type: 'user

src/mezz/sys-codec.r

+51-6
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,40 @@ REBOL [
1717
}
1818
]
1919

20+
register-codec: function [
21+
{Registers non-native codec to system/codecs and it's suffixes into system/options/file-types}
22+
codec [object!] "Codec to register (should be based on system/standard/codec template)"
23+
/local name suffixes
24+
][
25+
if not word? name: try [codec/name][
26+
cause-error 'Script 'wrong-type 'codec/name
27+
]
28+
29+
append system/codecs reduce [to set-word! name codec]
30+
31+
if block? suffixes: try [codec/suffixes][
32+
append append system/options/file-types suffixes name
33+
]
34+
codec
35+
]
36+
2037
decode: function [
2138
{Decodes a series of bytes into the related datatype (e.g. image!).}
2239
type [word!] {Media type (jpeg, png, etc.)}
23-
data [binary!] {The data to decode}
40+
data [binary! string!] {The data to decode}
2441
][
2542
unless all [
2643
cod: select system/codecs type
27-
data: do-codec cod/entry 'decode data
44+
data: either handle? try [cod/entry] [
45+
; original codecs were only natives
46+
do-codec cod/entry 'decode data
47+
][
48+
either function? try [:cod/decode][
49+
cod/decode data
50+
][
51+
cause-error 'internal 'not-done type
52+
]
53+
]
2854
][
2955
cause-error 'access 'no-codec type
3056
]
@@ -39,7 +65,16 @@ encode: function [
3965
][
4066
unless all [
4167
cod: select system/codecs type
42-
data: do-codec cod/entry 'encode data
68+
data: either handle? try [cod/entry] [
69+
; original codecs were only natives
70+
do-codec cod/entry 'encode data
71+
][
72+
either function? try [:cod/encode][
73+
cod/encode data
74+
][
75+
cause-error 'internal 'not-done type
76+
]
77+
]
4378
][
4479
cause-error 'access 'no-codec type
4580
]
@@ -51,11 +86,21 @@ encoding?: function [
5186
data [binary!]
5287
][
5388
foreach [name codec] system/codecs [
54-
if do-codec codec/entry 'identify data [
55-
return name
89+
either handle? try [cod/entry] [
90+
if do-codec codec/entry 'identify data [
91+
return name
92+
]
93+
][
94+
if all [
95+
function? try [:cod/identify]
96+
cod/identify data
97+
][
98+
return name
99+
]
56100
]
101+
57102
]
58103
none
59104
]
60105

61-
export [decode encode encoding?]
106+
export [register-codec decode encode encoding?]

0 commit comments

Comments
 (0)