makemymenu()


typedef enum{

 C_SET=0,

 C_MEAL=1

} mycategory_t;



typedef struct{

mycategory_t cat;

char name[20];

char size[10];

int price;

} mymenu_t;



로 구성된 구조체에 다음 메뉴를 저장하기를 원한다


{

"Yori":

[

{

  "name":"Haemultang",

          "size":"Jong",

          "price":50000

        },

{

          "name":"Haemultang",

          "size":"Dae",

          "price":60000

        },

{

          "name":"Haemuljimg",

          "size":"Jong",

          "price":50000

        },

{

          "name":"Haemuljim",

          "size":"Dae",

          "price":60000

        }

],

"Sicksa":

[

 {

          "name":"BJnoodle",

          "size":"1 piece",

          "price":7000

        },

{

          "name":"FIredSquid",

          "size":"1 piece",

          "price":8000

        },

{

          "name":"Rice with Suisi",

          "size":"1 piece",

          "price":10000

        }

]

}




---------------------------------------------------------------------------------------------------

int makemymenu(const char *json, jsmntok_t *t, int r, mymenu_t* m[]){
int size=0;
        int i=0,j=0,menucount=0;
int type=0;
char through[20];

        for(i=1;i<r;i++){
        if(t[i+1].type==2){
if( !strncmp("Yori", json + t[i].start,t[i].end-t[i].start)){type=0;}
else if ( !strncmp("Sicksa", json + t[i].start,t[i].end-t[i].start)){type=1;}
else type =0;
        size=t[i+1].size;
       
i+=4;

                for(j=menucount;j<menucount+size;i+=3,j++){
m[j]->cat=type;
                strncpy(m[j]->name, json + t[i].start,t[i].end-t[i].start);i+=2;
                strncpy(m[j]->size,json + t[i].start,t[i].end-t[i].start);i+=2;
strncpy(through,json+t[i].start,t[i].end-t[i].start);
                m[j]->price=atoi(through);
                }i-=3;
        menucount+=size;
}

        }



        return menucount;

        }

다음 코드는 object의 type을 "Yori"와 "Sicksa"로 구분하여 이름, 크기 그리고 가격으로 구조체에 저장하는 함수이다.
즉, 음식점의 요리류와 식사류의 음식을 메뉴로 parsing하는 함수이다



+ Recent posts