1
1
using LazyArtifacts
2
2
using Lerche: Lerche, Lark, Transformer, @rule , @inline_rule
3
+ using CodecZlib: GzipDecompressorStream, GzipCompressorStream
3
4
4
- export FileFormat
5
+ export FileFormat, GzipFormat
5
6
6
7
# version of model zoo to grab files from
7
8
const zoo_version = " /Circuit-Model-Zoo-0.1.4"
@@ -13,6 +14,10 @@ Lerche.visit_tokens(t::JuiceTransformer) = false
13
14
# file formats supported by this package
14
15
abstract type FileFormat end
15
16
17
+ struct GzipFormat <: FileFormat
18
+ inner_format:: FileFormat
19
+ end
20
+
16
21
# usual comment format for DIMACS-based file formats
17
22
const dimacs_comments = raw """
18
23
COMMENT : ("c" | "cc") (_WS /[^\n ]/*)? (_NL | /$/)
@@ -32,7 +37,11 @@ include("plot.jl")
32
37
# if no logic circuit file format is given on read, infer file format from extension
33
38
34
39
function file2logicformat (file)
35
- if endswith (file," .jlc" )
40
+ if endswith (file," .gz" )
41
+ file_inner, _ = splitext (file)
42
+ format_inner = file2logicformat (file_inner)
43
+ GzipFormat (format_inner)
44
+ elseif endswith (file," .jlc" )
36
45
JlcFormat ()
37
46
elseif endswith (file," .sdd" )
38
47
SddFormat ()
@@ -83,6 +92,10 @@ Base.parse(::Type{LogicCircuit}, args...) =
83
92
Base. read (io:: IO , :: Type{LogicCircuit} , args... ) =
84
93
read (io, PlainLogicCircuit, args... )
85
94
95
+ Base. read (io:: IO , :: Type{LogicCircuit} , f:: GzipFormat ) =
96
+ # avoid method ambiguity
97
+ read (io, PlainLogicCircuit, f)
98
+
86
99
# when asked to parse/read as `StructLogicCircuit`, default to `PlainStructLogicCircuit`
87
100
88
101
Base. parse (:: Type{StructLogicCircuit} , args... ) =
@@ -91,9 +104,17 @@ Base.parse(::Type{StructLogicCircuit}, args...) =
91
104
Base. read (io:: IO , :: Type{StructLogicCircuit} , args... ) =
92
105
read (io, PlainStructLogicCircuit, args... )
93
106
107
+ Base. read (io:: IO , :: Type{StructLogicCircuit} , f:: GzipFormat ) =
108
+ # avoid method ambiguity
109
+ read (io, PlainStructLogicCircuit, f)
110
+
94
111
Base. read (ios:: Tuple{IO,IO} , :: Type{StructLogicCircuit} , args... ) =
95
112
read (ios, PlainStructLogicCircuit, args... )
96
113
114
+ Base. read (ios:: Tuple{IO,IO} , :: Type{StructLogicCircuit} , f:: Tuple{GzipFormat,VtreeFormat} ) =
115
+ # avoid method ambiguity
116
+ read (ios, PlainStructLogicCircuit, f)
117
+
97
118
# copy read/write API for tuples of files
98
119
99
120
function Base. read (files:: Tuple{AbstractString, AbstractString} , :: Type{C} , args... ) where C <: StructLogicCircuit
@@ -111,4 +132,28 @@ function Base.write(files::Tuple{AbstractString,AbstractString},
111
132
write ((io1, io2), circuit, args... )
112
133
end
113
134
end
114
- end
135
+ end
136
+
137
+ # (de)compress Gzip streams
138
+
139
+ Base. read (io:: IO , circuit_type, f:: GzipFormat ) =
140
+ read (GzipDecompressorStream (io), circuit_type, f. inner_format)
141
+
142
+ Base. write (io:: IO , circuit, f:: GzipFormat ) = begin
143
+ iogz = GzipCompressorStream (io)
144
+ write (iogz, circuit, f. inner_format)
145
+ close (iogz)
146
+ end
147
+
148
+ Base. read (ios:: Tuple{IO,IO} , circuit_type, f:: Tuple{GzipFormat,VtreeFormat} ) = begin
149
+ ios = (GzipDecompressorStream (ios[1 ]), ios[2 ])
150
+ formats = (f[1 ]. inner_format, f[2 ])
151
+ read (ios, circuit_type, formats)
152
+ end
153
+
154
+ Base. write (io:: Tuple{IO,IO} , circuit, f:: Tuple{GzipFormat,VtreeFormat} ) = begin
155
+ iosgz = (GzipCompressorStream (io[1 ]), io[2 ])
156
+ formats = (f[1 ]. inner_format, f[2 ])
157
+ write (iosgz, circuit, formats)
158
+ close (iosgz[1 ])
159
+ end
0 commit comments