python调用C库时参数太多,约定传json格式字符串,C解析
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct __Json_ { int byExposureModeSet; int byFocusMode; int wMinFocusDistance; }Json; int HandleStr(char *pStr, Json *pStuJson); int GetValue(char **pStr, char *pValue); void EarseSpace(char *pStr); int DelLeftSpace(char *szbuf); int DelRightSpace(char *szbuf); int main() { char str[1024] = ""byExposureModeSet" : "2", "byFocusMode" : "2", "wMinFocusDistance" : "10""; printf("%s ", str); Json stuJson = {0}; memset(&stuJson, 0, sizeof(stuJson)); HandleStr(str, &stuJson); printf("stuJson.byExposureModeSet = %d, stuJson.byFocusMode = %d, stuJson.wMinFocusDistance = %d ", stuJson.byExposureModeSet, stuJson.byFocusMode, stuJson.wMinFocusDistance); return 0; } int HandleStr(char *pStr, Json *pStuJson) { int iRet = -1; char *tmp = pStr; char key[64] = {0}; char value[64] = {0}; while(tmp != NULL) { memset(key, 0, sizeof(key)); memset(value, 0, sizeof(value)); iRet = GetValue(&tmp, key); tmp = strstr(tmp, ":") + 1; iRet = GetValue(&tmp, value); printf("{%s} : {%s} ", key, value); if (0 == strcmp(key, "byExposureModeSet")) { pStuJson->byExposureModeSet = atoi(value); } else if (0 == strcmp(key, "byFocusMode")) { pStuJson->byFocusMode = atoi(value); } else if (0 == strcmp(key, "wMinFocusDistance")) { pStuJson->wMinFocusDistance = atoi(value); } tmp = strstr(tmp, ","); } return iRet; } int GetValue(char **pStr, char *pValue) { int iRet = 0; char *pBegin = *pStr; char *pEnd = NULL; pBegin = strstr(pBegin, """); pEnd = strstr(pBegin+1, """); int len_str = pEnd-pBegin; memcpy(pValue, pBegin+1, len_str-1); //printf("pValue = {%s} ", pValue); iRet = len_str; *pStr = pEnd; return iRet; } int DelLeftSpace(char *szbuf) { int iRet = -1; int iLen = 0; int i = 0; int j = 0; int k = 0; if(NULL != szbuf) { iRet = 0; iLen = strlen(szbuf); for(i = 0; i < iLen; i++) { if( (' ' != szbuf[i]) && (' ' != szbuf[i]) && (' ' != szbuf[i]) && (' ' != szbuf[i]) ) { if(0 != i) { j = i; k = 0; while(j < iLen) { szbuf[k++] = szbuf[j++]; } szbuf[k] = '