By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

How to parse this string into a list structure?

P: n/a
I am expecting a string of this format:

"id1:param1,param2;id2:param1,param2,param3;id "

The tokens are seperated by semicolon ";"

However each token is really a struct of the following format:

struct mst_
{
int id;
struct params_* parms ; //0 or more
struct mst_ *next ;
};
where:

struct params_
{
float argval;
struct params_ * next;
};
Nov 7 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Fri, 07 Nov 2008 02:36:04 +0000, "(2b|!2b)==?"
<vo*******@ursa-major.comwrote:
>I am expecting a string of this format:

"id1:param1,param2;id2:param1,param2,param3;id "

The tokens are seperated by semicolon ";"

However each token is really a struct of the following format:

struct mst_
{
int id;
struct params_* parms ; //0 or more
struct mst_ *next ;
};
where:

struct params_
{
float argval;
struct params_ * next;
};
Try to remember to put the question ( How to parse this string into a
list structure?) in your text, not just in your subject.

With regard to parsing: If you are ABSOLUTELY certain all the strings
are well formed and if the string is modifiable, you could use strtok.
If either of the previous conditions is false, or if you think you
might need to reprocess the string, or if you just don't like strtok,
you could use strchr to find your separators and sscanf to extract the
fields of interest. If there is a possibility that some of the fields
will contain invalid data, you might want to convert each field into a
string and use the appropriate strto_ functions which will let you
check for errors. (Not part of your question but why is argval a
float and not a double?)

With regard to the linked list: If you KNOW the maximum number of ids
and the maximum number of total parameters, you could define an array
of struct mst_ and an array of struct params_, each with the required
number of elements, and set the pointers to the address of each
element as appropriate. If you don't know the maximums or if the
numbers are potentially larger than you want to define arrays for, you
can use malloc to allocate space for each structure as you decide you
have something to store in it. In either case, algorithms for
chaining nodes to a linked list are mostly language independent
(consequently slightly off topic) and can be found in almost any data
structures text. Implementations of these algorithms have been
discussed repeatedly in this newsgroup (google is at least one source
of archived messages).

--
Remove del for email
Nov 7 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.