I have to maintain C code that has both generic components and product specific components. I would like to simplify my code so that I have just one generic product.h file which has a structure like
#if (PRODUCT_ID == 1)
#define PRODUCT_NAME Product1
#else
#if (PRODUCT_ID == 2)
#define PRODUCT_NAME Product2
#else
#error "Unsupported product id"
#endif
#endif
Then, whenever I have a header foo.h which has product specific components, I would like to use syntax like this
#include "product.h"
#include PRODUCT_SPECIFIC_INCLUDE
where PRODUCT_SPECIFIC_INCLUDE
should be derived from __FILE__
and PRODUCT_NAME
macro in such a way that it would translate to
#include "Product1/foo.h"
that is, the product specific header file has the same filename as the generic file, but is located in a product specific folder, whose name is the value of PRODUCT_NAME
macro.
It seems that whatever I try has preprocessor stringification issues. I can't be the first to want such a structure. What am I missing?
Update
Here is what I currently have for PRODUCT_SPECIFIC_INCLUDE
which does not work
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define PRODUCT_SPECIFIC_INCLUDE TOKENPASTE2(PRODUCT_SPECIFIC, __FILE__)
PRODUCT_SPECIFIC_INCLUDE
PRODUCT_SPECIFIC_INCLUDE
which does not work <code>#define TOKENPASTE(x, y) x ## y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) #define PRODUCT_SPECIFIC_INCLUDE TOKENPASTE2(PRODUCT_SPECIFIC, __FILE__)</code>