Link to home
Start Free TrialLog in
Avatar of cost
cost

asked on

Concat and expand strings in resource files.

The scenario is this. I have written a program and used a company name in many dialogs. Now they program to be sold by another company as well, and then all dialogs must have the other company's name. I'm guessing there's a chance that this will happen again, so instead of changing all dialogs I'm thinking of having the dialogs build dynamicly. Now to the problem. Since I was in a hurry (and still am), no string resources were used. Also, every dialog was created within the resource editor (VC++). So I was thinking somehing like this:

// use the define needed for this build
#define COMPANY_NAME xxx
//#define COMPANY_NAME yyy

// Suppose the original text is something like "Hello and welcome to this great program by COMPANY_NAME. If you found any bugs, it's your own fault for doing things wrong so please don't call."
#define _MY_TEXT_FOR_THIS_DIALOG_ "bla bla bla" ## COMPANY_NAME ## "bla bla bla"

//Then in the resource
BEGIN
   LTEXT _MY_TEXT_FOR_THIS_DIALOG_, IDC_STATIC,1,10,100,20
END

#undefine _MY_TEXT_FOR_THIS_DIALOG_

The problem is then that the preprocessor doesn't expand COMPANY_NAME to xxx or yyy.

I know how to solve the problem by rewriting lot's of code, but I guess it should be possible to do something like this?

Thanks,

Andreas
ASKER CERTIFIED SOLUTION
Avatar of B1-66ER
B1-66ER

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cost I tried your solution and works, the only thing you have to do is to put your define in the resouce.h, or one of the headers files included by the rc file.
The issue with your and B1-66ER solution is that the company would appear with apex ("). This is a problem when the preprocessor concatenates strings.
The only solution I see is to write the company is inside the define:

#define _MY_TEXT_FOR_THIS_DIALOG_ "bla bla bla yourcompanyname bla bla bla"

remember to put the define in the resouce.h

Regards, Marco
Avatar of B1-66ER
B1-66ER

Hi marcodalzotto.
Before posting this message i`ll try such concatenation using gcc, and then VC++ 6.0, in such way
in gcc:

#define COMPANY_NAME "xxx"
#define _MY_TEXT_FOR_THIS_DIALOG_ "bla bla bla" COMPANY_NAME "bla bla bla"

int main() {  puts(_MY_TEXT_FOR_THIS_DIALOG_) ; return(0) ;}

in studio:

#define COMPANY_NAME "xxx"
#define _MY_TEXT_FOR_THIS_DIALOG_ "bla bla bla" COMPANY_NAME "bla bla bla"

int main()
{
      MessageBox(0,_MY_TEXT_FOR_THIS_DIALOG_,"",MB_OK) ;
      
      return 0;
}

everywhere i get message "bla bla blaxxxbla bla bla".
In this you are right, I've tried as you sed and with MessageBox works fine. But when I use the define with a dialog resource as cost situation is I get the apex ("). I've tried some code like:

#define stringer(a, b, c) #a#b#c
#define _MY_TEXT_FOR_THIS_DIALOG_ stringer(##bla bla##, ##Company##, ##bla bla##)

but it is the same problem
yes, yes Marco, i understand, thats why i describe, why i post those message :))
I'm Italian, so often I don't understand all meanings, sorry  :))
If all else fails, you can run your source code modules through a text substitution during the compile.

e.g. with Perl (see http://www.activestate.com/Products/ActivePerl/)

   perl -ibak -p -e "s/Acme Corporation/Bugs Bunny Ltd/g" your_header.h your_source.cpp
Avatar of cost

ASKER

Perl is not really an option. Then I prefer, remaking everything so that I create the dialog texts without the resource editor.

It seems like the resource compiler doesn't handle string concatenation very well. The suggestion with,
#define _MY_TEXT_FOR_THIS_DIALOG_ "bla bla bla yourcompanyname bla bla bla"
doesn't really work either, since I then have to change company name in all dialog texts and that is exactly what I want to avoid.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cost

ASKER

Yes that will probably work in most cases, but the method is a bit clumpsy. I have different resourse files, and if more stuff is added I think it's easier to just use the MY_COMPANY parameter, than to add which files should be included in the substitution and which strings to substitute.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Split between B1-66ER and rstaveley

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

jhshukla
EE Cleanup Volunteer