hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *  Copyright (c) 2020, Rockchip Electronics Co., Ltd
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
 
#ifndef J2S_COMMON_H
#define J2S_COMMON_H
 
#ifdef __cplusplus
extern "C" {
#endif
 
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#define J2S_VERSION "1.0.0~20201229"
 
//#define DEBUG
 
#ifndef unlikely
#define unlikely(x) __builtin_expect((x), 0)
#endif
 
#ifndef MSG
#define MSG(level, fmt, args...)                                      \
    fprintf(stderr, #level ": %s(%d) [%s]: " fmt, __FILE__, __LINE__, \
        __FUNCTION__, ##args)
#endif
 
#ifndef DBG
#if defined(NDEBUG)
#define DBG(fmt, args...)
#elif defined(DEBUG)
#define DBG(fmt, args...) MSG(D, fmt, ##args)
#else
#define DBG(fmt, args...)    \
    if (getenv("J2S_DEBUG")) \
    MSG(D, fmt, ##args)
#endif
#endif
 
#ifndef INFO
#define INFO(fmt, args...) MSG(I, fmt, ##args)
#endif
 
#ifndef WARN
#define WARN(fmt, args...) MSG(W, fmt, ##args)
#endif
 
#ifndef ERR
#define ERR(fmt, args...) MSG(E, fmt, ##args)
#endif
 
#ifndef DASSERT
#define DASSERT(b, action)                             \
    do {                                               \
        if (unlikely(!(b))) {                          \
            ERR("debug assertion failure (%s)\n", #b); \
            action;                                    \
        }                                              \
    } while (0)
#endif
 
#ifndef DASSERT_MSG
#define DASSERT_MSG(b, action, fmt, args...) \
    do {                                     \
        if (unlikely(!(b))) {                \
            ERR(fmt, ##args);                \
            action;                          \
        }                                    \
    } while (0)
#endif
 
#define MAX_LINE 8192
#define MAX_NAME 64
 
#define J2S_DESC_MAGIC "@desc:"
 
typedef enum {
    J2S_TYPE_INT_8 = 1,
    J2S_TYPE_UINT_8,
    J2S_TYPE_INT_16,
    J2S_TYPE_UINT_16,
    J2S_TYPE_INT_32,
    J2S_TYPE_UINT_32,
    J2S_TYPE_INT_64,
    J2S_TYPE_UINT_64,
    J2S_TYPE_FLOAT,
    J2S_TYPE_DOUBLE,
    J2S_TYPE_STRING, /* For char *|char [] */
    J2S_TYPE_STRUCT,
} j2s_type;
 
typedef enum {
    J2S_FLAG_ARRAY = 1 << 0,
    J2S_FLAG_POINTER = 1 << 1,
    J2S_FLAG_DEP_ARRAY = 1 << 2, /* int array[1][2] */
    J2S_FLAG_DEP_POINTER = 1 << 3, /* char ** */
    J2S_FLAG_ARRAY_POINTER = 1 << 4, /* typedef int (*array_ptr_int)[4] */
} j2s_flag;
 
#define J2S_IS_SIMPLE_STRING(obj) \
    ((obj)->type == J2S_TYPE_STRING && ((obj)->flags == J2S_FLAG_ARRAY || (obj)->flags == J2S_FLAG_POINTER))
 
#define J2S_IS_ARRAY(obj) \
    ((obj)->flags & J2S_FLAG_ARRAY && !((obj)->flags & J2S_FLAG_ARRAY_POINTER))
 
#define J2S_IS_POINTER(obj) \
    ((obj)->flags & J2S_FLAG_POINTER && !J2S_IS_ARRAY(obj))
 
#ifdef __cplusplus
}
#endif
 
#endif // J2S_COMMON_H