Class: ChemScanner::ChemDraw::CdxReader

Inherits:
Object
  • Object
show all
Defined in:
lib/chem_scanner/chem_draw/cdx_reader.rb

Overview

Class which traverse the tree in CDX binary files

Constant Summary collapse

HEADER_STRING_LEN =
8
HEADER_STRING =
"VjCD0100"
HEADER_LENGTH =
28
TAG_OBJECT =
0x8000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, is_path) ⇒ CdxReader

Returns a new instance of CdxReader.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 14

def initialize(file, is_path)
  @ids = []
  @depth = 0

  @bin = is_path ? IO.binread(file) : file

  if @bin[0, HEADER_STRING_LEN] == HEADER_STRING
    @iter = HEADER_LENGTH
    @valid = true
  else
    @valid = false
  end
end

Instance Attribute Details

#binObject (readonly)

Returns the value of attribute bin.



12
13
14
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 12

def bin
  @bin
end

#cur_tagObject (readonly)

Returns the value of attribute cur_tag.



12
13
14
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 12

def cur_tag
  @cur_tag
end

#depthObject (readonly)

Returns the value of attribute depth.



12
13
14
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 12

def depth
  @depth
end

#iterObject (readonly)

Returns the value of attribute iter.



12
13
14
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 12

def iter
  @iter
end

#lenObject (readonly)

Returns the value of attribute len.



12
13
14
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 12

def len
  @len
end

#validObject (readonly)

Returns the value of attribute valid.



12
13
14
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 12

def valid
  @valid
end

Instance Method Details

#current_idObject



78
79
80
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 78

def current_id
  @ids.last
end

#dataObject



82
83
84
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 82

def data
  @buf.dup
end

#end?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 28

def end?
  @iter > @bin.size
end

#ignore_objectObject

rubocop:enable Metrics/PerceivedComplexity



74
75
76
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 74

def ignore_object
  read_next(true, @depth - 1)
end

#read(objects_only = false, target_depth = -2)) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 38

def read(objects_only = false, target_depth = -2)
  while @iter <= @bin.size
    tag = read_int16

    return -1 if tag.nil?

    if tag.zero?
      if @depth.zero?
        @iter = @bin.size
        return 0
      end
      @depth -= 1
      @ids.pop

      return 0 if target_depth.negative? || @depth == target_depth
    elsif (tag & TAG_OBJECT).nonzero?
      @ids.push(read_int32)
      @depth += 1

      return tag if target_depth.negative? || @depth - 1 == target_depth
    else
      @len = read_int16
      unless objects_only
        @buf = @bin[@iter, @len]
        @iter += @len

        return tag
      end

      @iter += @len
    end
  end
  0
end

#read_next(objects_only = false, target_depth = -2)) ⇒ Object



32
33
34
35
# File 'lib/chem_scanner/chem_draw/cdx_reader.rb', line 32

def read_next(objects_only = false, target_depth = -2)
  @cur_tag = read(objects_only, target_depth)
  @cur_tag
end