from gen_common import *
def parse_event_fields(lines, idx, event_dict):
- field_names = []
- field_types = []
+ fields = []
end_of_event = False
- num_fields = 0
-
# record all fields in event definition.
# note: we don't check if there's a leading brace.
while not end_of_event and idx < len(lines):
line = lines[idx].rstrip()
idx += 1
- field = re.match(r'(\s*)(\w+)(\s*)(\w+)', line)
+ match = re.match(r'(\s*)([\w\*]+)(\s*)([\w]+)(\[\d+\])*', line)
- if field:
- field_types.append(field.group(2))
- field_names.append(field.group(4))
- num_fields += 1
+ if match:
+ field = {
+ "type": match.group(2),
+ "name": match.group(4),
+ "size": int(match.group(5)[1:-1]) if match.group(5) else 1
+ }
+ fields.append(field)
end_of_event = re.match(r'(\s*)};', line)
- event_dict['field_types'] = field_types
- event_dict['field_names'] = field_names
- event_dict['num_fields'] = num_fields
+ event_dict['fields'] = fields
+ event_dict['num_fields'] = len(fields)
return idx
#pragma pack(push, 1)
struct ${name}Data
{<%
- field_names = protos['events'][name]['field_names']
- field_types = protos['events'][name]['field_types'] %>
+ fields = protos['events'][name]['fields'] %>
// Fields
- % for i in range(len(field_names)):
- ${field_types[i]} ${field_names[i]};
+ % for i in range(len(fields)):
+ % if fields[i]['size'] > 1:
+ ${fields[i]['type']} ${fields[i]['name']}[${fields[i]['size']}];
+ % else:
+ ${fields[i]['type']} ${fields[i]['name']};
+ % endif
% endfor
};
#pragma pack(pop)
//////////////////////////////////////////////////////////////////////////
struct ${name} : Event
{<%
- field_names = protos['events'][name]['field_names']
- field_types = protos['events'][name]['field_types'] %>
+ fields = protos['events'][name]['fields'] %>
${name}Data data;
// Constructor
${name}(
- % for i in range(len(field_names)):
- % if i < len(field_names)-1:
- ${field_types[i]} ${field_names[i]},
+ % for i in range(len(fields)):
+ % if i < len(fields)-1:
+ % if fields[i]['size'] > 1:
+ ${fields[i]['type']}* ${fields[i]['name']},
+ uint32_t ${fields[i]['name']}_size,
+ % else:
+ ${fields[i]['type']} ${fields[i]['name']},
+ % endif
% endif
- % if i == len(field_names)-1:
- ${field_types[i]} ${field_names[i]}
+ % if i == len(fields)-1:
+ % if fields[i]['size'] > 1:
+ ${fields[i]['type']}* ${fields[i]['name']},
+ uint32_t ${fields[i]['name']}_size
+ % else:
+ ${fields[i]['type']} ${fields[i]['name']}
+ % endif
% endif
% endfor
)
{
- % for i in range(len(field_names)):
- data.${field_names[i]} = ${field_names[i]};
+ % for i in range(len(fields)):
+ % if fields[i]['size'] > 1:
+ % if fields[i]['type'] == 'char':
+ // Copy size of string (null-terminated) followed by string buffer info entire buffer
+ SWR_ASSERT(${fields[i]['name']}_size + 1 < ${fields[i]['size']} - sizeof(uint32_t), "String length must be less than size of char buffer - size(uint32_t)!");
+ memcpy(data.${fields[i]['name']}, &${fields[i]['name']}_size, sizeof(uint32_t));
+ strcpy_s(data.${fields[i]['name']} + sizeof(uint32_t), ${fields[i]['name']}_size + 1, ${fields[i]['name']});
+ % else:
+ memcpy(data.${fields[i]['name']}, ${fields[i]['name']}, ${fields[i]['name']}_size);
+ % endif
+ % else:
+ data.${fields[i]['name']} = ${fields[i]['name']};
+ % endif
% endfor
}