Originally Posted by
Salem
Lemme guess, you're trying to do strtok() in your parser on a const char* pData string.
Yes, you are right! In fact the code looks like this:
Code:
#include <stdio.h>
#include <string.h>
typedef struct MPRInfo{
long long cnt;
char smb[8];
size_t r1;
size_t r2;
size_t r3;
double l1;
double l2;
double l3;
} stMPR;
int parse_mp_info(stMPR* stData,const char* pData, const char* pName){
if(pName!=NULL){
strcpy(stData->smb,pName);
}
char buffer[128] = {0};
char *p_tmp = NULL;
char *p_tag = strstr(pData,"ticks");
if(p_tag){
p_tmp = strchr(p_tag,':');
if(p_tmp){
char *p_start = ++p_tmp;
char *p_end = strchr(p_tag,',');
unsigned len = p_end-p_start;
for(unsigned i=0;i<len;i++){
buffer[i]=p_start[i];
}
stData->cnt = strtoll(buffer,NULL,0);
}
}
p_tag = strstr(pData,"rates");
if(p_tag){
char *pch = strtok(p_tag,":,");
unsigned proc_val = 0;
while(pch!=NULL){
if(strcmp(pch,"\"r1\"")==0){
proc_val=1;
}
if(proc_val==1){ //should store value in struct field
char *pdiv = strchr(pch,':');
memset(buffer,0,128*sizeof(char));
for(int i=0;i<strlen(pch);i++){
buffer[i]=pch[i];
}
stData->r1 = strtol(buffer,NULL,0);
}
pch = strtok(NULL,":,");
}
}
return 0;
}
int main()
{
const char *pData = "{\"ticks\":1657195557242,\"rates\":[{\"r1\":145,\"r2\":716,\"r3\":2145}],\"lows\":[{\"l1\":0.05,\"l2\":0.0135,\"l\":1.02}]}";
stMPR stParsed = {0};
int res = parse_mp_info(&stParsed,pData,"Test1");
if(res!=0)
printf("Error parsing data!\n");
return 0;
}
Under Windows i get to the bold line and then receicve the error. Under linux i receicve the error the moment i try to step into the function..