xml - Building in PC a C struct for an embedded target -


i need extract data xml , link struct c embedded project.

the xml unavailable in runtime. (no file system, file big, , security reasons). have parse , generate struct on pc pre-linkedge.

if use binary data won't necesarilly in same format in target (even compiler same vendor, right?). generate c file compiled within project? there easier way?

struct mystruct s = generatemystruct("file.xml");  s.generatecfile("convertedxml.c"); 

controlling structure layout

if use data types in <stdint.h> (e.g. int32_t, uint8_t) , define fields every byte in struct keep types aligned sizes (e.g. 32-bit values should 4-byte aligned, 16-bit values should 2-byte aligned), should okay , structure's layout should match on both platforms.

for example, don't write struct this:

typedef struct {     uint8_t a;     uint32_t b; } foo; 

because compiler might magically stuff 3 bytes in between a , b b multiple of 4 bytes start of structure. instead, put padding bytes in yourself:

typedef struct {     uint8_t a;     uint8_t padding[3];     uint32_t b; } foo; 

sometimes compilers have extensions controlling packing , alignment. make sure both compilers have these extensions before using them. or ignore extensions , manually pad data above.

generating c initialization code

once you've written code creates structure , fills in data parsed xml, write function generatecfile generate initialization file. should passed not output file or filename, pointer initialized structure. (s.generatecfile("convertedxml.c"); c++ syntax, not c.)

void generatecfile(const struct foo* s, const char* filename) {     file* fp = fopen(filename, "w");      fprintf(fp, "#include \"initfoo.h\"\n\n");     fprintf(fp, "const struct foo kinitfoo = {\n");     fprintf(fp, "    %u, { 0, 0, 0 }, %u\n", s->a, s->b);     fprintf(fp, "};\n");      fclose(fp); } 

by using standard initializer syntax, don't have worry byte order differences between platforms.

you should use generated c file short header file declaring constant structure:

#ifndef initfoo_h #define initfoo_h  #include "foo.h"  extern const struct foo kinitfoo;  #endif 

btw, there's nothing saying code generator has written in same language target's compiler. write code generators in python output c++.

# remember double braces should in output. template = ''' #include "initfoo.h"  const struct foo kinitfoo = {{     {a:d}, {{ 0, 0, 0 }}, {b:d} }}; '''  def generatecfile(foo, filename):     open(filename, "w") f:         f.write(template.format(**foo));  # test code generatecfile({"a": 15, "b": 45}, "convertedxml.c") 

also, higher-level languages python tend have easier use xml or json parsers. ymmv.

for either code generator, , given = 15 , b = 45 in parsed structure, should in convertedxml.c:

#include "initfoo.h"  const struct foo kinitfoo = {     15, { 0, 0, 0 }, 45 }; 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -