我需要的strtok和strtok_single的混合 [英] I need a mix of strtok and strtok_single
问题描述
我有以下字符串,我试图分析变量。
I have the following string that I am trying to parse for variables.
char data[]="to=myself@gmail.com&cc=youself@gmail.com&title=&content=how are you?&signature=best regards."
我开始用的strtok及以下code
I started with strtok and the following code
char *to=parsePostData("to",data);
char* parsePostData(char s[],char t[])
{
char *postVal;
char *pch;
char tCpy[512];//Make a copy. Otherwise, strtok works on the char pointer, and original char array gets modified/ corrupted.
strcpy(tCpy,t);
pch = strtok (tCpy,"=&");
while (pch != NULL)
{
if(strcmp(pch,s)==0) {
pch= strtok (NULL, "&");
return pch;
}else{
pch = strtok (NULL, "=&");
}
}
}
这工作得很好,当谈到连续的分隔符,如称号后的人,除了。所以,我发现这个习俗strtok_single实现。
<一href=\"http://stackoverflow.com/questions/8705844/need-to-know-when-no-data-appears-between-two-token-separators-using-strtok\">Need在使用strtok的双令牌分离器之间似乎没有数据就知道()
This works fine, except when it comes to consecutive delimiters such as the one after "title". So I found this custom strtok_single implementation. Need to know when no data appears between two token separators using strtok()
char * strtok_single (char * str, char const * delims)
{
static char * src = NULL;
char * p, * ret = 0;
if (str != NULL)
src = str;
if (src == NULL)
return NULL;
if ((p = strpbrk (src, delims)) != NULL) {
*p = 0;
ret = src;
src = ++p;
}
return ret;
}
但有了这个,问题是我不能让签名,因为没有和放大器;之后分隔符。
But with this, the problem is I cannot get "signature", as there is no & delimiter after that.
怎样才能得到这个二者的混合,所以我不错过了最后一个变量,我可以应付连续的分隔符?
How can I get a mix of this two, so I dont miss out the last variable, and I can handle consecutive delimiters?
推荐答案
您还没有确切地告诉我们,你说的这工作正常,但它似乎足以说,要分析一个<$ C $是什么意思C>应用程序/ x-WWW的形式urlen codeD 字符串。你为什么不摆在首位这么说?
You haven't exactly told us what you mean by "this works fine", though it seems sufficient to say that you want to parse an application/x-www-form-urlencoded
string. Why didn't you say so in the first place?
考虑到第一个字段,键
,可由第一任=
或终止'和;
。这将是适当的搜索,在任一这些字符的结束令牌,提取键
。
Consider that the first field, key
, may be terminated by the first of either '='
or '&'
. It would be appropriate to search for a token that ends in either of those characters, to extract key
.
第二个字段,值
,但不是由=
字符终止,所以它的不适当的要寻找该字符提取值
。你想要搜索'和;
仅
The second field, value
, however, isn't terminated by an '='
character, so it's inappropriate to be searching for that character to extract value
. You'd want to search for '&'
only.
当然。您的可能的使用 strtok的
来解析这一点,但是我敢肯定有很多更合适的工具。 strcspn
,例如,不会对数据
,这意味着你不需要做任何改变数据副本
,你是...
Sure. You could use strtok
to parse this, however I'm sure there are many more suitable tools. strcspn
, for example, won't make any changes to data
, which means you won't need to make a copy of data
as you are...
#include <stdio.h>
#include <string.h>
int main(void) {
char data[]="to=myself@gmail.com&cc=youself@gmail.com&title=&content=how are you?&signature=best regards.";
char *key = data;
do {
int key_length = strcspn(key, "&=");
char *value = key + key_length + (key[key_length] == '=');
int value_length = strcspn(value, "&");
printf("Key: %.*s\n"
"Value: %.*s\n\n",
key_length, key,
value_length, value);
key = value + value_length + (value[value_length] == '&');
} while (*key);
return 0;
}
这篇关于我需要的strtok和strtok_single的混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!