preprocessor: how to operate on strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abir

    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
  • red floyd

    #2
    Re: preprocessor: how to operate on strings

    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

    Comment

    • abir

      #3
      Re: preprocessor: how to operate on strings

      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

      Comment

      • James Kanze

        #4
        Re: preprocessor: how to operate on strings

        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:james.kan ze@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

        Comment

        • Ben Bacarisse

          #5
          Re: preprocessor: how to operate on strings

          abir <abirbasak@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.

          Comment

          • Robbie Hatley

            #6
            Re: preprocessor: how to operate on strings


            "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


            Comment

            • Ben Bacarisse

              #7
              Re: preprocessor: how to operate on strings

              "Robbie Hatley" <see.my.signatu re@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.

              Comment

              • James Kanze

                #8
                Re: preprocessor: how to operate on strings

                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:james.kan ze@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

                Comment

                • Ben Bacarisse

                  #9
                  Re: preprocessor: how to operate on strings

                  abir <abirbasak@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.

                  Comment

                  • Robbie Hatley

                    #10
                    Re: preprocessor: how to operate on strings


                    "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


                    Comment

                    • Ben Bacarisse

                      #11
                      Re: preprocessor: how to operate on strings

                      "Robbie Hatley" <see.my.signatu re@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.

                      Comment

                      Working...