1#****************************************************************************
2# Copyright (c) 2001-2014
3#
4# This file is part of the QuickFIX FIX Engine
5#
6# This file may be distributed under the terms of the quickfixengine.org
7# license as defined by quickfixengine.org and appearing in the file
8# LICENSE included in the packaging of this file.
9#
10# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12#
13# See http://www.quickfixengine.org/LICENSE for licensing information.
14#
15# Contact ask@quickfixengine.org if any conditions of this licensing are
16# not clear to you.
17#****************************************************************************
18
19class Comparator < Hash
20
21  def initialize(patterns)
22    patterns.each_line do
23      | line |
24      line.chomp!
25      array = line.split("=")
26      num = array[0].to_i
27      regex = Regexp.new(array[1])
28      self[num] = regex;
29    end
30  end
31
32  def compare(left, right)
33    @reason = nil
34    left_array = left.split("\001")
35    right_array = right.split("\001")
36    # check for number of fields
37    if left_array.size != right_array.size
38      @reason = "Number of fields do not match"
39      return false
40    end
41    left_array.each_index do
42      | index |
43      left_field = left_array[index].split("=")
44      right_field = right_array[index].split("=")
45      # check if field is in same order
46      if left_field[0] != right_field[0]
47        @reason = "Expected field (" + left_field[0] + ") but found field (" + right_field[0] + ")"
48	return false
49      end
50
51      regexp = self[left_field[0].to_i]
52      # do a straight comparison or regex comparison
53      if regexp == nil
54	if left_field[1] != right_field[1]
55	  @reason = "Value in field (" + left_field[0] + ") should be (" + left_field[1] + ") but was (" + right_field[1] + ")"
56	  return false
57	end
58      else
59	if !(regexp === right_field[1])
60	  @reason = "Field (" + left_field[0] + ") does not match pattern"
61	  return false
62	end
63      end
64    end
65    return true
66  end
67
68  def reason()
69    return @reason
70  end
71
72end
73