Class: Search_Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/Search_Engine.rb

Overview

Copyright © 2005 Martin Ankerl

Instance Method Summary collapse

Constructor Details

#initialize(gui, data) ⇒ Search_Engine

Returns a new instance of Search_Engine.



3
4
5
6
7
# File 'lib/Search_Engine.rb', line 3

def initialize(gui, data)
  @gui = gui
  @data = data
  @search_thread = nil
end

Instance Method Details

#get_match_dataObject



62
63
64
# File 'lib/Search_Engine.rb', line 62

def get_match_data
  str_to_match_data(@gui.search_field.text)
end

#get_word(str, index, delim = 32) ⇒ Object

32==space



123
124
125
126
127
128
129
130
131
132
# File 'lib/Search_Engine.rb', line 123

def get_word(str, index, delim=32) # 32==space
  word = ""
  c = str[index]
  while (c && c != delim)
    word += c.chr
    index += 1
    c = str[index]
  end
  [word, index]
end

#match?(item, match_data) ⇒ Boolean

Find out if item is matched by current search criteria.

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/Search_Engine.rb', line 140

def match?(item, match_data)
  words, exclude, min, max = match_data

  # check text that has to be there
  searchable, sortable_downcase, sortable_normal = item.sortable(0)
  words.each do |match_str, is_sensitive|
    if is_sensitive
      return false unless sortable_normal.include?(match_str)
    else
      return false unless sortable_downcase.include?(match_str)
    end
  end

  # check text not allowed to be there
  exclude.each do |match_str, is_sensitive|
    if is_sensitive
      return false if sortable_normal.include?(match_str)
    else
      return false if sortable_downcase.include?(match_str)
    end
  end

  # each check ok
  true
end

#on_searchObject

Executed whenever a search criteria changes to update the packet list.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/Search_Engine.rb', line 10

def on_search
  # restart current search
  @end_time = Time.now + $cfg.search_delay
  @restart_search = true
  @gui.search_label.enabled = false
  return if @search_thread && @search_thread.status

  @search_thread = Thread.new(@search_thread) do
    begin
      @gui.search_label.enabled = false
      # wait untill deadline
      while (t = (@end_time - Time.now)) > 0
        sleep(t)
      end

      @data.gui_mutex.synchronize do
        # the thread has to use the gui mutex inside
        @restart_search = false

        match_data = get_match_data

        # remove all items
        @gui.packet_list.dirty_clear

        # add all items that match the search criteria
        status_text_deadline = Time.now + $cfg.status_line_update_interval
        @data.items.each do |item|
          #item.parent = @gui.packet_list if match?(item, match_data)
          if match?(item, match_data)
            item.show
            now = Time.now
            if now > status_text_deadline
              update_search_status_text
              status_text_deadline = now + $cfg.status_line_update_interval
            end
          end
          break if @restart_search
        end
        update_search_status_text

        if (@gui.packet_list.numItems > 0)
          @gui.packet_list.setCurrentItem(0)
          @gui.packet_list.selectItem(0)
          @gui.main.show_info(@gui.packet_list.getItem(0).packet_item.data)
        end
        @gui.search_label.enabled = true

      end # synchronize
    end while @restart_search# || match_data != @gui.search_field.text.downcase.split
  end #thread.new
end

#str_to_match_data(str, index = 0) ⇒ Object

Converts a string into a match_data representation.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/Search_Engine.rb', line 67

def str_to_match_data(str, index=0)
  words = [ ]
  exclude = [ ]
  is_exclude = false

  while str[index]
    case str[index]
    when ?", ?'
      word, index = get_word(str, index+1, str[index])
      unless word.empty?
        if is_exclude
          exclude.push word
          is_exclude = false
        else
          words.push word
        end
      end

    when 32 # space
      is_exclude = false

=begin
    when ?>
      min, index = get_word(str, index+1)
      min = @gui.logic.size_to_nr(min)

    when ?<
      max, index = get_word(str, index+1)
      max = @gui.logic.size_to_nr(max)
=end
    when ?-
      is_exclude = true

    else
      word, index = get_word(str, index)
      if is_exclude
        exclude.push word
        is_exclude = false
      else
        words.push word
      end
    end

    index += 1
  end

  # check if word has upcase letters
  words.collect! do |w|
    [w, /[A-Z]/.match(w)!=nil]
  end
  exclude.collect! do |w|
    [w, /[A-Z]/.match(w)!=nil]
  end
  [words, exclude]
end

#update_search_status_textObject

Update the text for the number of displayed packs.



135
136
137
# File 'lib/Search_Engine.rb', line 135

def update_search_status_text
  @gui.search_label.text = sprintf($cfg.text.search, @gui.packet_list.numItems, @data.items.size)
end