cjson 读写文件

/*
 *  @file: alert_config.c
 *  @brief:
 *  @version: 0.0
 *  @author:
 *  @date: 2019/11/20
 */
/******************************************************************************
@note
    Copyright 2019, Megvii Corporation, Limited
                            ALL RIGHTS RESERVED
******************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include  
#include  
#include
#include
#include
#include
#include
#include


/************************** C FUNC **************************/
#ifdef __cplusplus
extern "C" {
#endif  //@__cplusplus

#ifndef PI
#define PI acos(-1)
#endif

#define MAX_NAMELENGTH_ALERT_RULE 16
#define MAX_ITEMS_PER_ALERTTYPE 5
#define MAX_SIDES_OF_ALERTAREA  10 
#define MAX_HUMANBODY_IN_ONE_IMAGE 16
#define MAX_HISTORY_ONE_TRAKER_ALERT  10
#define MAX_MISS_ONE_TRAKER_ALERT 15
#define MIN_MAJOR_TRAKER_ALERT 3

#define MAX_NORMAL_VECTOR_LINE_LEN_ALERT 96
#define MAX_NORMAL_VECTOR_LINE_DIR_LEN_ALERT 24 

#ifndef uint8_t
#define uint8_t unsigned char
#endif

enum{
    ALERT_NORMAL = 0,
    ALERT_TRIPWIRE_INTRUSION=1<<0,
    ALERT_AREA_INVASION = 1<<1,
    ALERT_AREA_LEAVE = 1<<2,
    ALERT_AREA_ENTER = 1<<3,
    ALERT_CLIMBWALL = 1<<4,
};
enum{
    ALERT_DIRECT_NONE = 0,
    ALERT_DIRECT_ALONG_NORMAL = 1<<0 ,
    ALERT_DIRECT_REVERSE_NORMAL = 1<<1,
    ALERT_DIRECT_UNKOWN = 1<<2,
};
enum {
    ALERT_FOOTRECT_CROSSLINE = 1<<0, // for tripwire 
    ALERT_FOOTRECT_PARTIN = 1<<1,
    ALERT_FOOTRECT_ALLPOINT_IN = 1<<2,
    ALERT_FOOTRECT_CLIMBWALL_FIRSTLINE_CROSS = 1<<3, // for climbWall
    ALERT_FOOTRECT_CLIMBWALL_SECONDLINE_CROSS = 1<<4,//for climbWall
    ALERT_FOOTRECT_ENTER = 1<<5, //only for area enter record state
};

enum ALERT_TRIPWIRE_DIRECT_STATE{
    ALERT_TRIPWIRE_DIRECT_ENTER_STATE = 1 <<0,//for first time the vector is the same director with normal vector
    ALERT_TRIPWIRE_DIRECT_STOP_STATE = 1 <<1,//for second time the vector is the same director with normal vector, will trigger alert msg
};

typedef struct _Point
{
    int x;
    int y;
}Point;

//structure for tripwire configuration
typedef struct _alert_tripWire_conf
{
    char name[MAX_NAMELENGTH_ALERT_RULE];
    int timeStamp;//unused, reserved for furture
    Point  warning[2];
    int direct;//0:bi-direction; 90:anticlockwise PI/2 corresponding to tripwire; -90:clockwise PI/2 corresponding to tripwire;
    int xmin;//the min x of two points of tripwire terminal, for calculating the intersection of tripwire and foot rect.
    int xmax;//the purpose is same as xmin
    int ymin;//the purpose is same as xmin
    int ymax;//the purpose is same as xmin
}alert_tripWire_conf;

//structure for climbWall configuration
typedef struct _alert_climbWall_conf
{
    char name[MAX_NAMELENGTH_ALERT_RULE];
    alert_tripWire_conf tripWire[2];//two lines for climbWall; see guide for detail
}alert_climbWall_conf;

//structure for area configuration, area invasion , area leave , and so on.
typedef struct _alert_area_conf
{
    char name[MAX_NAMELENGTH_ALERT_RULE];
    int timeStamp;//unused, reserved for furture
    int sidesNum;//number of sides in polygon
    Point sides[MAX_SIDES_OF_ALERTAREA];//the corners points of polygon
}alert_area_conf;

//total alert configuration 
typedef struct _alert_conf
{
    float ValidBoxRatio;//the ratio of body rect to get footrect
    int StopDuration;//the during frame for judging if or not sending alert
    float areaOverlapRatio;//the ratio threshold of overlap area-size to the footrect
    int climbWallNum;//the number of climbWall configuration items
    int TripWireNum;//the number of tripwire configuration items
    int areaInvasionNum;//the number of area-invasion configuration items
    int areaLeaveNum;//the number of area-leave configuration items
    int areaEnterNum;//unused, for furture extension
    alert_climbWall_conf climbWall[MAX_ITEMS_PER_ALERTTYPE];//items of climbWall configuration
    alert_tripWire_conf TripWire[MAX_ITEMS_PER_ALERTTYPE];//items of tripwire configuration
    alert_area_conf areaInvasion[MAX_ITEMS_PER_ALERTTYPE];//items of areainvasion configuration
    alert_area_conf areaLeave[MAX_ITEMS_PER_ALERTTYPE];//items of arealeave configuration
    alert_area_conf areaEnter[MAX_ITEMS_PER_ALERTTYPE];//unused, for furture extension
}alert_conf;

static alert_conf  alertConf;//global alert configuration variable

//get config from alert_conf.json
int alert_conf_parse_from_buf(char *confPath)
{
    alert_conf *pAlertConf = &alertConf;

    std::ifstream infile;
    int length;
    infile.open(confPath);     // open input file 
    infile.seekg(0, std::ios::end);    // go to the end  
    length = infile.tellg();           // report location (this is the length)  
    infile.seekg(0, std::ios::beg);    // go back to the beginning  
    char *pbuf = new char[length];    // allocate memory for a buffer of appropriate dimension  
    infile.read((char*)pbuf, length);       // read the whole file into the buffer  
    infile.close();                    // close file handle

    cJSON *pJsonTop;
    
    pJsonTop = cJSON_Parse(pbuf);
    if(NULL == pJsonTop)
    {
        printf("%s(%d):%s\n", __FILE__, __LINE__, confPath );
        printf("%s\n", pbuf);
        return 1;
    }
    delete []pbuf;

    cJSON *pJsonSub = pJsonTop->child;
    while(pJsonSub)
    {
        printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonSub->string);
        if(strcmp(pJsonSub->string, "climbWall") == 0)
        {              
            printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonSub->string);
            cJSON *pJsonItem = pJsonSub->child;
            while(pJsonItem)
            {
                printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonItem->string);
                cJSON *pJsonOneLine = pJsonItem->child;
                while(pJsonOneLine)
                {
                    printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonOneLine->string);
                    cJSON *pJsonOnePoint = pJsonOneLine->child;
                    while(pJsonOnePoint)
                    {
                        printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonOnePoint->string);
                        cJSON *pJsonOneAxis = pJsonOnePoint->child;
                        while(pJsonOneAxis)
                        {
                            printf("%s(%d): %s, value %d\n", __FILE__, __LINE__, 
                                    pJsonOneAxis->string, pJsonOneAxis->valueint);

                            pJsonOneAxis = pJsonOneAxis->next;
                        }

                        pJsonOnePoint= pJsonOnePoint->next;
                    }

                    pJsonOneLine = pJsonOneLine->next;
                }
                 
                pJsonItem = pJsonItem->next;
            }
        } 

        if(strcmp(pJsonSub->string, "TripWire") == 0)
        {              
            printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonSub->string);
            cJSON *pJsonItem = pJsonSub->child;
            while(pJsonItem)
            {
                printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonItem->string);
                cJSON *pJsonOneAttr= pJsonItem->child;
                while(pJsonOneAttr)
                {
                    printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonOneAttr->string);
                    if(strcmp(pJsonOneAttr->string, "warning") == 0)
                    {
                        cJSON *pJsonOnePoint = pJsonOneAttr->child;
                        while(pJsonOnePoint)
                        {
                            printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonOnePoint->string);
                            cJSON *pJsonOneAxis = pJsonOnePoint->child;
                            while(pJsonOneAxis)
                            {
                                printf("%s(%d): %s, value %d\n", __FILE__, __LINE__, 
                                        pJsonOneAxis->string, pJsonOneAxis->valueint);

                                pJsonOneAxis = pJsonOneAxis->next;
                            }

                            pJsonOnePoint= pJsonOnePoint->next;
                        }
                    }

                    pJsonOneAttr = pJsonOneAttr->next;
                }
                 
                pJsonItem = pJsonItem->next;
            }
        } 
        
        if(strcmp(pJsonSub->string, "areaInvasion") == 0)
        {              
            printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonSub->string);
            cJSON *pJsonItem = pJsonSub->child;
            while(pJsonItem)
            {
                printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonItem->string);
                cJSON *pJsonOneAttr= pJsonItem->child;
                while(pJsonOneAttr)
                {
                    printf("%s(%d): %s\n", __FILE__, __LINE__, pJsonOneAttr->string);
                    if(strcmp(pJsonOneAttr->string, "sidesNum") == 0)
                    {
                        printf("%s(%d): %s, sidesNum %d\n", __FILE__, __LINE__, pJsonOneAttr->string, pJsonOneAttr->valueint);
                    }
                    if(strstr(pJsonOneAttr->string, "p") != 0)
                    {
                        cJSON *pJsonOneElem= pJsonOneAttr->child;
                        while(pJsonOneElem)
                        {
                            printf("%s(%d): %s, value %d\n", __FILE__, __LINE__, pJsonOneElem->string, pJsonOneElem->valueint);

                            pJsonOneElem = pJsonOneElem->next;
                        }
                    }

                    pJsonOneAttr = pJsonOneAttr->next;
                }
                 
                pJsonItem = pJsonItem->next;
            }
        } 

        if(strcmp(pJsonSub->string, "ValidBoxRatio") == 0)
        {              
            printf("%s(%d): %s, %lf\n", __FILE__, __LINE__, pJsonSub->string, pJsonSub->valuedouble);
        }
        if(strcmp(pJsonSub->string, "StopDuration") == 0)
        {              
            printf("%s(%d): %s, %d\n", __FILE__, __LINE__, pJsonSub->string, pJsonSub->valueint);
        }
        if(strcmp(pJsonSub->string, "areaOverlapRatio") == 0)
        {              
            printf("%s(%d): %s, %lf\n", __FILE__, __LINE__, pJsonSub->string, pJsonSub->valuedouble);
        }

        pJsonSub = pJsonSub->next;
    }
    printf("\n*********************************************\n");
    printf("%s", cJSON_Print(pJsonTop));
    printf("*********************************************\n");

//    delete []pbuf;
    return 0;
}

int main(int argc, char** argv)
{
    alert_conf_parse_from_buf(argv[1]);
    return 0;
}

#ifdef __cplusplus
}
#endif //@__cplusplus

你可能感兴趣的:(json等通用文件格式)