C常用自定义函数集锦
来源:互联网 发布:杀马特火星文 知乎 编辑:程序博客网 时间:2024/06/06 04:01
1 AT Command Parser
#include <stdlib.h>
#include <string.h>
typedef int int32;
static char *at_tok_next_int(char *str, int32 *tok)
{
char *p, *end;
if (!str || !tok) {
return NULL;
}
p = str;
while (*p != '\0' && *p != '\r' && *p != '\n' && isspace(*p)) p++;
if (*p == '\0') {
goto err;
}
if (strchr(p, ',')) {
end = strchr(p, ',');
*end = '\0';
end++;
} else {
end = p + strlen(p);
}
*tok = strtoul(p, NULL, 0);
return end;
err:
return NULL;
}
static char *at_tok_next_str(char *str, char *tok)
{
char *p, *end;
unsigned int i;
if (!str || !tok) {
return NULL;
}
p = str;
while (*p != '\0' && *p != '\r' && *p != '\n' && isspace(*p)) p++;
if (*p == '\0') {
goto err;
}
if (strchr(p, ',')) {
end = strchr(p, ',');
*end = '\0';
end++;
} else {
end = p + strlen(p);
}
strcpy(tok, p);
for (i = strlen(tok); i >= 0; i--) {
if (isalpha(tok[i]) || isdigit(tok[i])) {
break;
} else if (isspace(tok[i]) || tok[i] == '\r' || tok[i] == '\n') {
tok[i] = '\0';
}
}
return end;
err:
return NULL;
}
int main(int argc, char **argv)
{
char at[] = {"AT+TEST=5,1,HELLO\r\n"};
int prx_len = strlen("AT+TEST=");
char *p = at + prx_len;
int v1;
int v2;
char v3[16];
p = at_tok_next_int(p, &v1);
if (p) {
p = at_tok_next_int(p, &v2);
}
if (p) {
p = at_tok_next_str(p, v3);
}
fprintf(stdout, "%d %d %s\n", v1, v2, v3);
return 0;
}
2 JSON
#define API(func) func
#define ACTION_SZ 64
#define AUTH_SZ 32
#define UUID_SZ 32
#define PARAMS_SZ 256
#define MSG_SZ 64
#define STATUS_SZ 32
#define RESULT_SZ 256
typedef unsigned int u32;
typedef struct req_arg {
char action[ACTION_SZ];
char auth[AUTH_SZ];
char uuid[UUID_SZ];
char params[PARAMS_SZ];
} req_arg_t;
typedef struct rsp_arg {
char msg[MSG_SZ];
char uuid[UUID_SZ];
char status[STATUS_SZ];
char result[RESULT_SZ];
} rsp_arg_t;
void API(u_json_clr_qm)(char *json)
{
int i;
for (i = 0; i < strlen(json); i++) {
if (json[i] == '\"') {
json[i] = ' ';
}
}
}
int API(u_json_get_value)(const char *json, int json_len, const char *key,
char *value, int value_len)
{
char *p, *p2, *end;
unsigned int nleft = 0, nright = 0;
unsigned int i, len;
if (!json || !key || !value) {
goto err;
}
p = strstr(json, key);
if (!p) {
printf("Oops\n");
goto err;
}
p += strlen(key);
while (*p != '\0' && *p != ':') p++;
if (*p != ':') {
printf("err1, invalid json string: %s\n", json);
goto err;
}
p++;
while (*p != '\0' && isspace(*p)) p++;
//printf("DEBUG:%s-\n", p);
if (*p == '{') {
p++;
end = p;
while (*end != '\0' && *end != '}') end++;
if (*end != '}') {
printf("err2, invalid json string: %s\n", json);
goto err;
}
p2 = p;
// In 64bit machine, this will show a warning message,
// because of sizeof(char *) = 8, but sizeof(u32) = 4
//while ((u32)p2 != (u32)end) {
while ((u32)p2 < (u32)end) {
if (*p2 == '{') {
nleft++;
}
p2++;
}
if (nleft > 0) {
end++;
while (*end != '\0') {
if (*end == '}') {
nright++;
}
if (nleft == nright) {
break;
}
end++;
}
}
} else {
end = p;
while (*end != '\0' && *end != ',' && *end != '}') end++;
// TODO: Need check error?
}
len = (u32)(end - p);
value_len = len < (value_len - 1) ? len : (value_len - 1);
memcpy(value, p, value_len);
value[value_len] = '\0';
for (i = value_len; i >= 0; i--) {
if (isalpha(value[i]) || isdigit(value[i])) {
break;
} else if (isspace(value[i])) {
value[i] = '\0';
}
}
return 0;
err:
return -1;
}
3 Customized Print Function
#include <stdarg.h>
static int format_xxx_data(unsigned char *buf, unsigned int buf_len, const char *fmt,...)
{
va_list ap;
int n;
if (NULL == buf) {
return -1;
}
va_start(ap, fmt);
n = vsnprintf(buf, buf_len, fmt, ap);
va_end(ap);
return n;
}
- C常用自定义函数集锦
- C#常用函数集锦
- oracle常用函数集锦
- C#常用函数集锦
- libxml2常用函数集锦
- SQL常用函数集锦
- ABAP常用函数集锦
- ABAP常用函数集锦
- SQL常用函数集锦
- SQL常用函数集锦
- SQL常用函数集锦
- SQL常用函数集锦
- SQL常用函数集锦
- SQL常用函数集锦
- C# 常用函数集锦
- oracle常用函数集锦
- IDL常用函数集锦
- SQL常用函数集锦
- -----最短路 最大值 hdu 5137-How Many Maos Does the Guanxi Worth
- C
- 利用tampermonkey向页面注入自定义Javascript
- Eclipse导出war包时去掉SVN文件
- 23种设计模式(20)-状态模式
- C常用自定义函数集锦
- 详解python中的__new__方法
- SpringData中的PagingAndSortingRepository接口
- yii2-GridView在开发中常用的功能及技巧
- Linux学习笔记(7)
- C/C++中输入多组数据方法
- 使用数据库UNIQUE constraint failed
- Spring MVC与Struts2的区别
- 结合FlexboxLayout实现标签选择功能