Comparison of specific raw data using a preprocessor

I have certain source data in my header file (which is generated automatically), for example:

#defined RAW_DATA 0x11, 0x20, 0x55, 0x00, x044

The goal is to check a specific RAW_DATA parameter at compile time, and if it is erroneous, output #error.

For example, during compilation, the preprocessor should check if the 2nd parameter RAW_DATA (in this case 0x20) is really 0x20 if it did not select #error.

The main problem is how to access a specific parameter in certain RAW_DATA, is this possible?

PS I am using the Keil compiler in C.

+4
source share
2 answers
#define RAW_DATA 0x11, 0x20, 0x55, 0x00, x044
#define X_GET_SECOND_PAR(par) GET_SECOND_PAR(par)
#define GET_SECOND_PAR(p1,p2,p3,p4,p5) p2

#if X_GET_SECOND_PAR(RAW_DATA) != 0x20
#error "2nd parameter shall be 0x20"
#endif

For a specific parameter check. This is not elegant.

+4

,

#defined RAW_DATA 0x11, 0x20, 0x55, 0x00, x044
#define RAW_DATA_PARAM_1(param1,param2,param3,param4,param5) param1 
#define RAW_DATA_PARAM_2(param1,param2,param3,param4,param5) param2
#define RAW_DATA_PARAM_3(param1,param2,param3,param4,param5) param3
#define RAW_DATA_PARAM_4(param1,param2,param3,param4,param5) param4
#define RAW_DATA_PARAM_5(param1,param2,param3,param4,param5) param5

#if RAW_DATA_PARAM_1(RAW_DATA) != 0x11
#error "wrong raw data param 1"
#elif RAW_DATA_PARAM_2(RAW_DATA) != 0x20
#error "wrong raw data param 2"
#elif RAW_DATA_PARAM_3(RAW_DATA) != 0x55
#error "wrong raw data param 3"
#elif RAW_DATA_PARAM_4(RAW_DATA) != 0x00
#error "wrong raw data param 4"
#elif RAW_DATA_PARAM_5(RAW_DATA) != 0x44
#error "wrong raw data param 5"
#endif
+2

Source: https://habr.com/ru/post/1626128/


All Articles