473,599 Members | 3,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preprocessor: how to operate on strings

Hi,
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?

i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.

like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -myID (string, using preprocessor) -MYID (using
template) .

I am interested in a compile time solution, runtime one is trivial.

thanks
abir basak
Apr 11 '08 #1
10 2133
abir wrote:
Hi,
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?

i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.

like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -myID (string, using preprocessor) -MYID (using
template) .

I am interested in a compile time solution, runtime one is trivial.
read up on the ## token pasting operator.

#define MY(id) my ## id

MY(Word) // generates myWord
MY(Goodness) // generates myGoodness
Apr 11 '08 #2
On Apr 11, 10:34 am, red floyd <no.s...@here.d udewrote:
abir wrote:
Hi,
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?
i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.
like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -myID (string, using preprocessor) -MYID (using
template) .
I am interested in a compile time solution, runtime one is trivial.

read up on the ## token pasting operator.

#define MY(id) my ## id

MY(Word) // generates myWord
MY(Goodness) // generates myGoodness
Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2
};
i want to make a few strings from it as TYPE1, TYPE2 , where the input
is meType1, meType2 and NOT anything else (which will be done using
some seq preprocessor constructs from boost) .
So to say, i want to apply some construct statically of meType1 to
generate TYPE1 either using preprocessor or using template.

