将字符串解析为单独的元素

发布于 2024-12-24 00:21:21 字数 1900 浏览 1 评论 0原文

海湾合作委员会 4.6.2 c89

我只是想知道这是否是解析字符串以从该 sdp 字符串中获取各个元素的好方法。

这是字符串:

"v=0\no=sjphone 853596844 1765236571 IN IP4 10.10.10.10\ns=-\nc=IN IP4 10.10.10.10\nt=0 0\nm=audio 19112 RTP/AVP 8\na=rtpmap:8 PCMA/8000\na=ptime:20\n"

我想像这样分隔它:

v=0
o=sjphone 853596844 1765236571 IN IP4 10.10.10.10
s=-
c=IN IP4 10.10.10.10
t=0 0
m=audio 19112 RTP/AVP 8
a=rtpmap:8 PCMA/8000
a=ptime:20

这是我的代码:

void parse_sdp_string(const char *sdp_string)
{
#define MAX_NUM_ELEMENTS 16    
    char elements[MAX_NUM_ELEMENTS][MAX_STRING_LEN] = {{0}};
    apr_size_t element_count = 0;
    apr_size_t i = 0;
    apr_size_t k = 0;

    char sdp_str[MAX_NUM_ELEMENTS * MAX_STRING_LEN] = {0};
    char *search_str = NULL;

    /* Copy the string as to not corrupt the original */
    apr_cpystrn(sdp_str, sdp_string, sizeof sdp_str);
    search_str = sdp_str;

    /* Find the last carriage return to compare that we are at the last one */
    char *end_ptr = strrchr(search_str, '\n');

    /* Increment until you find a carriage return */
    while(*search_str != '\0') {
        /* Fill the element in the array */
        elements[i][k++] = *search_str;
        search_str++;

        /* nul terminate and reset before starting copy next element */
        if(*search_str == '\n') {
            elements[i][++k] = '\0';
            /* Next element */
            i++;
            /* Set back to zero to copy from */
            k = 0;

            /* Check that we are not at the last '\n' */
            if(search_str == end_ptr) {
                /* This is the end, record the number of elements copied */
                element_count = i;
                break;
            }

            /* skip over the current \n as we don't need to copy that */
            search_str++;
        }
    }
}

非常感谢您的任何建议,

gcc 4.6.2
c89

I am just wondering if this is a good way to parse a string to get the individual elements out of this sdp string.

This is the string:

"v=0\no=sjphone 853596844 1765236571 IN IP4 10.10.10.10\ns=-\nc=IN IP4 10.10.10.10\nt=0 0\nm=audio 19112 RTP/AVP 8\na=rtpmap:8 PCMA/8000\na=ptime:20\n"

And I want to separate it like this:

v=0
o=sjphone 853596844 1765236571 IN IP4 10.10.10.10
s=-
c=IN IP4 10.10.10.10
t=0 0
m=audio 19112 RTP/AVP 8
a=rtpmap:8 PCMA/8000
a=ptime:20

This is my code:

void parse_sdp_string(const char *sdp_string)
{
#define MAX_NUM_ELEMENTS 16    
    char elements[MAX_NUM_ELEMENTS][MAX_STRING_LEN] = {{0}};
    apr_size_t element_count = 0;
    apr_size_t i = 0;
    apr_size_t k = 0;

    char sdp_str[MAX_NUM_ELEMENTS * MAX_STRING_LEN] = {0};
    char *search_str = NULL;

    /* Copy the string as to not corrupt the original */
    apr_cpystrn(sdp_str, sdp_string, sizeof sdp_str);
    search_str = sdp_str;

    /* Find the last carriage return to compare that we are at the last one */
    char *end_ptr = strrchr(search_str, '\n');

    /* Increment until you find a carriage return */
    while(*search_str != '\0') {
        /* Fill the element in the array */
        elements[i][k++] = *search_str;
        search_str++;

        /* nul terminate and reset before starting copy next element */
        if(*search_str == '\n') {
            elements[i][++k] = '\0';
            /* Next element */
            i++;
            /* Set back to zero to copy from */
            k = 0;

            /* Check that we are not at the last '\n' */
            if(search_str == end_ptr) {
                /* This is the end, record the number of elements copied */
                element_count = i;
                break;
            }

            /* skip over the current \n as we don't need to copy that */
            search_str++;
        }
    }
}

Many thanks for any suggestions,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-12-31 00:21:21

我会考虑使用strtok()。它将为您节省大量代码。

I would consider using strtok(). It will save you a lot of code.

夜无邪 2024-12-31 00:21:21

使用sscanf()

有了它,您可以执行以下操作:

int i, n;
float x;
char name[50];
n = sscanf("25 54.32E-1 Hamster", "%d%f%s", &i, &x, name);

结果为 i = 25,x = 54.32E-1,name =“Hamster”。

Use sscanf().

With it you can do things like:

int i, n;
float x;
char name[50];
n = sscanf("25 54.32E-1 Hamster", "%d%f%s", &i, &x, name);

which results in i = 25, x = 54.32E-1, name = "Hamster".

十雾 2024-12-31 00:21:21

使用 strtokstrstr 等函数将使您的生活变得更加轻松。实际上几乎所有属于 string.h 的“搜索”组的内容 很有用。

Using functions like strtok and strstr will make your life a lot easier. Actually almost everything that belongs to the "Searching" group of string.h can be of great use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文