473,287 Members | 1,581 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,287 software developers and data experts.

How to pass this string into a linked list ?

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_
{
double argval;
struct params_ * next;
};
Could anyone suggest an elegant way to parse strings with the specified
formatting into a linked list (struct mst_) ?
Nov 7 '08 #1
6 4709
On Nov 7, 2:24 am, "(2b|!2b)==?" <void-s...@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_
{
double argval;
struct params_ * next;

};

Could anyone suggest an elegant way to parse strings with the specified
formatting into a linked list (struct mst_) ?
Not quite sure i understood the string format supplied.
"id1:param1,param2;id2:param1,param2,param3;id "
as in?
"1:1.1,2.2;2:3.3,4.4,5.5;id"

i see the integer id's and the double argval's
but whats id at the end here?
or did you mean
"id1:param1,param2;id2:param1,param2,param3;id3:pa ram1"

Nov 7 '08 #2
On Nov 7, 7:24*am, "(2b|!2b)==?" <void-s...@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_
{
* * double argval;
* * struct params_ * next;

};

Could anyone suggest an elegant way to parse strings with the specified
formatting into a linked list (struct mst_) ?
Here is one way to do so:

#include <stdio.h>
#include <malloc.h>

struct params
{
double arg;
params* next;
};

struct mst
{
int id;
params* params;
mst* next;
};

void mst_print(FILE* file, mst* head)
{
for(;head; head = head->next)
{
fprintf(file, "%d", head->id);
if(head->params)
{
char sep = ':';
for(params* param = head->params; param; param = param-
>next)
{
fprintf(file, "%c%f", sep, param->arg);
sep = ',';
}
}
fprintf(file, ";");
}
}

mst* mst_destroy(mst* head)
{
while(head)
{
mst* node = head;
head = head->next;
for(params* node_params = node->params; node_params;)
{
params* p = node_params;
node_params = node_params->next;
free(p);
}
free(node);
}
return NULL;
}

mst* mst_parse(char const** input)
{
mst *head = NULL, **tail = &head;
while(*input)
{
// parse id
int id, eaten = 0;
sscanf(*input, "%d%n", &id, &eaten);
if(!eaten)
return head;
*input += eaten;
// allocate the next mst node and initialise it
mst* next = (mst*)malloc(sizeof(mst));
if(!next)
return mst_destroy(head);
next->id = id;
next->params = NULL;
next->next = NULL;
// append the node to the list
*tail = next;
tail = &next->next;
// parse params
if(':' == **input)
{
params** tail_params = &next->params;
++*input;
for(;;)
{
double arg;
eaten = 0;
sscanf(*input, "%lf%n", &arg, &eaten);
if(!eaten)
return head;
*input += eaten;
// allocate the next params node and initialise it
params* next_params =
(params*)malloc(sizeof(params));
if(!next_params)
return mst_destroy(head);
next_params->arg = arg;
next_params->next = NULL;
// append to the params list
*tail_params = next_params;
tail_params = &next_params->next;
// see if there are more params
if(',' != **input)
break;
++*input;
}
}
// see if there are more ids
if(';' != **input)
break;
++*input;
}
return head;
}

char const* inputs[] = {
""
, "1"
, "1:1.0"
, "1:1.0,2.0"
, "1:1.0,2.0;2:11.0,22.0;"
, "1:1.0,2.0;2:11.0,22.0;qwerty"
, NULL
};

int main()
{
for(char const** i = inputs; *i; ++i)
{
char const* input = *i;
printf("parsing '%s': ", input);
mst* head = mst_parse(&input);
if(*input)
printf("<choked at postion %d", (int)(input - *i));
mst_print(stdout, head);
mst_destroy(head);
printf("\n");
}
}

Output:
parsing '':
parsing '1': 1;
parsing '1:1.0': 1:1.000000;
parsing '1:1.0,2.0': 1:1.000000,2.000000;
parsing '1:1.0,2.0;2:11.0,22.0;':
1:1.000000,2.000000;2:11.000000,22.000000;
parsing '1:1.0,2.0;2:11.0,22.0;qwerty': <choked at postion 22>
1:1.000000,2.000000;2:11.000000,22.000000;

--
Max
Nov 7 '08 #3
Maxim Yegorushkin wrote:
On Nov 7, 7:24 am, "(2b|!2b)==?" <void-s...@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_
{
double argval;
struct params_ * next;

};

Could anyone suggest an elegant way to parse strings with the specified
formatting into a linked list (struct mst_) ?

Here is one way to do so:

#include <stdio.h>
#include <malloc.h>

struct params
{
double arg;
params* next;
};

struct mst
{
int id;
params* params;
mst* next;
};

void mst_print(FILE* file, mst* head)
{
for(;head; head = head->next)
{
fprintf(file, "%d", head->id);
if(head->params)
{
char sep = ':';
for(params* param = head->params; param; param = param-
>next)
{
fprintf(file, "%c%f", sep, param->arg);
sep = ',';
}
}
fprintf(file, ";");
}
}

