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

4. Re: Manage C structures

Icy_Viking said...

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.

sequence r = allocateStructure(rect) 
sequence rs = readStructure(r) 
 
for i = 1 to length(rs)  do 
    printf(1,"%s",{i}) 
end for 

There were many little errors in your test program:

  • you created a structure but didn't write anything in it
  • you read an index {i} instead of a structure member {rs[i]}
  • you printed a sequence (%s) but you wrote integers (%d)

Here is the corrected code.

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)  
writeStructure(r, {10, 20, 50, 100} )  
sequence rs = readStructure(r)  
  
for i = 1 to length(rs)  do  
    printf(1,"%d ",{rs[i]})  
end for  
  
freeStructure(r) 
maybe_any_key() 

Jean-Marc

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

5. Re: Manage C structures

petelomax said...
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?

You are right Pete. I din't realize I declared first item as an int. It should have been a pointer.

Jean-Marc

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

6. Re: Manage C structures

Icy_Viking said...

Although I'm not sure I fully understand.

I'm parsing C code to extract variables, then I convert their type to a pseudo Euphoria type which is independant from OS and architecture (all the T_ types, thanks to Borland Pascal from the 80's) and at least I adapt those pseudo variables to the OS and architecture. So the same code applies to all versions of OpenEuphoria 4 (4.0 and 4.1) on Windows and Linux and on 32-bit and 64-bit.

Jean-Marc

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

7. Re: Manage C structures

Before it gets deprecated by libffi, here is another release in pure Euphoria: http://jean-marc.duro.pagesperso-orange.fr/Structures_v0.2.0_2022-09-26.zip

It can read or write individual items in addition to writing or reading whole simple structures.

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

a = "A string", b = 25.2, c = 'a' 
b = 40.0 
a = "A string", b = 40.0, c = 'a' 
Press Any Key to continue... 

Jean-Marc

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

Search



Quick Links

User menu

Not signed in.

Misc Menu