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
costAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

B1-66ERCommented:
>>The problem is then that the preprocessor doesn't expand COMPANY_NAME to xxx or yyy.

try

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marcodalzottoCommented:
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
B1-66ERCommented:
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".
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

marcodalzottoCommented:
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
B1-66ERCommented:
yes, yes Marco, i understand, thats why i describe, why i post those message :))
marcodalzottoCommented:
I'm Italian, so often I don't understand all meanings, sorry  :))
rstaveleyCommented:
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
costAuthor Commented:
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.
rstaveleyCommented:
Perl is a particularly good tool for text manipulation, but you can write a C++ utility do do the same.

e.g. As a filter STDIN -> STDOUT:
--------8<--------
#include <iostream>
#include <string>

int main(int argc,const char*argv[])
{
      if (argc != 3)
            return (std::cerr << "Usage: " << argv[0] << " \"Original text\" \"Replacement text\" < {inputfile} > {outputfile}\n"),1;

      std::string original_text = argv[1];
      std::string replacement_text = argv[2];

      std::string line;
      while (getline(std::cin,line)) {
            std::string::size_type pos;
            while ((pos = line.find(original_text)) != std::string::npos)
                  line.replace(pos,original_text.size(),replacement_text);
            std::cout << line << '\n';
      }
}
--------8<--------
costAuthor Commented:
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.
jhshuklaCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.