|
#!/usr/bin/ruby |
|
# vim: filetype=ruby fileencoding=UTF-8 shiftwidth=2 tabstop=2 autoindent expandtab |
|
# |
|
# MIT License |
|
# |
|
# Copyright (C) 2014 @cat_in_136 |
|
# |
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
# of this software and associated documentation files (the "Software"), to deal |
|
# in the Software without restriction, including without limitation the rights |
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
# copies of the Software, and to permit persons to whom the Software is |
|
# furnished to do so, subject to the following conditions: |
|
# |
|
# The above copyright notice and this permission notice shall be included in |
|
# all copies or substantial portions of the Software. |
|
# |
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
# SOFTWARE. |
|
|
|
require 'rexml/document' |
|
require 'json' |
|
|
|
class DoxyStructXml |
|
def initialize |
|
@structs = {} |
|
end |
|
attr_reader :structs |
|
|
|
def load_doxy_struct_xml_file(file) |
|
File.open(file, 'r') do |f| |
|
doc = REXML::Document.new(f) |
|
|
|
id = doc.elements['doxygen/compounddef'].attributes['id'] |
|
|
|
members = [] |
|
doc.elements.each('doxygen/compounddef//memberdef') do |elem| |
|
member = {} |
|
if elem.elements['type/ref'] |
|
member[:typeref] = elem.elements['type/ref'].attributes['refid'] |
|
member[:type] = elem.elements['type/ref'].text |
|
else |
|
member[:type] = elem.elements['type'].text |
|
end |
|
member[:argsstring] = elem.elements['argsstring'].text |
|
member[:name] = elem.elements['name'].text |
|
|
|
members << member |
|
end |
|
|
|
@structs[id] = { |
|
:name => doc.elements['doxygen/compounddef/compoundname'].text, |
|
:members => members |
|
} |
|
end |
|
end |
|
|
|
def data(struct_id) |
|
d = {} |
|
d[:id] = struct_id |
|
d[:name] = @structs[struct_id][:name] |
|
d[:members] = [] |
|
@structs[struct_id][:members].each do |member| |
|
m = {} |
|
m[:name] = member[:name] |
|
m[:type] = member[:type] |
|
m[:name] << member[:argsstring] if member[:argsstring] |
|
if member[:typeref] |
|
m[:type_data] = data(member[:typeref]) |
|
end |
|
d[:members] << m |
|
end |
|
|
|
d |
|
end |
|
end |
|
|
|
if $0 == __FILE__ |
|
doxy_struct_xml = DoxyStructXml.new |
|
Dir.glob('{struct,union}_*.xml') do |file| |
|
doxy_struct_xml.load_doxy_struct_xml_file(file) |
|
end |
|
|
|
struct_id = ARGV[0] |
|
|
|
unless struct_id |
|
$stderr << "Usage: ruby #{$0} struct_id\n" |
|
$stderr << "struct_id: " |
|
doxy_struct_xml.structs.each do |key,value| |
|
$stderr << key << " " |
|
end |
|
$stderr << "\n\n" |
|
exit 1 |
|
end |
|
|
|
if doxy_struct_xml.structs[struct_id] |
|
$stdout << JSON.dump(doxy_struct_xml.data(struct_id)) |
|
$stdout << "\n" |
|
else |
|
$stderr << "#{struct_id} does not found" |
|
exit 1 |
|
end |
|
end |
|
|
|
|