Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse string for struct member name

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Is there a way to parse a string and use that information to fill the corresponding member in a structure? So, for example:

struct some_stuff {
int abc;
int def;
} stuff;

string = "def = 99";

after parsing:
member = "def";
memval = 99;

stuff."member" = 99;

Does that make sense? Thanks in advance!!!

 
Only by creating in advance some kind of table which associates strings with structure members.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Can you give me an example of what you mean by "associates strings with structure members"?

Thank you much!
 
More importantly, why would you want to do that?
If you explain what you're trying to accomplish, we might be able to suggest a better solution.
 
I am basically creating an XML Reader that will populate my data structure (there are some utilities out there that do this, but I can only seem to find ones that are reading from an XML file...I'm reading from a stream (string)) and have gotten to the point where I can parse out the data and the tag names, but I haven't thought of a convenient way to know which member's data was parsed...other than having a huge IF statement that says "if (strcmp(tag,"def")==0) {", and so on... SO, I was thinking that if I could somehow translate the "def" to map directly to a member in my structure, I could have one line like "mystruct.'member' = 'data'".

The more I think about it, the more this doesn't seem reasonable to me...so, if you have other suggestions, please let me know.

By the way, I will be writing an XML Writer as well...so, if you have any insight into the mapping from the struct member name to a tag, that would be good too ;-)
 
Sounds like you're looking for a map.
If you were using C++ it would be simple: std::map<string, string>
But if you're really stuck with using C, try searching for some C map implementations.
 
What about a slightly more XML friendly data structure?
Code:
struct xmlvar {
    struct xmlvar  *next;
    char           *name;
    int             value;
};

struct xmlvar *xmlvarlist = NULL;

As you read each XML attribute, you allocate another xmlvar struct and append it to the list. You can even make it tree-like so it replicates the XML hierarchy as well, which may be useful when it comes to writing it back out.

Accessing a value is a bit more complicated than
[tt]foo = stuff.abc;[/tt]
but is a very generalised
[tt]foo = readVar(xmlvarlist,"abc");[/tt]

Writing it out again is dead easy as well, you just walk the list / tree you created and or modified.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Better try one of trustworthy (and free) XML parsing libraries for C (expat, for example). Don't invent wheels...
 
Although I normally would agree with you, ArkM, about not inventing wheels, expat just seems so complicated to me. Parsing XML...or ANY string, for that matter...is pretty trivial...as long as the XML is not too complicated. All I want to do is parse each element and load a structure.
 
The problem is, XML only guarantees semantic well-formedness. That still leaves quite a lot of presentation flexibility which might bite at some point.

If this is a one-off, and you have good control over the presentation as well, then a simple string matching approach would work.

But if neither of these things is true, then getting to know expat or something similar, is definitely worth the effort IMO. Plus, once you're actually familiar with it, then you can use that knowledge in say Perl (if you wanted to), which also has an expat interface. That's "leverage" if you want a buzzword.

A bit of practice with outline.c should get you up and running.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top