473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

adressing a structure

I have a structure that looks like this:

struct parameterSchedule_t {

uint16_t waterFlow,
uint16_t timerBase,
uint16_t safetyDelay,
uint16_t pressureRiseTimer,
uint16_t stallTimer,
uint8_t autoRestartPower,
uint8_t autoRestartTemp,
uint8_t compassOverride,
uint8_t Group,
uint8_t hmiAddress,
uint8_t plcAddress,
char systemDate[32],
char systemTime[32],
};

This represents the layout in an XML file. I also have an array of XML
elements that looks similar:

char *elements[] = {
"waterFlow",
"timerBase",
"safetyDelay",
"pressureRiseTimer",
"stallTimer",
"autoRestartPower",
"autoRestartTemp",
"compassOverride",
"Group",
"hmiAddress",
"plcAddress",
"systemDate",
"systemTime",
};

What I want to do is to recurse through the XML tree, and somehow look
up the appropriate element in the structure to match the XML element I
just pulled out.... (too much time spent with PHP recently....)

I can't quite figure out how to do that without duplicating a lot of
information. The goal is to be easily maintainable so I am looking for
some what that doesn't require a minor change to touch several header
and code files... Ideally something that can either generate the
structure and array from #defines or something that can be used to get
the correct structure member at runtime....

--Yan
Sep 21 '06 #1
1 1551
CptDondo schrieb:
I have a structure that looks like this:

struct parameterSchedule_t {

uint16_t waterFlow,
uint16_t timerBase,
uint16_t safetyDelay,
uint16_t pressureRiseTimer,
uint16_t stallTimer,
uint8_t autoRestartPower,
uint8_t autoRestartTemp,
uint8_t compassOverride,
uint8_t Group,
uint8_t hmiAddress,
uint8_t plcAddress,
char systemDate[32],
char systemTime[32],
};

This represents the layout in an XML file. I also have an array of XML
elements that looks similar:

char *elements[] = {
"waterFlow",
"timerBase",
"safetyDelay",
"pressureRiseTimer",
"stallTimer",
"autoRestartPower",
"autoRestartTemp",
"compassOverride",
"Group",
"hmiAddress",
"plcAddress",
"systemDate",
"systemTime",
};

What I want to do is to recurse through the XML tree, and somehow look
up the appropriate element in the structure to match the XML element I
just pulled out.... (too much time spent with PHP recently....)

I can't quite figure out how to do that without duplicating a lot of
information. The goal is to be easily maintainable so I am looking for
some what that doesn't require a minor change to touch several header
and code files... Ideally something that can either generate the
structure and array from #defines or something that can be used to get
the correct structure member at runtime....
This will not be easy and depends entirely on what you really need.

If there will be only one object of the struct type, then you can
proceed along the lines of
#define stringize(s) #s
#define XSTR(s) stringize(s)

enum eType { eTypeU16, .... };
#define UINT16 eTypeU16, uint16_t
....
struct lookupEntry {
const char *key;
void* data;
size_t elemSize;
size_t numElem;
eType type;
};
#define createEntry(key, etype, type, num) {\
XSTR(key), \
NULL, \
sizeof (type), \
num, \
etype \
}

struct lookupEntry AbstractStruct[] = {
createEntry(waterFlow, UINT16, 1),
createEntry(timerBase, UINT16, 1),
....
createEntry(systemTime, CHAR, 32)
};

.....
for (i = 0; i < sizeof AbstractStruct/sizeof AbstractStruct[0]; i++)
{
AbstractStruct[i].data = malloc(elemSize * numElem);
if (NULL == data) {
....
}
....
}

After that, you only are stuck with access code depending
on enum eType.
You cannot generate entries in two types/objects at the
same time via #define, so this is as good as it gets.

If you have more than this one object, then it probably
is better to generate the struct and lookup externally.
Then you can create an ideal hash for your key XML elements
such that lookup works easily.
Then you can index your access operations according to the
hash.
In the simplest case, your programme creates something like
that:
typedef void (*pStoreElement)(struct parameterSchedule_t*,
const char *);
pStoreElement[ELEMENTS+1] = {
store_Invalid,
store_waterFlow,
....
};
void storeElement (struct parameterSchedule_t* obj,
const char *key,
const char *value)
{
pStoreElement[lookup(key)](obj, value);
}
where you can start with lookup():
int lookup (const char *key)
{
for (i = 0; i < ELEMENTS; ++i) {
if (!strcmp(keyTable[i], key))
return i+1;
}
return 0;
}
and where the functions store_* are simple access operations:
void store_waterFlow(struct parameterSchedule_t* obj,
const char *value)
{
unsigned long val = strtoul(value, NULL, 10);
if (val < 17 || val 53) {
val = 0;
}
obj->waterFlow = val;
}

Depending on your application, there may be a much better
approach -- or maybe a programming language that is much better
suited to the problem.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
0
by: Erik Juhlin | last post by:
Hello! I use wse to implement ws-security and everything works just fine and the SOAP-Header looks nice. Though I want to remove the elements that involve adressing (or not include them). But I...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.