1. Manage C structures

I have updated my previous structure manager so that it can use C syntax. It is available here: http://jean-marc.duro.pagesperso-orange.fr/Structures_v0.1.0_2022-09-23.zip

Here is the example code:

include std/dll.e 
include std/machine.e 
include std/console.e 
include common.e 
include structs.e 
 
sequence myStructure =  
"struct _mydata {" & 
"  int a;" & 
"  float b;" & 
"  char c;" & 
"} bar;" 
 
f_debug = open("debug.log", "w") 
debug_level = VERBOSE 
 
sequence struct = allocateStructure(myStructure) 
writeStructure(struct, {allocate_string("A string"), 12, 25.2} ) 
sequence s = readStructure(struct) 
printf(1, "s2[1] = %s, s2[2] = %d, s2[3] = %4.1f\n", {peek_string(s[1]), s[2], s[3]}) 
freeStructure(struct) 
 
close(f_debug) 
maybe_any_key() 

The parser is ready for nested structures and unions (see decompose_struct.ex, result below) but the manager can only deal with simple structures yet: one level, no union, no nesting.

sequence myStructure =  
"struct _mydata {" & 
"    int which_one;" & 
"    union _data {" & 
"            int a;" & 
"            float b;" & 
"            char c;" & 
"    } foo;" & 
"} bar;" 

vars = 
  [SU_TYPE] STRUCT 
  [SU_TYPE_NAME] "_mydata" 
  [SU_VAR_NAME] "bar" 
  [SU_BLOCK] 
  .  [1] "int which_one;" 
  .  [2] 
  .  .  [SU_TYPE] UNION 
  .  .  [SU_TYPE_NAME] "_data" 
  .  .  [SU_VAR_NAME] "foo" 
  .  .  [SU_BLOCK] {"int a;", "float b;", "char c;"} 

Jean-Marc

new topic     » topic index » view message » categorize

2. Re: Manage C structures

This is pretty neat. Although I'm not sure I fully understand. I can't get the one simple example test I wrote to work correctly.

include std/dll.e 
include std/machine.e 
include std/console.e 
 
include common.e 
include structs.e 
 
sequence rect = "struct _rect {" & 
		"int x;" & 
		"int y;" & 
		"int w;" & 
		"int h;" & 
		"} RECT;" 
		 
 
sequence r = allocateStructure(rect) 
sequence rs = readStructure(r) 
 
for i = 1 to length(rs)  do 
    printf(1,"%s",{i}) 
end for 
 
freeStructure(r) 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Manage C structures

jmduro said...
writeStructure(struct, {allocate_string("A string"), 12, 25.2} ) 

Hmm, isn't that putting ptr/int/float into an int/float/char?
Also, wouldn't triple-quotes be better?

Icy_Viking said...
for i = 1 to length(rs)  do 
    printf(1,"%s",{i}) 
end for 

That's never going to work, maybe you meant printf(1,"%s",{rs[i]}), or printf(1,"%d ",{rs[i]}), or why not just try ?rs

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu