D
David Mathog
I have been doing a bit of work lately with Windows Enhanced
Metafiles. The structures for these are defined in an include
wingdi.h and for most structures there are corresponding GDI get/set
functions. Imagine for a minute that you wanted to write basic get/
set functions in C from scratch with as little work as possible. It
seems like it should be (largely) possible to do so automatically by
recursively processing the structs. This cannot be an original idea,
has anybody ever seen this done before?
This is what I mean. starting from lines like these in the header
file (pointer variants of struct names removed for clarity):
typedef struct POINTL { LONG x; LONG y; } POINTL;
typedef struct RECTL { LONG left; LONG top; LONG right; LONG bottom; }
RECTL;
typedef struct tagEMR { DWORD iType; DWORD nSize; } EMR;
typedef struct tagEMRARC { EMR emr; RECTL rclBox; POINTL ptlStart;
POINTL ptlEnd; }
EMRARC,EMRARCTO,, EMRCHORD, EMRPIE;
So for instance:
int getEMR(void *ptr, DWORD *iType, DWORD *nsize){
offset=0;
offset+=getDWORD(ptr+offset,iType);
offset+=getDWORD(ptr+offset,nsize);
return(offset);
}
int getEMRPIE(void *ptr, EMR **emr, RECTL **rclBox, POINTL **
ptlStart, POINTL **ptlEnd){
int offset=0;
offset += getEMR(ptr+offset,emr);
offset += getRECTL(ptr+offset,rclBox);
offset += getPOINTL(ptr+offset,ptlstart);
offset += getPOINTL(ptr+offset,ptlstart);
return(offset);
}
The set functions would be pretty similar except with EMR *emr, RECTL
*rclBox and so forth.
I understand that in this particular set of structs there is a further
complication because some fields are the offsets to data (from the
ptr) and not actually the data itself. For those one would recurse
through structs that are read and set up the function with extra "get"
calls and parameters. For instance, the EMRTEXT struct has two such
offsets:
offString offset to the characters
offDx offset to the intercharacter spacing array
and any struct that references an EMRTEXT would automatically add
those parameters to its call list and would add get functions to
retrieve them. However, since the data type associated with the
offset is always
an integer, it would put in something like this for the programmer to
fix up later:
#typedef TYPE_offString /* FIXME *./
#typedef TYPE_offDx /* FIXME */
int getSOMESTRUCT(.....TYPE_offString **offString, TYPE_offDx
**offDx){
....
offset+=getNOTDEFINEDYET(ptr,offString); /* FIXME, may need
count */
offset+=getNOTDEFINEDYET(ptr,offDx); /* FIXME, may need count */
return(offset);
}
Some work would still be necessary, like writing the basic things such
as "getDWORD", but this approach should automate at least 95% of the
work.
Thoughts?
Metafiles. The structures for these are defined in an include
wingdi.h and for most structures there are corresponding GDI get/set
functions. Imagine for a minute that you wanted to write basic get/
set functions in C from scratch with as little work as possible. It
seems like it should be (largely) possible to do so automatically by
recursively processing the structs. This cannot be an original idea,
has anybody ever seen this done before?
This is what I mean. starting from lines like these in the header
file (pointer variants of struct names removed for clarity):
typedef struct POINTL { LONG x; LONG y; } POINTL;
typedef struct RECTL { LONG left; LONG top; LONG right; LONG bottom; }
RECTL;
typedef struct tagEMR { DWORD iType; DWORD nSize; } EMR;
typedef struct tagEMRARC { EMR emr; RECTL rclBox; POINTL ptlStart;
POINTL ptlEnd; }
EMRARC,EMRARCTO,, EMRCHORD, EMRPIE;
So for instance:
int getEMR(void *ptr, DWORD *iType, DWORD *nsize){
offset=0;
offset+=getDWORD(ptr+offset,iType);
offset+=getDWORD(ptr+offset,nsize);
return(offset);
}
int getEMRPIE(void *ptr, EMR **emr, RECTL **rclBox, POINTL **
ptlStart, POINTL **ptlEnd){
int offset=0;
offset += getEMR(ptr+offset,emr);
offset += getRECTL(ptr+offset,rclBox);
offset += getPOINTL(ptr+offset,ptlstart);
offset += getPOINTL(ptr+offset,ptlstart);
return(offset);
}
The set functions would be pretty similar except with EMR *emr, RECTL
*rclBox and so forth.
I understand that in this particular set of structs there is a further
complication because some fields are the offsets to data (from the
ptr) and not actually the data itself. For those one would recurse
through structs that are read and set up the function with extra "get"
calls and parameters. For instance, the EMRTEXT struct has two such
offsets:
offString offset to the characters
offDx offset to the intercharacter spacing array
and any struct that references an EMRTEXT would automatically add
those parameters to its call list and would add get functions to
retrieve them. However, since the data type associated with the
offset is always
an integer, it would put in something like this for the programmer to
fix up later:
#typedef TYPE_offString /* FIXME *./
#typedef TYPE_offDx /* FIXME */
int getSOMESTRUCT(.....TYPE_offString **offString, TYPE_offDx
**offDx){
....
offset+=getNOTDEFINEDYET(ptr,offString); /* FIXME, may need
count */
offset+=getNOTDEFINEDYET(ptr,offDx); /* FIXME, may need count */
return(offset);
}
Some work would still be necessary, like writing the basic things such
as "getDWORD", but this approach should automate at least 95% of the
work.
Thoughts?