Using different constants for different porjects.

V

valentin tihomirov

Hello,
I've written a program for a usb controller. I want to change some data
strings for another project so that program code remains the same. Some data
is hardcoded like

BYTE code UsbRomStringsLangId[] = {
4, // Length of language descriptor ID
3, // LANGID tag
9, // Low byte of 0x0409 (English)
4 // High byte of 0x0409 (English)
};
BYTE code UsbRomAcmInterfaceString[USB_ACM_INTERFACE_STRING_LENGTH] = {
USB_ACM_INTERFACE_STRING_LENGTH, DESC_TYPE_STRING, //"CDC-ACM Comm"
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'A', 0, 'C', 0, 'M', 0, ' ', 0, 'C', 0,
'o', 0, 'm', 0, 'm', 0, 0, 0};
BYTE code UsbRomCdcInterfaceString[USB_CDC_INTERFACE_STRING_LENGTH] = {
USB_CDC_INTERFACE_STRING_LENGTH, DESC_TYPE_STRING,
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'A', 0, 'C', 0, 'M', 0, ' ', 0, 'D', 0,
'a', 0, 't', 0, 'a', 0, 0, 0};

How can I do that?
 
M

Mike Wahler

valentin tihomirov said:
Hello,
I've written a program for a usb controller. I want to change some data
strings for another project so that program code remains the same. Some data
is hardcoded like

BYTE code UsbRomStringsLangId[] = {
4, // Length of language descriptor ID
3, // LANGID tag
9, // Low byte of 0x0409 (English)
4 // High byte of 0x0409 (English)
};
BYTE code UsbRomAcmInterfaceString[USB_ACM_INTERFACE_STRING_LENGTH] = {
USB_ACM_INTERFACE_STRING_LENGTH, DESC_TYPE_STRING, //"CDC-ACM Comm"
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'A', 0, 'C', 0, 'M', 0, ' ', 0, 'C', 0,
'o', 0, 'm', 0, 'm', 0, 0, 0};
BYTE code UsbRomCdcInterfaceString[USB_CDC_INTERFACE_STRING_LENGTH] = {
USB_CDC_INTERFACE_STRING_LENGTH, DESC_TYPE_STRING,
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'A', 0, 'C', 0, 'M', 0, ' ', 0, 'D', 0,
'a', 0, 't', 0, 'a', 0, 0, 0};

How can I do that?

Remove the hardcoding, store your strings externally (e.g. in a file
or 'registry', etc.), and have the programs load them.

-Mike
 
R

Rob Thorpe

valentin tihomirov said:
Hello,
I've written a program for a usb controller. I want to change some data
strings for another project so that program code remains the same. Some data
is hardcoded like

BYTE code UsbRomStringsLangId[] = {
4, // Length of language descriptor ID
3, // LANGID tag
9, // Low byte of 0x0409 (English)
4 // High byte of 0x0409 (English)
};
BYTE code UsbRomAcmInterfaceString[USB_ACM_INTERFACE_STRING_LENGTH] = {
USB_ACM_INTERFACE_STRING_LENGTH, DESC_TYPE_STRING, //"CDC-ACM Comm"
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'A', 0, 'C', 0, 'M', 0, ' ', 0, 'C', 0,
'o', 0, 'm', 0, 'm', 0, 0, 0};
BYTE code UsbRomCdcInterfaceString[USB_CDC_INTERFACE_STRING_LENGTH] = {
USB_CDC_INTERFACE_STRING_LENGTH, DESC_TYPE_STRING,
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'A', 0, 'C', 0, 'M', 0, ' ', 0, 'D', 0,
'a', 0, 't', 0, 'a', 0, 0, 0};

How can I do that?

Rather than defining these variables at compile time do so at runtime
during initialisation. Write a function to convert a string into the
form you need, so:

interleave_with_zero("CDC-ACM Comm")

returns 'C', 0, 'D', 0, 'C', 0, ...

You should do something like this anyway, for clarity.
 
T

Thad Smith

valentin said:
Hello,
I've written a program for a usb controller. I want to change some data
strings for another project so that program code remains the same. Some data
is hardcoded like

BYTE code UsbRomStringsLangId[] = {
4, // Length of language descriptor ID
3, // LANGID tag
9, // Low byte of 0x0409 (English)
4 // High byte of 0x0409 (English)
};
.....

A common way is to use conditional coding for the variant parts:

#if SYSTEM==SYSTEM_A
const char foo[] = {"init sys A"};
#elif SYSTEM==SYSTEM_B
const char foo[] = {"init sys B"};
#else
#error SYSTEM undefined
#endif

You can have your makefile or project file define SYSTEM for the
perspective build.

Another way is to isolate all the system-dependent items in a module
which ONLY holds the dependent items and only compile the version that
matches the target being generated.

Thad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top