How to convert a $(macros) into a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DonMatteo
    New Member
    • Mar 2008
    • 5

    How to convert a $(macros) into a string

    Hi there,

    I am writing a code to print the results for any given project into the corresponding solution directory. Here is my idea

    //*** CODE***//

    // In C/C++ properties, Preprocessor, "Preprocess or Definitions":
    MY_SOLUTIONDIR= $(SolutionDir)

    // In the cpp file:
    #define OP OP2(MY_SOLUTION DIR)
    #define OP2(x) OP3(x)
    #define OP3(a) #a

    string print_folder = OP;
    print(print_fol der, my_results);


    //*** RESULTS***//
    MY_SOLUTIONDIR = C:\Projects\My_ project
    print_folder = "C: ProjectsMy_proj ect" (here I lose the backslashes)

    I need to convert MY_SOLUTIONDIR into a string for my print function. With my 3-level macro OP I manage to add quotation marks to get a string but I have a problem with the backslash as C++ requires double backslashes.

    Any idea on how to convert the MY_SOLUTIONDIR into a 'good' string? Any other idea on how to use the $(SolutionDir) of the preprocessor for a function requiring a string as an input?

    Many thanks,

    Matthieu
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    C++ does not require double backslashes. Windows paths use a backslash and that is the C/C++ escape sequence. So to get one backslash you need \\ in the string.

    Just go into your print_folder string and double up the backslashes. The compiler will force a backslash when the backslash escape sequence is entountered. Therefore, in your compiled string there will be one backslash.

    BTW: There a five major features in C++ to replace macros. You should not be using macros in C++.

    Comment

    • Sick0Fant
      New Member
      • Feb 2008
      • 121

      #3
      Originally posted by weaknessforcats
      BTW: There a five major features in C++ to replace macros. You should not be using macros in C++.
      Tell that to my prof in college :-).

      Comment

      • DonMatteo
        New Member
        • Mar 2008
        • 5

        #4
        Originally posted by weaknessforcats
        C++ does not require double backslashes. Windows paths use a backslash and that is the C/C++ escape sequence. So to get one backslash you need \\ in the string.

        Just go into your print_folder string and double up the backslashes. The compiler will force a backslash when the backslash escape sequence is entountered. Therefore, in your compiled string there will be one backslash.

        BTW: There a five major features in C++ to replace macros. You should not be using macros in C++.
        Thanks for your answer,

        But if you read again my post, you ll notice that there is no backslash to double since they are erased.
        The problem is:
        1) I cannot work on the macro since it is not a string and not a char[], it is just a raw text replaced by the preprocessor at compilation time. The only solution I found is to use another macro OP(x) #x to add quotation marks.
        2) As soon as I convert the macro into a string, the backslashes are seen as an escape statement. I think it is IMPOSSIBLE to double the backslashes in the macro, first i need to convert it into a string. But if I try to do so, then I have a proble... see 1)

        The only solution for me is to use another string class, like a verbatim class in C# which accepts single backslash...??? ?
        Who will be strong enought to break this problem...?

        Comment

        • Sick0Fant
          New Member
          • Feb 2008
          • 121

          #5
          Originally posted by DonMatteo
          Thanks for your answer,

          But if you read again my post, you ll notice that there is no backslash to double since they are erased.
          The problem is:
          1) I cannot work on the macro since it is not a string and not a char[], it is just a raw text replaced by the preprocessor at compilation time. The only solution I found is to use another macro OP(x) #x to add quotation marks.
          2) As soon as I convert the macro into a string, the backslashes are seen as an escape statement. I think it is IMPOSSIBLE to double the backslashes in the macro, first i need to convert it into a string. But if I try to do so, then I have a proble... see 1)

          The only solution for me is to use another string class, like a verbatim class in C# which accepts single backslash...??? ?
          Who will be strong enought to break this problem...?
          They already broke this problem by developing the C++ language. Use it to your advantage.

          Comment

          • DonMatteo
            New Member
            • Mar 2008
            • 5

            #6
            Originally posted by Sick0Fant
            They already broke this problem by developing the C++ language. Use it to your advantage.
            Anybody who understands the question?
            I have a macro FOLDER with raw text C:\Myproject
            I need to get a string "C:\\Myproj ect"

            2 alternatives:

            1)
            OP(x) #x
            string my_folder = OP(FOLDER )
            doublebackslash (my_folder)

            Problem is the single backslash is erased in the second line when I convert it to a string. So no way to double it since it is seen as an escape statement and then erased.

            2)
            double the backslash in the macro first.
            Problem is it is impossible since it is a macro, just raw text replaced by processor at compilation time, it s not a string or a char[], it s nothing so icant do anything before to convert it

            need to convert it first, but if I convert it then the single backslash is a problem...
            Any idea? any other way to get a 'clean' string with the solution directory, sinble or double backslashes but a STRING!!!

            Comment

            • Sick0Fant
              New Member
              • Feb 2008
              • 121

              #7
              If you are using C++, don't use macros!! Crikey.

              Comment

              Working...