码迷,mamicode.com
首页 > Web开发 > 详细

ic_json_get_value_str

时间:2015-06-23 06:15:15      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ic_jsonHelper.h"
#include "ic_datetime.h"
#include "ic_comm.h"


int ic_json_sprint_kv_d(char *str, const char *k, int v)
{
    const char sep = g_ic_box.json_sep;
    return sprintf(str, "%c%s%c:%d,", sep, k, sep, v);
}
int ic_json_sprint_kv_s(char *str, const char *k, const char *v)
{
    const char sep = g_ic_box.json_sep;
    return sprintf(str, "%c%s%c:%c%s%c,", sep, k, sep, sep, v, sep);
}

int ic_json_sprint_kv_time(char *str, const char *k, time_t *v)
{
    if(!v) return 0;
    
    static char tmbuf[100];
    ic_str_time(tmbuf, sizeof(tmbuf), v);
    return ic_json_sprint_kv_s(str, k, tmbuf);
}

int ic_json_sprint_kv_mac(char *str, const char *k, ic_mac *v)
{
    if(!v) return 0;
    
    static char tmbuf[MAC_ADDR_LEN * 3+1];
    return ic_json_sprint_kv_s(str, k, strMAC(tmbuf, v));

}


icrt_t ic_json_get_value_d(const char *jstr, const char *k, int *v)
{
    if(!jstr || !k || !v) return ICRT_FAILL_NULL;
    
    const char sep = g_ic_box.json_sep;
    static char kbuf[IC_KEY_LEN_MAX];
    
    snprintf(kbuf, sizeof(kbuf), "%c%s%c", sep, k, sep);
    char * vh = strstr(jstr, kbuf);
    if(!vh) return ICRT_FAILL_NON;

    vh = vh + strlen(kbuf);
    int i = 0, c = 0, si = 0, se = 0, pre = 0;
    int vi = 0;
    
    int tc = ‘:‘; // target char
    for(; (c=vh[i]) != tc && c != ‘\0‘; i++);
    if(c!=tc) return ICRT_FAILL;
    vh = vh + i+1;
    
    *v = atol(vh);
    
    /*
    // non zero
    for(i=0; (c=vh[i]) !=‘\0‘ && c == ‘ ‘ || c == ‘\t‘; i++);
    if(c==‘\0‘) return 0;
    vh = vh + i+1;
    */

    return ICRT_OK;
}

size_t ic_json_get_value_str(const char *jstr, const char *k, char *v, size_t vsize)
{
    int rt = 0;

    if(!jstr || !k || !v) return rt;
    
    const char sep = g_ic_box.json_sep;
    static char kbuf[IC_KEY_LEN_MAX];
    
    snprintf(kbuf, sizeof(kbuf), "%c%s%c", sep, k, sep);
    char * vh = strstr(jstr, kbuf);
    if(!vh) return rt;

    vh = vh + strlen(kbuf);
    int i = 0, c = 0, si = 0, se = 0, pre = 0;
    int vi = 0;
    
    int tc = ‘:‘; // target char
    for(; (c=vh[i]) != tc && c != ‘\0‘; i++);
    if(c!=tc) return 0;
    vh = vh + i+1;
    

    tc = ‘\"‘; // target char
    for(i=0; (c=vh[i]) != tc && c != ‘\0‘; i++);
    if(c!=tc) return 0;
    vh = vh + i+1;

    // copy valure to vbuf
    tc = ‘\"‘; // target char
    c = pre = vh[i];
    for(i=0; ((c=vh[i]) != ‘\0‘) && (i<vsize); pre=c, i++){
        if(pre!=‘\\‘ && c==tc) break;
        v[i] = c;
    }
    if(c==tc){
        v[i] = ‘\0‘;
        return i;
    }

    

    return 0;
}

ic_json_get_value_str

标签:

原文地址:http://www.cnblogs.com/x113/p/4594458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!