mst* mst_destroy(mst* head)
{
while(head)
{
mst* node = head;
head = head->next;
for(params* node_params = node->params; node_params;)
{
params* p = node_params;
node_params = node_params->next;
free(p);
}
free(node);
}
return NULL;
}

mst* mst_parse(char const** input)
{
mst *head = NULL, **tail = &head;
while(*input)
{
// parse id
int id, eaten = 0;
sscanf(*input, "%d%n", &id, &eaten);
if(!eaten)
return head;
*input += eaten;
// allocate the next mst node and initialise it
mst* next = (mst*)malloc(sizeof(mst));
if(!next)
return mst_destroy(head);
next->id = id;
next->params = NULL;
next->next = NULL;
// append the node to the list
*tail = next;
tail = &next->next;
// parse params
if(':' == **input)
{
params** tail_params = &next->params;
++*input;
for(;;)
{
double arg;
eaten = 0;
sscanf(*input, "%lf%n", &arg, &eaten);
if(!eaten)
return head;
*input += eaten;
// allocate the next params node and initialise it
params* next_params =
(params*)malloc(sizeof(params));
if(!next_params)
return mst_destroy(head);
next_params->arg = arg;
next_params->next = NULL;
// append to the params list
*tail_params = next_params;
tail_params = &next_params->next;
// see if there are more params
if(',' != **input)
break;
++*input;
}
}
// see if there are more ids
if(';' != **input)
break;
++*input;
}
return head;
}

char const* inputs[] = {
""
, "1"
, "1:1.0"
, "1:1.0,2.0"
, "1:1.0,2.0;2:11.0,22.0;"
, "1:1.0,2.0;2:11.0,22.0;qwerty"
, NULL
};

int main()
{
for(char const** i = inputs; *i; ++i)
{
char const* input = *i;
printf("parsing '%s': ", input);
mst* head = mst_parse(&input);
if(*input)
printf("<choked at postion %d", (int)(input - *i));
mst_print(stdout, head);
mst_destroy(head);
printf("\n");
}
}

Output:
parsing '':
parsing '1': 1;
parsing '1:1.0': 1:1.000000;
parsing '1:1.0,2.0': 1:1.000000,2.000000;
parsing '1:1.0,2.0;2:11.0,22.0;':
1:1.000000,2.000000;2:11.000000,22.000000;
parsing '1:1.0,2.0;2:11.0,22.0;qwerty': <choked at postion 22>
1:1.000000,2.000000;2:11.000000,22.000000;

--
Max
Thanks Maxim. This is exactly what I needed.
Nov 7 '08 #4
Maxim Yegorushkin wrote:
On Nov 7, 7:24 am, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
>I am expecting a string of this format:
<snip></snip>

Maxim: incidentally, there was a typo in the subject. I had meant to
type 'parse' (instead of pass) - but you understood anyway and provided
a useful solution to the question - many tx.
Nov 7 '08 #5
On Nov 7, 10:33*am, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
Maxim Yegorushkin wrote:
On Nov 7, 7:24 am, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
I am expecting a string of this format:

<snip></snip>

Maxim: incidentally, there was a typo in the subject. I had meant to
type 'parse' (instead of pass) - but you understood anyway and provided
a useful solution to the question - many tx.
Ok.

And there is a typo in mst_parse:

while(*input)

Should be:

while(**input)

;)

--
Max

Nov 7 '08 #6
I am expecting a string of this format:

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

The tokens are seperated by semicolon ";"
Just as an reminder:

In some countries the decimal point in 456.23 is replaced with ','
like in 456,23.
Some languages, apps and locals do regognize that.
So just think about if ',' is a good seperator at all.

My 2 cents.
eiji
Nov 7 '08 #7

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

Similar topics

3
by: dk | last post by:
Hi all, Would appreciate some advice on the following: I am trying to speed up an Access database connected to a SQL Server back-end. I know I can use a pass-through query to pass the sql...
1
by: linux.lover | last post by:
hello, I have defined kernel data structure in LINUX kernel source code and written linked list functionalities in kernel source file. My data structure is struct node { char index; char...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
5
by: Jamie | last post by:
Hello Newsgroup: I'm not a C programmer, though I've dabbled on and off through the years. (wish I could justify doing more in C/C++ because it is enjoyable, just highly time consuming compared...
7
by: semut | last post by:
Given that the string is of null-terminated type. What could be the possible causes (by experience) the string to have no null character (\0) and cause buffer overflow later. I know it is quite...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
6
by: Poke386 | last post by:
I'm in the process of making an text based-rpg in C++. Its just a little project so I can learn some object-oriented programming, nothing serious. My problem is that I've created a class like so: ...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.