1

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,

2
  • 2
    Are you sure you want to do this in C? Sounds like a job for a good scripting language. Commented Dec 8, 2011 at 11:05
  • I didn't think it would be that difficult. Just wanted an idea how to get started. Thanks. Commented Dec 8, 2011 at 11:15

5 Answers 5

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2
#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;
}

Comments

1

strtok is reasonable.

Comments

1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.