ruby-lzws 1.4.0 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,194 +0,0 @@
1
- # Ruby bindings for lzws library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "English"
5
-
6
- require_relative "../validation"
7
-
8
- module LZWS
9
- module Stream
10
- module ReaderHelpers
11
- def getbyte
12
- read 1
13
- end
14
-
15
- def each_byte(&block)
16
- each_string method(:getbyte), &block
17
- end
18
-
19
- def readbyte
20
- readstring method(:getbyte)
21
- end
22
-
23
- def ungetbyte(byte)
24
- Validation.validate_string byte
25
-
26
- @buffer.prepend byte
27
-
28
- nil
29
- end
30
-
31
- # -- char --
32
-
33
- def getc
34
- if @external_encoding.nil?
35
- byte = getbyte
36
- return nil if byte.nil?
37
-
38
- return transcode_to_internal byte
39
- end
40
-
41
- char = ::String.new :encoding => ::Encoding::BINARY
42
-
43
- # Read one byte until valid string will appear.
44
- loop do
45
- byte = getbyte
46
- return nil if byte.nil?
47
-
48
- char << byte
49
-
50
- char.force_encoding @external_encoding
51
- return transcode_to_internal char if char.valid_encoding?
52
-
53
- char.force_encoding ::Encoding::BINARY
54
- end
55
- end
56
-
57
- def readchar
58
- readstring method(:getc)
59
- end
60
-
61
- def each_char(&block)
62
- each_string method(:getc), &block
63
- end
64
-
65
- def ungetc(char)
66
- ungetstring char
67
- end
68
-
69
- # -- lines --
70
-
71
- def gets(separator = $OUTPUT_RECORD_SEPARATOR, limit = nil)
72
- # Limit can be a first argument.
73
- if separator.is_a? ::Numeric
74
- limit = separator
75
- separator = $OUTPUT_RECORD_SEPARATOR
76
- end
77
-
78
- line_ending =
79
- if separator.nil?
80
- nil
81
- else
82
- Validation.validate_string separator
83
- ::String.new separator, :encoding => target_encoding
84
- end
85
-
86
- Validation.validate_positive_integer limit unless limit.nil?
87
-
88
- line = ::String.new :encoding => target_encoding
89
-
90
- loop do
91
- char = getc
92
-
93
- if char.nil?
94
- return nil if line.empty?
95
-
96
- break
97
- end
98
-
99
- line << char
100
-
101
- break if
102
- (!line_ending.nil? && line.end_with?(line_ending)) ||
103
- (!limit.nil? && line.length >= limit)
104
- end
105
-
106
- @lineno += 1
107
-
108
- line
109
- end
110
-
111
- def readline
112
- readstring method(:gets)
113
- end
114
-
115
- def readlines
116
- lines = []
117
- each_line { |line| lines << line }
118
-
119
- lines
120
- end
121
-
122
- def each_line(&block)
123
- each_string method(:gets), &block
124
- end
125
-
126
- alias each each_line
127
-
128
- def ungetline(line)
129
- ungetstring line
130
-
131
- @lineno -= 1
132
-
133
- nil
134
- end
135
-
136
- # -- common --
137
-
138
- protected def readstring(each_proc)
139
- string = each_proc.call
140
- raise ::EOFError if string.nil?
141
-
142
- string
143
- end
144
-
145
- protected def each_string(each_proc, &block)
146
- return enum_for __method__, each_proc unless block.is_a? ::Proc
147
-
148
- loop do
149
- string = each_proc.call
150
- break if string.nil?
151
-
152
- yield string
153
- end
154
-
155
- nil
156
- end
157
-
158
- protected def ungetstring(string)
159
- Validation.validate_string string
160
-
161
- string = ::String.new string, :encoding => @internal_encoding unless @internal_encoding.nil?
162
- string = transcode_to_external string unless @external_encoding.nil?
163
-
164
- string.force_encoding ::Encoding::BINARY
165
- @buffer.prepend string
166
-
167
- nil
168
- end
169
-
170
- # -- etc --
171
-
172
- module ClassMethods
173
- def open(file_path, *args, &block)
174
- Validation.validate_string file_path
175
- Validation.validate_proc block
176
-
177
- ::File.open file_path, "rb" do |io|
178
- reader = new io, *args
179
-
180
- begin
181
- yield reader
182
- ensure
183
- reader.close
184
- end
185
- end
186
- end
187
- end
188
-
189
- def self.included(klass)
190
- klass.extend ClassMethods
191
- end
192
- end
193
- end
194
- end
@@ -1,78 +0,0 @@
1
- # Ruby bindings for lzws library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "forwardable"
5
-
6
- module LZWS
7
- module Stream
8
- class Stat
9
- # Libraries like minitar tries to access stat to know whether stream is seekable.
10
- # We need to mark stream as not directory, file, etc, because it is not seekable.
11
-
12
- # User can use disabled delegates using :io reader.
13
-
14
- extend ::Forwardable
15
-
16
- METHODS_RETURNING_FALSE = %i[
17
- blockdev?
18
- chardev?
19
- directory?
20
- executable?
21
- executable_real?
22
- file?
23
- grpowned?
24
- owned?
25
- pipe?
26
- setgid?
27
- setuid?
28
- socket?
29
- sticky?
30
- symlink?
31
- zero?
32
- ]
33
- .freeze
34
-
35
- DELEGATES = %i[
36
- <=>
37
- atime
38
- birthtime
39
- blksize
40
- blocks
41
- ctime
42
- dev
43
- dev_major
44
- dev_minor
45
- ftype
46
- gid
47
- ino
48
- inspect
49
- mode
50
- mtime
51
- nlink
52
- rdev
53
- rdev_major
54
- rdev_minor
55
- readable?
56
- readable_real?
57
- size
58
- size?
59
- uid
60
- world_readable?
61
- world_writable?
62
- writable?
63
- writable_real?
64
- ]
65
- .freeze
66
-
67
- def initialize(stat)
68
- @stat = stat
69
- end
70
-
71
- METHODS_RETURNING_FALSE.each do |method_name|
72
- define_method(method_name) { false }
73
- end
74
-
75
- def_delegators :@stat, *DELEGATES
76
- end
77
- end
78
- end
@@ -1,91 +0,0 @@
1
- # Ruby bindings for lzws library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "English"
5
-
6
- require_relative "../error"
7
- require_relative "../validation"
8
-
9
- module LZWS
10
- module Stream
11
- module WriterHelpers
12
- def <<(object)
13
- write object
14
- end
15
-
16
- def print(*objects, field_separator: $OUTPUT_FIELD_SEPARATOR, record_separator: $OUTPUT_RECORD_SEPARATOR)
17
- objects.each do |object|
18
- write object
19
- write field_separator unless field_separator.nil?
20
- end
21
-
22
- write record_separator unless record_separator.nil?
23
-
24
- nil
25
- end
26
-
27
- def printf(*args)
28
- write sprintf(*args)
29
-
30
- nil
31
- end
32
-
33
- def putc(object, encoding: ::Encoding::BINARY)
34
- case object
35
- when ::Numeric
36
- write object.chr(encoding)
37
- when ::String
38
- write object[0]
39
- else
40
- raise ValidateError, "invalid object: \"#{object}\" for putc"
41
- end
42
-
43
- object
44
- end
45
-
46
- def puts(*objects)
47
- objects.each do |object|
48
- if object.is_a? ::Array
49
- puts(*object)
50
- next
51
- end
52
-
53
- source = object.to_s
54
- newline = "\n".encode source.encoding
55
-
56
- # Do not add newline if source ends with newline.
57
- if source.end_with? newline
58
- write source
59
- else
60
- write source + newline
61
- end
62
- end
63
-
64
- nil
65
- end
66
-
67
- # -- etc --
68
-
69
- module ClassMethods
70
- def open(file_path, *args, &block)
71
- Validation.validate_string file_path
72
- Validation.validate_proc block
73
-
74
- ::File.open file_path, "wb" do |io|
75
- writer = new io, *args
76
-
77
- begin
78
- yield writer
79
- ensure
80
- writer.close
81
- end
82
- end
83
- end
84
- end
85
-
86
- def self.included(klass)
87
- klass.extend ClassMethods
88
- end
89
- end
90
- end
91
- end