//每次都要重新写一遍,贴这里,以后直接用。。。。。
//1.定时器的使用 --- select 方法
-
#include
-
#include
-
-
void setTimer(int seconds, int mseconds)
-
{
-
struct timeval temp;
-
-
temp.tv_sec = seconds;
-
temp.tv_usec = mseconds;
-
-
select(0, NULL, NULL, NULL, &temp);
-
// do somthing ... ...
-
return ;
-
}
//2. get value from json //从json中查找name,并获取其value
-
int get_value_from_json(char *json,char *name, char *value)
-
{
-
int ret = 0;
-
char tmp_payload[256] = {0};
-
char *begin = NULL;
-
char *end = NULL;
-
char tmp_name[32]={0};
-
-
sprintf(tmp_name,"\"%s\":\"",name);
-
-
if(payload)
-
{
-
strcpy(tmp_payload, json);
-
begin = strstr(tmp_payload, tmp_name);
-
if(begin)
-
{
-
begin += strlen(tmp_name);
-
end = strstr(begin, "\"");
-
if(end)
-
{
-
*end = '\0';
-
strcpy(value, begin);
-
return 0;
-
}
-
}
-
}
-
return -1;
-
}
//3. 字符串替换//将string里面的
substr替换成replacement
-
char *str_replace(char *string, char *substr, char *replacement )
-
{
-
char *tok = NULL;
-
char *newstr = NULL;
-
char *oldstr = NULL;
-
-
/* if either substr or replacement is NULL, duplicate string a let caller handle it */
-
if ( substr == NULL || replacement == NULL )
-
return strdup (string);
-
-
newstr = strdup (string);
-
while ( (tok = strstr( newstr, substr)))
-
{
-
oldstr = newstr;
-
newstr = malloc (strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
-
-
/*failed to alloc mem, free old string and return NULL */
-
if (newstr == NULL)
-
{
-
free (oldstr);
-
return NULL;
-
}
-
memcpy ( newstr, oldstr, tok - oldstr );
-
memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
-
memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
-
memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
-
-
free(oldstr);
-
}
-
-
return newstr;
-
}
阅读(1098) | 评论(0) | 转发(0) |