C查询字符串解析 [英] C query string parsing

查看:117
本文介绍了C查询字符串解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询字符串

address=1234&port=1234&username=1234&password=1234&gamename=1234&square=1234&LOGIN=LOGIN

我正在尝试将其解析为不同的变量:地址,端口,用户名,密码,游戏名,方块和命令(将保留LOGIN)

I am trying to parse it into different variables: address,port,username,password,gamename,square and command (which would hold LOGIN)

我当时正在考虑使用strtok,但我认为它不会起作用.如何解析字符串以捕获变量?

I was thinking of using strtok but I don't think it would work. How can I parse the string to capture the variables ?

PS-部分字段可能为空-未提供游戏名称或正方形

P.S - some of the fields might be empty - no gamename provided or square

推荐答案

在解析可能在定界符之间包含空字段的字符串时,不能使用strtok,因为strtok将把任意数量的顺序定界符视为一个定界符.

When parsing a sting that may contain an empty-field between delimiters, strtok cannot be used, because strtok will treat any number of sequential delimiters as a single delimiter.

因此,在您的情况下,如果 variable = values 字段在'&'分隔符之间也可能包含空字段,则必须使用strsep或其他功能,例如strcspnstrpbrk或简单地strchr以及几个指针可以沿着字符串向下移动.

So in your case, if the variable=values fields may also contain an empty-field between the '&' delimiters, you must use strsep, or other functions such as strcspn, strpbrk or simply strchr and a couple of pointers to work your way down the string.

strsep函数是BSD函数,可能不包含在您的C库中. GNU包含了strsep,它被设想为替代strtok,只是因为strtok无法处理空字段.

The strsep function is a BSD function and may not be included with your C library. GNU includes strsep and it was envisioned as a replacement for strtok simply because strtok cannot handle empty-fields.

(如果没有strsep可用,则只需保留 start end 指针,并使用strchr之类的函数来定位每个指针'&'的出现,将 end 指针设置为定界符之前的一个,然后从 start 和<之间的字符获取 var = value 信息. em> end 指针,然后将两者都更新为指向定界符后的一个并重复.)

(If you do not have strsep available, you will simply need to keep a start and end pointer and use a function like strchr to locate each occurrence of '&' setting the end pointer to one before the delimiter and then obtaining the var=value information from the characters between start and end pointer, then updating both to point one past the delimiter and repeating.)

在这里,您可以将strsep"&\n"分隔符一起使用,以定位每个'&'(假定包含'\n'字符,则假定是从面向 line _ 的文件中读取该行的. >输入功能,例如fgets或POSIX getline).然后,您可以简单地调用strtok,使用"="作为分隔符,从strsep返回的每个令牌中解析 var = value 文本('\n'已从最后一个令牌中删除用strsep解析时)

Here, you can use strsep with a delimiter of "&\n" to locate each '&' (the '\n' char included presuming the line was read from a file with a line-oriented input function such as fgets or POSIX getline). You can then simply call strtok to parse the var=value text from each token returned by strsep using "=" as the delimiter (the '\n' having already been removed from the last token when parsing with strsep)

"...gamename=1234&&square=1234..."之间插入用于处理的特定空白字段的示例如下:

An example inserting a specific empty-field for handling between "...gamename=1234&&square=1234...", could be as follows:

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

int main (void) {

    char array[] =  "address=1234&port=1234&username=1234&password=1234"
                    "&gamename=1234&&square=1234&LOGIN=LOGIN",
        *query = strdup (array),  /* duplicate array, &array is not char** */
        *tokens = query,
        *p = query;

    while ((p = strsep (&tokens, "&\n"))) {
        char *var = strtok (p, "="),
             *val = NULL;
        if (var && (val = strtok (NULL, "=")))
            printf ("%-8s    %s\n", var, val);
        else
            fputs ("<empty field>\n", stderr);
    }

    free (query);
}

(注意: strsepchar**参数作为其第一个参数,并将修改该参数以使其指向一个超出定界符的位置,因此您必须保留对原始内容开头的引用分配的字符串(上面的query).

(note: strsep takes a char** parameter as its first argument and will modify the argument to point one past the delimiter, so you must preserve a reference to the start of the original allocated string (query above)).

使用/输出示例

$ ./bin/strsep_query
address     1234
port        1234
username    1234
password    1234
gamename    1234
<empty field>
square      1234
LOGIN       LOGIN

(注意:已将"1234"转换为数值)

仔细检查一下,如果还有其他问题,请告诉我.

Look things over and let me know if you have further questions.

这篇关于C查询字符串解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