ant2009
ant2009

Reputation: 22486

parsing a string into separate strings

gcc 4.6.1 c89

I have a string that I need parse. The string is sdp information. I am trying to seperate each of the elements into seperate strings.

Basically the string would contain some thing like this:

v=0 o=sip_user IN 10230 22472 IP4 10.10.10.44 s=SIP_CALL c=IN IP4 10.10.10.44 m=audio 49152 RTP/AVP 0 8 a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000

And I would like to seperate them into different strings. So this needs to be broken down into seperate strings.

str1 v=0 
str2 o=sip_user IN 10230 22472 IP4 10.10.10.44 
str3 s=SIP_CALL 
str4 c=IN IP4 10.10.10.44 
str5 m=audio 49152 RTP/AVP 0 8 
str6 a=rtpmap:0 PCMU/8000 
str7 a=rtpmap:8 PCMA/8000

I was trying to do something like this as a start. But even though it finds the v= I need to find when the next line will start.

char *str_v = NULL  
str_v = strstr(sdp_string, "v=");

if(str_v != NULL) {
    printf("found line: [ %s ]", str_v);
}

Many thanks for any suggestions,

Upvotes: 1

Views: 202

Answers (5)

u0b34a0f6ae
u0b34a0f6ae

Reputation: 49793

First, find the beginnings of all substrings (parts array below), then null terminate each of them. This is just to show how simple it can be to parse if the format is fixed.

#include <stdio.h>
#include <string.h>

int main() {
        char input[] = "v=0 o=sip_user IN 10230 22472 IP4 10.10.10.44 s=SIP_CALL c=IN IP4 10.10.10.44 m=audio 49152 RTP/AVP 0 8 a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000";
        char *parts[16] = {0};
        int top = 0;
        int i;
        char *s = input;
        while (top < 15 && (s = strchr(s, '='))) {   /* find each = and save a pointer to before it */
                parts[top++] = s-1;
                s++;
        }
        for (i = 1; parts[i]; i++)
                parts[i][-1] = '\0';  /* null terminate each part */
        for (i = 0; parts[i]; i++)
                printf("%s\n", parts[i]);
        return 0;
}

Upvotes: 2

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char source[] ="v=0 o=sip_user IN 10230 22472 IP4 10.10.10.44 s=SIP_CALL c=IN IP4 10.10.10.44 m=audio 49152 RTP/AVP 0 8 a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000";
    char *str[7];
    char *pf=source, *ps;
    int i;
    for(i=0;i<7;i++){
        pf = strchr(pf, '=');
        ps = strchr(pf+1, '=');
        if(ps == NULL)
            ps = source + sizeof(source)/sizeof(char);
        else
            ps -=2;
        pf -= 1;
        str[i]=(char*)malloc((ps - pf + 1)*sizeof(char));
        strncpy(str[i], pf, ps - pf);
        pf+=2;;
    }
    for(i=0;i<7;i++)
        printf("str%d %s\n", i+1, str[i]);
    for(i=0;i<7;i++)
        free(str[i]);
    return 0;
}

Upvotes: 2

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Use strtok http://husnusensoy.wordpress.com/2007/01/13/strtok-for-c-guys/

you can use strtok to split the spaces and then again in that loop split the ='s

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

Use strchr to find the '=', then search backwards from that point to the first space. Then you know when the next string starts. Use strndup (not the 'n' in the function name) to create the strings. Continue until you reach end of original string.

If you use strndup or any other function to allocate memory, remember to free the memory later.

Upvotes: 1

fat
fat

Reputation: 7103

strtok is reasonable.

Upvotes: 1

Related Questions