thanks
abir
Apr 11 '08 #3
On Apr 11, 7:48 am, abir <abirba...@gmai l.comwrote:
On Apr 11, 10:34 am, red floyd <no.s...@here.d udewrote:
abir wrote:
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?
i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.
like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -myID (string, using preprocessor) -MYID (using
template) .
I am interested in a compile time solution, runtime one is trivial.
read up on the ## token pasting operator.
#define MY(id) my ## id
MY(Word) // generates myWord
MY(Goodness) // generates myGoodness
Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2 };
i want to make a few strings from it as TYPE1, TYPE2 , where
the input is meType1, meType2 and NOT anything else (which
will be done using some seq preprocessor constructs from
boost) .
You can't, really. You have two choices: either write a small
parser yourself (e.g. ignoring everything but enum's), and use
it to generate the strings, or write a small program which
generates both the enum and the strings from some simply
formatted input. I've done the first, and it's not that hard.
And in your case, the second would only be about 10 lines of GNU
awk. (Standard awk doesn't have a toupper function, so you'd
have to implement that as well.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 11 '08 #4
abir <ab*******@gmai l.comwrites:
<snip>
Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2
};
i want to make a few strings from it as TYPE1, TYPE2 , where the input
is meType1, meType2 and NOT anything else (which will be done using
some seq preprocessor constructs from boost) .
So to say, i want to apply some construct statically of meType1 to
generate TYPE1 either using preprocessor or using template.
You might consider using an extra pre-processor step. M4 (a
general-purpose macro processor) can be told to use sufficiently
obscure syntax that it is unlikely to mess with any of your C++ but
will pick out exactly those bits where you want to do this sort of
transformation.

--
Ben.
Apr 11 '08 #5

"abir" asked:
This is not strictly a C++ language question.
It's on-topic, though. The preprocessor is a part of C++.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?
Some really nifty things can be done with the preprocessor.
I used the following to cut several hundred messy lines of
error-prone cut-n-paste code down to about a dozen lines in
the program I'm maintaining here at work:

#define AUXI_ITEM(r,t,c ) IG ## r ## _ ## t ## c

The "##" directive is a "string paster".
This macro builds a string out of 3 input strings (t, r, c).
It just pastes together "IG", r, "_", t, c.

For example, the preprocessor would convert

AUXI_ITEM ( 07 , AgQm , 3375 )

to:

IG07_AgQm3375
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Apr 11 '08 #6
"Robbie Hatley" <se************ **@for.my.email .addresswrites:
"abir" asked:
>This is not strictly a C++ language question.

It's on-topic, though. The preprocessor is a part of C++.
>Is there any way to form a string from an identifier with the
preprocessor with some operations ?

Some really nifty things can be done with the preprocessor.
I used the following to cut several hundred messy lines of
error-prone cut-n-paste code down to about a dozen lines in
the program I'm maintaining here at work:

#define AUXI_ITEM(r,t,c ) IG ## r ## _ ## t ## c

The "##" directive is a "string paster".
Small point: it is usually called a token paster. # is the one that
turns a macro argument into a string literal.
This macro builds a string out of 3 input strings (t, r, c).
Really, it builds a token out 3 other tokens. I know you know this.
I just think it help to keep the terminology clear.
It just pastes together "IG", r, "_", t, c.

For example, the preprocessor would convert

AUXI_ITEM ( 07 , AgQm , 3375 )

to:

IG07_AgQm3375
--
Ben.
Apr 11 '08 #7
On Apr 11, 7:48 am, abir <abirba...@gmai l.comwrote:
On Apr 11, 10:34 am, red floyd <no.s...@here.d udewrote:
abir wrote:
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?
i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.
like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -myID (string, using preprocessor) -MYID (using
template) .
I am interested in a compile time solution, runtime one is trivial.
read up on the ## token pasting operator.
#define MY(id) my ## id
MY(Word) // generates myWord
MY(Goodness) // generates myGoodness
Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2 };
i want to make a few strings from it as TYPE1, TYPE2 , where
the input is meType1, meType2 and NOT anything else (which
will be done using some seq preprocessor constructs from
boost) .
You can't, really. You have two choices: either write a small
parser yourself (e.g. ignoring everything but enum's), and use
it to generate the strings, or write a small program which
generates both the enum and the strings from some simply
formatted input. I've done the first, and it's not that hard.
And in your case, the second would only be about 10 lines of GNU
awk. (Standard awk doesn't have a toupper function, so you'd
have to implement that as well.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
abir <ab*******@gmai l.comwrites:
<snip>
Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2
};
i want to make a few strings from it as TYPE1, TYPE2 , where the input
is meType1, meType2 and NOT anything else (which will be done using
some seq preprocessor constructs from boost) .
So to say, i want to apply some construct statically of meType1 to
generate TYPE1 either using preprocessor or using template.
You might consider using an extra pre-processor step. M4 (a
general-purpose macro processor) can be told to use sufficiently
obscure syntax that it is unlikely to mess with any of your C++ but
will pick out exactly those bits where you want to do this sort of
transformation.

--
Ben.
Jun 27 '08 #9

"abir" asked:
This is not strictly a C++ language question.
It's on-topic, though. The preprocessor is a part of C++.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?
Some really nifty things can be done with the preprocessor.
I used the following to cut several hundred messy lines of
error-prone cut-n-paste code down to about a dozen lines in
the program I'm maintaining here at work:

#define AUXI_ITEM(r,t,c ) IG ## r ## _ ## t ## c

The "##" directive is a "string paster".
This macro builds a string out of 3 input strings (t, r, c).
It just pastes together "IG", r, "_", t, c.

For example, the preprocessor would convert

AUXI_ITEM ( 07 , AgQm , 3375 )

to:

IG07_AgQm3375
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2332
by: Eric | last post by:
Hello all, I've got this line of given code (cannot change this; wizard-generated, but value may change someday): #define IDB_BUTTON 225 Somewhere in the code I found / need this: .....somefunction("#225").... This string in the function call is the same number like in the define, but the constant wasn't used. Because for future changes and for readability I
4
1971
by: Neil Zanella | last post by:
Hello, I would like to have the preprocessor form a string containing the value of INT_MAX from limits.h but I am fairly certain that this is impossible using C alone... yes I could call sprintf, but I would have liked some solution based on the preprocessor, so that I can have my string literal ready at compile time instead of at run time. Oh well.. Thanks,
9
3649
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor phase only. I am not using the standalone program 'cpp' because proper functioning of my project...
7
44405
by: Erik Leunissen | last post by:
L.S. How can I make certain code parts be compiled conditionally, depending on the definition of a macro such as: #define VERSION "2.3" Is it all right to do things like:
3
13514
by: CPA Study Group | last post by:
Is it possible to write a C Preprocessor macro which changes the case of a string. This will save me time by not having to use awk of write my own preprocessor. Here is a sample code which I want to work --------- #ifdef LOWER #define NORMAL(x) MKLOWER(x) #else #define NORMAL(x) MKUPPER(x) #endif
3
7519
by: John Devereux | last post by:
Hi, I would like to be able to define a number with the preprocessor, then be able to use it to generate both numeric and string constants. For example, #define VERSION_MAJOR 4 #define VERSION_MINOR 47
2
1375
by: I am Sam | last post by:
I want declare some const strings in my application but I don't want to repeat the constents in every page I write in my codebehind I was wondering if there is some kind of directive or preprocessor I can declare at the top of my codebehind that will import the constents from a seperate file. I hope I'm using the correct language I am not sure if it is understandable but I have to try. Thank you in adavance to anyone that will reply. ...
5
2533
by: .rhavin grobert | last post by:
i read that the preproc will parse macros inside a string if they are prefixed with a sharp. so i did.... ________________________- #define MAJORRELEASE 0 #define PATCHLEVEL 7 #ifdef _DEBUG #define MINORRELEASE 5 #else
4
1704
by: gpearman | last post by:
Hi all, I'm trying to generate a static string from two defines that are passed in on the command line during make (via -D). I'll admit that my understanding of the C preprocessor is not great ,and I can't work out how to join the two together at compile time. At the moment I've tried static const U8BIT *app_sw_ver_no_str = (U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING
0
7992
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8398
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8400
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8051
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2414
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.