class SteamCodec::KeyValues::Parser
Public Class Methods
getQuoteList(data)
click to toggle source
# File lib/steam_codec/key_values.rb, line 37 def self.getQuoteList(data) indexes = [] quoted = false length = data.length length.times do |index| if data[index] == '"' escaped = false escaped = self.isEscaped(data, index) if quoted if not escaped indexes << index quoted = !quoted end end end raise RuntimeError, "Unmatched quotes" if quoted indexes end
isEscaped(data, index, char = '\\')
click to toggle source
# File lib/steam_codec/key_values.rb, line 24 def self.isEscaped(data, index, char = '\\') return false if index == 0 escaped = false (index - 1).downto(0) do |num| if data[num] == char escaped = !escaped else break end end escaped end
proccess(data, last = true)
click to toggle source
# File lib/steam_codec/key_values.rb, line 11 def self.proccess(data, last = true) token = /"[^"]*"/ data = data.gsub(/(?<=^|[\s{}])(\s*)([^"{}\s\n\r]+)(\s*)(?=[\s{}]|\z)/, '\1"\2"\3') if last data.gsub!(/(#{token}:\s*#{token}|})(?=\s*")/, '\1,') data.gsub!(/(#{token})(?=\s*{|[ \t\f]+")/, '\1:') else data.gsub!(/(#{token}:\s*#{token}|})(?=\s*"|\s*\z)/, '\1,') data.gsub!(/(#{token})(?=\s*{|[ \t\f]+"|\s*\z)/, '\1:') end data end
toJSON(data)
click to toggle source
# File lib/steam_codec/key_values.rb, line 55 def self.toJSON(data) raise ArgumentError, "data must be String" unless data.is_a?(String) str = '' previous = 0 data.gsub!(/^#(include|base).*$/, '') # include and base not supported so just ignore quoteList = self.getQuoteList(data) quoteList.each_index do |index| quoted = index % 2 == 1 part = data[previous...quoteList[index]] previous = quoteList[index] + 1 next if part.empty? and not quoted if quoted str += '"' + part.gsub(/\n/,'\n') + '"' lastIndex = data.length lastIndex = quoteList[index + 1] if index + 1 < quoteList.length nextPart = data[previous...lastIndex].gsub(/\\\\.*$/,'') # remove comments nextPart = self.proccess(nextPart, true) case nextPart when /\A(\s*{|[ \t]+\z)/ str += ':' when /\A\s+\z/ str += ',' end else part = part.gsub(/\\\\[^\n]*$/,'') # remove comments str += self.proccess(part, index + 1 >= quoteList.length) end end lastIndex = data.length part = data[previous...lastIndex] str += self.proccess(part, true) if part '{' + str + '}' end