开发者

split dvb url address into separate values

Let assume we have such URL:

dvb://1.3f3.255c.15

my question is how to parse this address in following way:

val1 = 1
val2 = 3f3
val3 = 255c
val4 = 15

Firs开发者_StackOverflowt idea is to use strchr() from standard C library, but maybe done it before. I would like to make it as simple as possible. When I will succeed I will put my solution.

Best Regards


You could use strtok

char dvb_url[] = "dvb://1.3f3.255c.15";

//Begin stripping the scheme (dvb:/)
char *tokenized = strtok(dvb_url, "/");
//Finish stripping scheme
tokenized = strtok(NULL, "/.");

while(tokenized != NULL)
{
    //Store in variable here            
    printf("%s\n", tokenized);
    tokenized = strtok(NULL, ".");
}


There is a very simple answer if your pattern is fixed. I don't how many people use it like this.

sscanf() is very handy here :)

char *dvb_string = "dvb://1.3f3.255c.15";
char proto[10], output1[10], output2[10], output3[10], output4[10];
sscanf(dvb_string, "%[^:]://%[^.].%[^.].%[^.].%s", proto, output1, output2, output3, output4);
fprintf(stderr, "%s -- > %s %s %s %s %s\n", dvb_string, proto, output1, output2, output3, output4);

OUTPUT:

dvb://1.3f3.255c.15 -- > dvb 1 3f3 255c 15

Simple as this :) enjoy C.

Regards


I think the strchr() function is handy enough:

char *str = "dvb://1.3f3.255c.15";
char val[10][256];
char *cur = str;
int i = 0;

for ( ; cur = strchr(str, '.'); cur != NULL && i < 10)
{
    strncpy(val[i], cur, 256);
    if(strchr(val[i], '.') != NULL)
        strchr(val[i], '.') = '\0';
    ++i;
}

You should end up with your values in val[0 to N].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