C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using a #define'd integer to create a #define'd string

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Francis DS
Guest





PostPosted: Mon Mar 01, 2004 12:28 pm    Post subject: Using a #define'd integer to create a #define'd string Reply with quote



Problem:

I have about 2,500 .h files with macro definitions like this:

#define MAXITEMS 400

where the value of MAXITEMS can vary in each file
(the 400 is an example).

I need to define MAXITEMS_STR to be a string with
the same sequence of digits as the integer.
IOW, I something like:

#define MAXITEMS_STR "400"

or even better do away with the macro and move to C++.
Something like:

const char * MAXITEMS_STR = "400";

I am trying to accomplish this in one .h file. Something like this:

mynew.h file
----------------
#include "the file that contains the definition of MAXITEMS as an integer"

// define MAXITEMS_STR using the value of MAXITEMS

#define MAXITEMS_STR "value of MAXITEMS" <--- how can I achieve this?
const char* MAXITEMS_STR = "value of MAXITEMS" <--- or this?

-fds

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Mon Mar 01, 2004 5:19 pm    Post subject: Re: Using a #define'd integer to create a #define'd string Reply with quote



In message <8653cb1a.0402291802.37919154 (AT) posting (DOT) google.com>, Francis DS
<francisds (AT) hotmail (DOT) com> writes
Quote:
Problem:

I have about 2,500 .h files with macro definitions like this:

#define MAXITEMS 400

where the value of MAXITEMS can vary in each file
(the 400 is an example).

I need to define MAXITEMS_STR to be a string with
the same sequence of digits as the integer.
IOW, I something like:

#define MAXITEMS_STR "400"

or even better do away with the macro and move to C++.
Something like:

const char * MAXITEMS_STR = "400";

I think that is a job for a program:-) I guess that using something such
as Perl or Python might be appropriate. Trying to fix it with some C or
C++ mechanism seems a poor idea to me.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sebastian Kapfer
Guest





PostPosted: Mon Mar 01, 2004 5:40 pm    Post subject: Re: Using a #define'd integer to create a #define'd string Reply with quote



On Mon, 01 Mar 2004 07:28:38 -0500, Francis DS wrote:

Quote:
Problem:

I have about 2,500 .h files with macro definitions like this:

#define MAXITEMS 400

where the value of MAXITEMS can vary in each file (the 400 is an
example).

I need to define MAXITEMS_STR to be a string with the same sequence of
digits as the integer. IOW, I something like:

#define MAXITEMS_STR "400"


#include <iostream>

#define NUMBER 400
#define STRINGIZE2(x) #x
#define STRINGIZE(x) STRINGIZE2(x)
const char *NUMBER_STR STRINGIZE(NUMBER)

int main()
{
int i = NUMBER;
const char *i_str = NUMBER_STR;
std::cout << i << " " << i_str << "n";
return 0;
}

Probably, you can also do this with some tricky templates and abandon the
C++ preprocessor. The above is the classic C-style solution.

--
Best Regards, | This signature is currently under construction.
Sebastian | Please check back later!
Quote:
--------------------------------------------------------
mailbox in "From" silently drops any mail > 20k

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Mon Mar 01, 2004 5:45 pm    Post subject: Re: Using a #define'd integer to create a #define'd string Reply with quote

Hi,

Francis DS wrote:
Quote:
Problem:

I have about 2,500 .h files with macro definitions like this:

#define MAXITEMS 400

where the value of MAXITEMS can vary in each file
(the 400 is an example).

I need to define MAXITEMS_STR to be a string with
the same sequence of digits as the integer.
IOW, I something like:

#define MAXITEMS_STR "400"

or even better do away with the macro and move to C++.
Something like:

const char * MAXITEMS_STR = "400";

You can experiment with this:

#include <iostream>

#define MAXITEMS 400
// more such definitions
// ...

// these two macros will automatically define the stringized
// version of the given macro
#define STRINGIZE_HELPER(m) #m
#define STRINGIZE(m) const char * m##_STR = STRINGIZE_HELPER(m);

// now, let's define MAXITEMS_STR
STRINGIZE(MAXITEMS)
// more such automatic definitions
// ...

int main()
{
// print the stringized value
std::cout << MAXITEMS_STR << 'n';
}

The solution above requires from you that you write those STRINGIZE(...)
definitions yourself.

The other possible solution is to write a script that will go through
your headers and that will smartly recognize #defs with integer values
(or maybe you already have some pattern that can make this recognition
easier) and generate the "const char *" definitions automatically.

If the number of your definitions is larger than a couple of tens, then
I'd certainly go the scripting way.

If you plan to have such code in the future, you can also experiment
with this:

#define DEF_NUM_AND_STR(name, val)
const long name = val;
const char *name##_str = #val;

and later:

DEF_NUM_AND_STR(MAXITEMS, 400)
// ...

This way, you get two definitions (one for long and one for string) in a
single line. If you later change the name or value, there is a single
place to do so.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Tue Mar 02, 2004 11:33 am    Post subject: Re: Using a #define'd integer to create a #define'd string Reply with quote

Francis DS wrote:
Quote:
Problem:

I have about 2,500 .h files with macro definitions like this:

#define MAXITEMS 400

where the value of MAXITEMS can vary in each file
(the 400 is an example).

I don't think I want to know why you're doing this.

Quote:
I need to define MAXITEMS_STR to be a string with
the same sequence of digits as the integer.
IOW, I something like:

#define MAXITEMS_STR "400"

or even better do away with the macro and move to C++.
snip


If you need the conversion to be done at compile time you must use the
preprocessor:

#define STR2(x) #x
#define STR(x) STR2(x)
#define MAXITEMS_STR STR(MAXITEMS)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.