Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cat-in-136/11336549 to your computer and use it in GitHub Desktop.
Save cat-in-136/11336549 to your computer and use it in GitHub Desktop.
doxygen の xml 出力を利用して、C言語構造体の構造を JSON で吐き出すスクリプト

How to run (example)

$ ls
foobar.h
$ cat foobar.h
// foobar.h
typedef enum _BarType {
  BAR_TYPE_A,
  BAR_TYPE_B
} BarType;

#define NUM_B 3

typedef union _Bar {
  uint a;
  uint b[NUM_B];
} Bar;

typedef struct _Foo {
  BarType bartype;
  Bar bar;
} Foo;
$ doxygen -g


Configuration file 'Doxyfile' created.

Now edit the configuration file and enter

  doxygen Doxyfile

to generate the documentation for your project

$ vi Doxyfile
$ grep '^GENERATE_XML' Doxyfile
GENERATE_XML           = YES
$ doxygen Doxyfile
(...snip...)
$ ls -F
Doxyfile  foobar.h  html/  latex/  xml/
$ cd xml
$ ruby path/to/doxy_struct_xml_to_json.rb
Usage: ruby doxy_struct_xml_to_json.rb struct_id
struct_id: struct__Foo  union__Bar  

$ ruby path/to/doxy_struct_xml_to_json.rb struct__Foo
{"id":"struct__Foo","name":"_Foo","members":[{"name":"bartype","type":"BarType"},{"name":"bar","type":"Bar","type_data":{"id":"union__Bar","name":"_Bar","members":[{"name":"a","type":"uint"},{"name":"b[NUM_B]","type":"uint"}]}}]}
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment