/
convert_to_xml.rb
88 lines (76 loc) · 2.76 KB
/
convert_to_xml.rb
1
2
3
4
5
6
7
8
9
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
=begin
convert_to_xml.rb is part of the GeneRic ASsembler (GRAS)
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=end
require 'blockparser.rb'
def in_quote(str)
return str if str.match(/^".*"$/)
return '"'+str+'"'
end
def e(str) #perform escaping
str.gsub!('&', '&')
str.gsub!('<', '<')
str.gsub!('>', '>')
return str
end
def convert(finname, foutname)
# puts 'Loading ...'
block=loadfile(finname)
# puts block.inspect
# block.to_xml(0)
if foutname==nil || foutname== '-'
fout=STDOUT
else
fout = File.open(foutname,"w+")
end
fout.puts '<?xml version="1.0" encoding="UTF-8"?>'
fout.puts '<systeminformation>'
cpu_block=block.sub_blocks["cpu"]
fout.puts ' <cpu>'
cpu_block.key_value.each{|k,v| fout.puts " <#{e(k)}>#{e(v)}</#{e(k)}>"} if cpu_block
fout.puts ' </cpu>'
param_block=block.sub_blocks["parameters"]
fout.puts ' <parameters>'
param_block.key_value.each{|k,v| fout.puts " <#{e(k)}>#{e(v)}</#{e(k)}>"} if param_block
param_block.sub_blocks.each do |k,v|
fout.puts(" <param name=#{e(in_quote(k))}>")
v.key_value.each{|x,y| fout.puts(" <#{e(x)}>#{e(y)}</#{e(x)}>")}
fout.puts(" </param>")
end if param_block
fout.puts ' </parameters>'
ins_block=block.sub_blocks["instructionset"]
fout.puts ' <instructionset>'
ins_block.key_value.each{|k,v| fout.puts " <#{e(k)}>#{e(v)}</#{e(k)}>"}
ins_block.sub_blocks.each do |k,v|
fout.puts(" <instruction name=#{in_quote(k)}>")
v.key_value.each{|x,y| fout.puts(" <#{e(x)}>#{e(y)}</#{e(x)}>")}
v.sub_blocks.each do |params, desc|
fout.puts(" <param_code name=#{e(in_quote(params))}>")
desc.key_value.each{|x,y| fout.puts " <#{e(x)}>#{e(y)}</#{e(x)}>"}
fout.puts(" </param_code>")
end
fout.puts(" </instruction>")
end
fout.puts ' </instructionset>'
fout.puts '</systeminformation>'
end
if ARGV.length==0
puts "usage: #{$0} in_file [out_file]"
exit(-1)
end
if ARGV.length==2
foutname=ARGV[1]
else
foutname=ARGV[0]+".xml"
end
convert(ARGV[0], foutname)