|
What kind of string you are dealing with (e.g. CString or whatever...)?
Maybe posting a little code snippet will help.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
A TCHAR array. Any ideas?
regards,
George
|
|
|
|
|
I don't know details of the code you're dealing with (since you did post no snippet) hence I can't help: maybe there are smart ways to do what you need, but, unfortunately I'm not a fortune teller.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks CPallini,
Here is some pseudo code, it is appreciated if you could share some ideas. Let me know if you need any further information.
<br />
#define PREFIX "FOO"<br />
<br />
int main()<br />
{<br />
TCHAR array_before [5][256] = {_T("FILE1"), _T("FILE2"), _T("FILE3"), _T("FILE4"), _T("FILE5")};<br />
<br />
<br />
<br />
}<br />
<br />
<br />
regards,<br />
George
|
|
|
|
|
The best way was already suggested by toxcct [^].
BTW why don't you make a search-and-replace with your current editor?
On the other hand, since strings obey a regular pattern, you have the option to keep code compact, building such pattern at runtime.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hi CPallini,
His (her) code does not compile. How to do a search and replace? I want to add prefix... nothing to replace.
regards,
George
|
|
|
|
|
George_George wrote: His (her) code does not compile .
IMPOSSIBLE.
George_George wrote: How to do a search and replace?
With search-and-replace tool of your favourite IDE (e.g. Visual Studio) or editor (e.g. notepad++).
George_George wrote: I want to add prefix... nothing to replace.
Yeah, but with the support of (a bit of) imagination:
_T("FILEX") becomes _T("FOOFILEX")
if you search for _T("FILE and replace it with _T("FOOFILE
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hi CPallini,
Could you try the code please? I am using Visual Studio 2005. The code can not compile. If there is any programming error in the code, please feel free to let me know.
<br />
#define PREFIX(x) _T("FOO##x");<br />
<br />
<br />
int main()<br />
{<br />
<br />
const TCHAR* SecondArray [2] = {PREFIX(_T("FILE1")), PREFIX(_T("FILE2"))};<br />
<br />
TCHAR* p = NULL;<br />
p = SecondArray [0];<br />
p = SecondArray [1];<br />
<br />
<br />
return 0;<br />
}<br />
main.cpp(9) : error C2143: syntax error : missing '}' before ';'
main.cpp(9) : error C2143: syntax error : missing ';' before ','
main.cpp(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(12) : error C2040: 'p' : 'int' differs in levels of indirection from 'TCHAR *'
main.cpp(12) : error C2065: 'SecondArray' : undeclared identifier
main.cpp(13) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(13) : error C2040: 'p' : 'int' differs in levels of indirection from 'TCHAR *'
main.cpp(16) : error C2059: syntax error : 'return'
main.cpp(17) : error C2059: syntax error : '}'
main.cpp(17) : error C2143: syntax error : missing ';' before '}'
main.cpp(17) : error C2059: syntax error : '}'
regards,
George
|
|
|
|
|
Anyway, the following one compiles:
#define PREFIX(x) _T("FOO") _T(#x)
int main()
{
const TCHAR* SecondArray [2] = {PREFIX(FILE1), PREFIX(FILE2)};
const TCHAR* p = NULL;
p = SecondArray [0];
p = SecondArray [1];
}
Be careful: copy exactly as it stands
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Sure? I mean, nesting macros? Is that version-dependent?
No, it is not:
"...the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement."
Looks like your macro will expand to nonsense...;P
_T("FOO") _T(FILE1)
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Hi jhwurmbach,
I do not quite understand your points. The code works in my Visual Studio 2005 environment. Could you show some pseudo code to express your ideas please?
regards,
George
|
|
|
|
|
Report of the macro expansion on my system:
const TCHAR* SecondArray [2] = {L"FOO" L"FILE1", L"FOO" L"FILE2"};
Then compiler string concatenation makes the trick.
BTW it was only an academic question, since the search-and-replace tool of his favourite editor can do it painless.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks CPallini,
regards,
George
|
|
|
|
|
Macro?
Macros are expanded before the compiler runs. It doesn't even get to see the unexpanded macro.
So, when you know the "FILEn" and the FOO-part at compile time, why don't you #define the strings as "FOOFILEn" in the first place?
If you don't know the filename or the prefix at compiletime (i.e. you only get it at runtime) there is not much a macro can do for you.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Thanks jhwurmbach,
I am just thinking that defining N macros is somewhat stupid.
Like,
#define NEWFILE1 "FOOFILE1"
...
#define NEWFILEN "FOOFILEN"
My situation is, I have some legacy code, which is an array -- very long, and all the file names are constant values. And to save my time of typing, I want to find some smarter ways to add prefix to all file names in the aray.
Any good ideas?
regards,
George
|
|
|
|
|
George_George wrote: My situation is, I have some legacy code, which is an array [...] I want to find some smarter ways to add prefix to all file names in the aray.
If you already have the filenames in an array of strings, then you can loop over the contents of the array and prepend your prefix to any one of them.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Thanks jhwurmbach,
You mean using something like strcat to calculate the string at runtime? I think if we could determine the values at compile time (this is why I am asking people here to see whether we could use MACRO or something to determine the string values at compile time), the performance of runtime will be better.
regards,
George
|
|
|
|
|
George_George wrote: if we could determine the values at compile time the performance of runtime will be better.
So, you have some code-file where a long, long list of strings is added into an array?
Or do you read some sort of text file at startup?
Sorry, I can only give vague suggestions, but you are not providing enough information.
You have a fixed-size array of TCHARs.
When is this array filled?
From where is it filled?
How often is is accessed? At different code locations?
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Thanks jhwurmbach,
Here is some pseudo code, it is appreciated if you could share some ideas. Let me know if you need any further information.
<br />
#define PREFIX "FOO"<br />
<br />
int main()<br />
{<br />
TCHAR array_before [5][256] = {_T("FILE1"), _T("FILE2"), _T("FILE3"), _T("FILE4"), _T("FILE5")};<br />
<br />
<br />
<br />
}<br />
<br />
<br />
regards,<br />
George
|
|
|
|
|
As CPallini and toxcct suggested, search&replace in the Editor or using Macro-Stringconcatenation if you are suspicious that the prefix might change in the future is probably the way to go.
Or loading the array content from a data-file at runtime, prefixing on the fly.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Hi jhwurmbach,
For your idea,
> Or loading the array content from a data-file at runtime, prefixing on the fly.
You mean using strcat or something similar?
I have tried to use ##, but the code does not compile.
<br />
#define PREFIX(x) _T("FOO")##x<br />
<br />
int main()<br />
{<br />
<br />
const TCHAR* SecondArray [2] = {_T("FILE1"), _T("FILE2")};<br />
<br />
TCHAR* p = NULL;<br />
p = PREFIX(SecondArray [0]);<br />
p = PREFIX(SecondArray [1]);<br />
<br />
<br />
return 0;<br />
}<br />
Error information,
main.c(11) : error C2146: syntax error : missing ';' before identifier 'SecondArray'
main.c(12) : error C2146: syntax error : missing ';' before identifier 'SecondArray'
regards,
George
|
|
|
|
|
Macros are evil, try using an STL container and use foreach() or something similar to process the containers items and prepend "FOO" to each item.
|
|
|
|
|
Jonathan [Darka] wrote: Macros are evil
No.
(Provided you know what are you doing).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks for your advice, Darka!
I think your solution will calculate the string at runtime? I think if we could determine the values at compile time (this is why I am asking people here to see whether we could use MACRO or something to determine the string values at compile time), the performance of runtime will be better.
regards,
George
|
|
|
|
|
#define FOOPREFIX(x) "FOO"##x
but i strongly advise against using such things
|
|
|
|
|