{"id":200,"date":"2012-02-12T16:57:16","date_gmt":"2012-02-12T15:57:16","guid":{"rendered":"http:\/\/paguilar.org\/?p=200"},"modified":"2012-02-12T16:57:16","modified_gmt":"2012-02-12T15:57:16","slug":"simple-c-to-xml-serializer-0-0-2-released-in-github","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=200","title":{"rendered":"Simple C to XML serializer 0.0.2 released in GitHub"},"content":{"rendered":"<p>Some while ago I wrote a simple C to XML serializer that reads C structs and unions from a header file a writes them to a XML file. You can read that post <a href=\"https:\/\/paguilar.org\/?p=121\">here<\/a>.<\/p>\n<p>I&#8217;ve improved it a little bit and released it in GitHub. You can check it out <a href=\"https:\/\/github.com\/paguilar\/sc2xml\">here<\/a>.<\/p>\n<p>This program is in its first stages and currently it only converts the most widely used C-structs constructs to XML. However, it could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar.<\/p>\n<p>The version I released in GitHub adds the support for structs\/unions defined inside a macro.<\/p>\n<p>I&#8217;ll try to explain how this feature works:<br \/>\nSuppose you have a file,<em> test3.h<\/em>, with a macro that defines the following struct:<\/p>\n<pre lang=\"c\">\n#define def_struct_data(prefix,number,code_number)\nstruct prefix##driver_data\n{\n    task_status_t tsk_sts;\n\n    struct buttons_platform_data pdata[number];\n    struct prefix##an_event event;\n\n    struct prefix##buttons_settings\n    {\n        uint8_t thresholds[3];\n        uint8_t pressed_threshold;\n        struct prefix##buttons_mask hash_table[code_number];\n    }__attribute__((packed)) settings;\n\n    uint8_t raw[((number) \/ 8) + 1];\n    uint8_t raw_mem[((number) \/ 8) + 1];\n\n}__attribute__((packed));\n<\/pre>\n<p>As you probably know, the syntax of a C macro is not valid for a C compiler, that&#8217;s one of the reasons for using the C pre-compiler: it <em>converts<\/em> the macro to something that the compiler understands.<br \/>\nIn the SC2XML case, applies the same condition. Therefore, you have to create a very simple C file that includes the file that you want to serialize, in this case test3.h, along with the use of the macro and execute sc2xml with that file:<\/p>\n<p>Assume that the name of your simple C file is <em>test3.stub.h<\/em>. It should looks like this:<\/p>\n<pre lang=\"c\">\n#include \"test3.h\"\ndef_struct_data(prefix,number,code_number)\n<\/pre>\n<p><em>def_struct_data<\/em> is the name of your macro.<\/p>\n<p>Execute sc2xml:<\/p>\n<pre lang=\"bash\">\n$ sc2xml test3.stub.h\n\nsc2xml 0.0.2\n\nINFO: *** Parsing file data\/test3.gen.h ***\n\n<\/pre>\n<p>The final generated XML file, <em>test3.h.xml<\/em>, will look like this:<\/p>\n<pre lang=\"xml\">\n<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<sc2xml>\n    <struct>\n        <struct_name>prefixdriver_data<\/struct_name>\n        <field type=\"task_status_t\">\n            <name>tsk_sts<\/name>\n        <\/field>\n        <field type=\"struct buttons_platform_data\" size=\"number\">\n            <name>pdata<\/name>\n        <\/field>\n        <field type=\"struct prefixan_event\">\n            <name>event<\/name>\n        <\/field>\n        <struct>\n            <struct_name>prefixbuttons_settings<\/struct_name>\n            <field type=\"uint8_t\" size=\"3\">\n                <name>thresholds<\/name>\n            <\/field>\n            <field type=\"uint8_t\">\n                <name>pressed_threshold<\/name>\n            <\/field>\n            <field type=\"struct prefixbuttons_mask\" size=\"code_number\">\n                <name>hash_table<\/name>\n            <\/field>\n            <struct_attributes>__attribute__ ( ( packed ) ) <\/struct_attributes>\n            <struct_nested_name>settings <\/struct_nested_name>\n        <\/struct>\n        <field type=\"uint8_t\" size=\"( ( number ) \/ 8 ) + 1\">\n            <name>raw<\/name>\n        <\/field>\n        <field type=\"uint8_t\" size=\"( ( number ) \/ 8 ) + 1\">\n            <name>raw_mem<\/name>\n        <\/field>\n        <struct_attributes>__attribute__ ( ( packed ) )<\/struct_attributes>\n    <\/struct>\n<\/sc2xml>\n<\/pre>\n<p>Now you can see that the XML file shows exactly the struct defined in the macro <em>def_struct_data<\/em> but with nice XML tags!<\/p>\n<p>I hope this project could be useful, specially if you want a non-trivial example that defines a parser and grammar with lex and yacc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some while ago I wrote a simple C to XML serializer that reads C structs and unions from a header file a writes them to a XML file. You can read that post here. I&#8217;ve improved it a little bit and released it in GitHub. You can check it out here. This program is in\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=200\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,17],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-compiling","category-xml"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":0,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}