class GenerateReason # Extract Error Code Constants from Header files def GenerateReason.extract_const(filename, const_prefix) constants = [] File.open(filename) do |file| file.each do |line| line.rstrip! # Skip empty and comment lines if line.length > 0 && line !~ /^\s*\/\*/ if match = /\s*#define\s+(#{const_prefix}\w+)[\(\s]+([-\dx]+)/.match(line) constants << [match[1], match[2]] end end end end constants end # Extract Error Code Constants from Header files # Uses lazy print to collect duplicate values def GenerateReason.reason_case(filename, prefix) last_rc = nil last_reason = nil str = '' GenerateReason.extract_const(filename, prefix).each do |item| if last_rc == item[1] str << " case %-30s: return \"#{item[0]} or #{last_reason}[#{item[1]}]\";\n" % item[0] last_rc = nil last_reason = nil else str << " case %-30s: return \"#{last_reason}[#{last_rc}]\";\n" % last_reason if last_rc last_rc = item[1] last_reason = item[0] end end str end # The WebSphere MQ header files have several duplicate values, # so need to exclude duplicates. def GenerateReason.selector_case(filename, prefix, excludes=nil, &block) GenerateReason.extract_const(filename, prefix).each do |item| next if item[0].include?('FIRST') || item[0].include?('LAST') next if excludes && excludes.include?(item[0]) block.call item end end def GenerateReason.wmq_reason(path) str = <