C常用自定义函数集锦

来源:互联网 发布:杀马特火星文 知乎 编辑:程序博客网 时间:2024/06/06 04:01

1 AT Command Parser

#include <stdio.h>
#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;
}

原创粉丝点击