Chinaunix首页 | 论坛 | 博客
  • 博客访问: 752449
  • 博文数量: 119
  • 博客积分: 137
  • 博客等级: 少校
  • 技术积分: 1582
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 16:39
文章分类

全部博文(119)

文章存档

2017年(3)

2016年(7)

2014年(1)

2013年(8)

2012年(20)

2011年(27)

2010年(53)

分类: C/C++

2016-09-06 10:21:37

//每次都要重新写一遍,贴这里,以后直接用。。。。。

//1.定时器的使用 --- select 方法

点击(此处)折叠或打开

  1. #include
  2. #include

  3. void setTimer(int seconds, int mseconds)
  4. {
  5.         struct timeval temp;
  6.   
  7.         temp.tv_sec = seconds;
  8.         temp.tv_usec = mseconds;
  9.   
  10.         select(0, NULL, NULL, NULL, &temp);
  11.         // do somthing ... ...
  12.         return ;
  13. }

//2. get value from json //从json中查找name,并获取其value

点击(此处)折叠或打开

  1. int get_value_from_json(char *json,char *name, char *value)
  2. {
  3.     int ret = 0;
  4.     char tmp_payload[256] = {0};
  5.     char *begin = NULL;
  6.     char *end = NULL;
  7.     char tmp_name[32]={0};
  8.     
  9.     sprintf(tmp_name,"\"%s\":\"",name);
  10.     
  11.     if(payload)
  12.     {
  13.         strcpy(tmp_payload, json);
  14.         begin = strstr(tmp_payload, tmp_name);
  15.         if(begin)
  16.         {
  17.             begin += strlen(tmp_name);
  18.             end = strstr(begin, "\"");
  19.             if(end)
  20.             {
  21.                 *end = '\0';
  22.                 strcpy(value, begin);
  23.                 return 0;
  24.             }
  25.         }
  26.     }
  27.     return -1;
  28. }



//3. 字符串替换//将string里面的substr替换成replacement

点击(此处)折叠或打开

  1. char *str_replace(char *string, char *substr, char *replacement )
  2. {
  3.     char *tok = NULL;
  4.     char *newstr = NULL;
  5.     char *oldstr = NULL;
  6.     
  7.     /* if either substr or replacement is NULL, duplicate string a let caller handle it */
  8.     if ( substr == NULL || replacement == NULL )
  9.         return strdup (string);
  10.     
  11.     newstr = strdup (string);
  12.     while ( (tok = strstr( newstr, substr)))
  13.     {
  14.         oldstr = newstr;
  15.         newstr = malloc (strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
  16.         
  17.         /*failed to alloc mem, free old string and return NULL */
  18.         if (newstr == NULL)
  19.         {
  20.             free (oldstr);
  21.             return NULL;
  22.         }
  23.         memcpy ( newstr, oldstr, tok - oldstr );
  24.         memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
  25.         memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
  26.         memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
  27.         
  28.         free(oldstr);
  29.     }
  30.     
  31.     return newstr;
  32. }



阅读(1098) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~