2 ## This file is part of the libsigrok project.
4 ## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
6 ## This program is free software: you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from xml.etree import ElementTree
21 from collections import OrderedDict
24 index_file = sys.argv[1]
26 # Get directory this script is in.
27 dirname = os.path.dirname(os.path.realpath(__file__))
30 ('sr_loglevel', ('LogLevel', 'Log verbosity level')),
31 ('sr_packettype', ('PacketType', 'Type of datafeed packet')),
32 ('sr_mq', ('Quantity', 'Measured quantity')),
33 ('sr_unit', ('Unit', 'Unit of measurement')),
34 ('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')),
35 ('sr_configkey', ('ConfigKey', 'Configuration key')),
36 ('sr_datatype', ('DataType', 'Configuration data type')),
37 ('sr_channeltype', ('ChannelType', 'Channel type')),
38 ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type'))])
40 index = ElementTree.parse(index_file)
42 # Build mapping between class names and enumerations.
44 classes = OrderedDict()
46 for compound in index.findall('compound'):
47 if compound.attrib['kind'] != 'file':
49 filename = os.path.join(
50 os.path.dirname(index_file),
51 '%s.xml' % compound.attrib['refid'])
52 doc = ElementTree.parse(filename)
53 for section in doc.find('compounddef').findall('sectiondef'):
54 if section.attrib["kind"] != 'enum':
56 for member in section.findall('memberdef'):
57 if member.attrib["kind"] != 'enum':
59 name = member.find('name').text
61 classes[member] = mapping[name]
63 header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
64 code = open(os.path.join(dirname, 'enums.cpp'), 'w')
66 for file in (header, code):
67 print >> file, "/* Generated file - edit enums.py instead! */"
69 # Template for beginning of class declaration and public members.
70 header_public_template = """
72 class SR_API {classname} : public EnumValue<enum {enumname}>
75 static const {classname} *get(int id);
78 # Template for beginning of private members.
79 header_private_template = """
81 static const std::map<enum {enumname}, const {classname} *> values;
82 {classname}(enum {enumname} id, const char name[]);
85 # Template for class method definitions.
87 {classname}::{classname}(enum {enumname} id, const char name[]) :
88 EnumValue<enum {enumname}>(id, name)
92 const {classname} *{classname}::get(int id)
94 return {classname}::values.at(static_cast<{enumname}>(id));
99 return str.join('\n\n',
100 [p.text.rstrip() for p in node.findall('para')])
102 for enum, (classname, classbrief) in classes.items():
104 enum_name = enum.find('name').text
105 members = enum.findall('enumvalue')
106 member_names = [m.find('name').text for m in members]
107 trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names]
108 briefs = [get_text(m.find('briefdescription')) for m in members]
110 # Begin class and public declarations
111 print >> header, header_public_template.format(
112 brief=classbrief, classname=classname, enumname=enum_name)
114 # Declare public pointers for each enum value
115 for trimmed_name, brief in zip(trimmed_names, briefs):
117 print >> header, '\t/** %s */' % brief
118 print >> header, '\tstatic const %s * const %s;' % (
119 classname, trimmed_name)
121 # Declare additional methods if present
122 filename = os.path.join(dirname, "%s_methods.hpp" % classname)
123 if os.path.exists(filename):
124 print >> header, str.join('', open(filename).readlines())
126 # Begin private declarations
127 print >> header, header_private_template.format(
128 classname=classname, enumname=enum_name)
130 # Declare private constants for each enum value
131 for trimmed_name in trimmed_names:
132 print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
134 # End class declaration
135 print >> header, '};'
138 print >> code, code_template.format(
139 classname=classname, enumname=enum_name)
141 # Define private constants for each enum value
142 for name, trimmed_name in zip(member_names, trimmed_names):
143 print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
144 classname, classname, trimmed_name, classname, name, trimmed_name)
146 # Define public pointers for each enum value
147 for trimmed_name in trimmed_names:
148 print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
149 classname, classname, trimmed_name, classname, trimmed_name)
151 # Define map of enum values to constants
152 print >> code, 'const std::map<enum %s, const %s *> %s::values = {' % (
153 enum_name, classname, classname)
154 for name, trimmed_name in zip(member_names, trimmed_names):
155 print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
158 # Define additional methods if present
159 filename = os.path.join(dirname, "%s_methods.cpp" % classname)
160 if os.path.exists(filename):
161 print >> code, str.join('', open(filename).readlines())