提交 7cd478bc 编写于 作者: 来碗豆腐脑's avatar 来碗豆腐脑

初始化

上级 aa91409b
{
"configurations": {
"Launch": {
"adapter": "vscode-cpptools",
"configuration": {
"request": "launch",
"program": "${fileDirname}/demo.exe",
"stopAtEntry": true
}
}
}
}
文件已添加
/**
* @file mRjson.c
* @Synopsis 简易json解析库
* @author Wang Jiancong
* @version v0.1
* @date 2022-09-12
*/
#include "mRjson.h"
#include "stdarg.h"
#include <stdlib.h>
#include <string.h>
/* --------------------------------------------------------------------------*/
/**
* @Synopsis 拷贝字符串直到遇到指定字符
*
* @Param str_dst 目标字符串
* @Param str_src 源字符串, 被拷贝
* @Param ch 指定字符
*/
/* --------------------------------------------------------------------------*/
static void cpy_str(char* str_dst, char* str_src, char ch)
{
while (*str_src != ch) {
*str_dst ++ = *str_src++;
}
}
/* --------------------------------------------------------------------------*/
/**
* @Synopsis 按指定格式输入json字符串,键,解析目标,保存结果
* ("%j,%i,%t,%r")
*
* @Param str
* @Param ...
*/
/* --------------------------------------------------------------------------*/
void mRjson_Decode(const char *str, ...)
{
va_list ap;
char* jstr;
char* istr;
char* tstr;
char* strp;
void* res = NULL;
va_start(ap, str);
for(;*str;str++)
{
if(*str == '%')
{
switch (*++str)
{
case 'j':
case 'J':
jstr = va_arg(ap, char*);
break;
case 'i':
case 'I':
istr = va_arg(ap, char*);
break;
case 't':
case 'T':
tstr = va_arg(ap, char*);
break;
case 'r':
case 'R':
strp = strstr(jstr,istr);
strp += strlen(istr);
while (*strp == ' ' ||
*strp == ':' ||
*strp == '"')
{
strp ++;
}
switch (*tstr) {
case 'd':
case 'D':
res = va_arg(ap, int*);
*(int*)res = atoi(strp);
break;
case 'f':
case 'F':
res = va_arg(ap, double*);
*(double*)res = atof(strp);
break;
case 's':
case 'S':
res = va_arg(ap, char*);
cpy_str(res, strp, '"');
/*strtok(strp,"\"");*/
/*strcpy(res,strp);*/
break;
}
break;
}
}
}
va_end(ap);
}
/**
* @file mRjson.c
* @Synopsis 简易json解析库
* @author Wang Jiancong
* @version v0.1
* @date 2022-09-12
*/
#ifndef _mRjson_h
#define _mRjson_h
void mRjson_Decode(const char *str, ...);
#endif
文件已添加
tar=demo
obj=test.o mRjson.o
CC=gcc
CFLAGS=-c -Wall -g
$(tar):$(obj)
$(CC) $^ -o $(tar)
%.o:%.c
$(CC) $^ $(CFLAGS) -o $@
clean:
$(RM) *.o $(tar) -r
/**
* @file mRjson.c
* @Synopsis 简易json解析库
* @author Wang Jiancong
* @version v0.1
* @date 2022-09-12
*/
#include <stdio.h>
#include "mRjson.h"
char json[] = "{\"size\":1,\"str\":\"hello\",\"double\":3.14}";
char ss[100] = "nihao";
int main(void)
{
int tmp;
double ftmp;
printf("json: %s\r\n\r\n",json);
mRjson_Decode("%J%I%T%R",json,"size","D",&tmp);
printf("res 1: %d\r\n", tmp);
mRjson_Decode("%J%I%T%R",json,"str","S",&ss[0]);
printf("res 2: %s\r\n", ss);
mRjson_Decode("%J%I%T%R",json,"double","F",&ftmp);
printf("res 3: %f\r\n", ftmp);
printf("json: %s\r\n\r\n",json);
return 0;
}
文件已添加
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册