preprocessor: how to operate on strings

Discussion in 'C++' started by abir, Apr 11, 2008.

  1. abir

    abir Guest

    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
     
    abir, Apr 11, 2008
    #1
    1. Advertising

  2. abir

    red floyd Guest

    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
     
    red floyd, Apr 11, 2008
    #2
    1. Advertising

  3. abir

    abir Guest

    On Apr 11, 10:34 am, red floyd <> wrote:
    > 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
     
    abir, Apr 11, 2008
    #3
  4. abir

    James Kanze Guest

    On Apr 11, 7:48 am, abir <> wrote:
    > On Apr 11, 10:34 am, red floyd <> wrote:
    > > 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 11, 2008
    #4
  5. abir <> writes:
    <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.
     
    Ben Bacarisse, Apr 11, 2008
    #5
  6. "Robbie Hatley" <> writes:

    > "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.
     
    Ben Bacarisse, Apr 11, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. benn
    Replies:
    2
    Views:
    601
  2. Maya Young

    Use C# to operate a mobile device

    Maya Young, Apr 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    623
    Maya Young
    Apr 17, 2004
  3. Dux
    Replies:
    1
    Views:
    290
    Mike Wahler
    Oct 5, 2003
  4. Cronus
    Replies:
    1
    Views:
    676
    Paul Mensonides
    Jul 15, 2004
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page