Preprocessor macro substitution

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

    Preprocessor macro substitution


    I've never really done too much stuff with macros. I have a program
    here where an target server address is automatically generated from
    WSDL. For testing, I want to use a localhost address.

    I could do it by overriding the endpoint explicitly, but I'd rather
    not. It would be cleaner to define a local testing environment.

    My first thought was just to use a #define, like:

    #define foo.bar localhost

    However, that's no good as foo.bar tain't a legal identifier with that
    '.' in it. Anyone have some clever preprocessor way to get what I want?





    Brian

  • Eric Sosman

    #2
    Re: Preprocessor macro substitution

    Default User wrote:
    I've never really done too much stuff with macros. I have a program
    here where an target server address is automatically generated from
    WSDL. For testing, I want to use a localhost address.
    >
    I could do it by overriding the endpoint explicitly, but I'd rather
    not. It would be cleaner to define a local testing environment.
    >
    My first thought was just to use a #define, like:
    >
    #define foo.bar localhost
    >
    However, that's no good as foo.bar tain't a legal identifier with that
    '.' in it. Anyone have some clever preprocessor way to get what I want?
    What you haven't told us is where you're starting: How do
    you get hold of the server address to begin with? "From WSDL"
    doesn't seem to me a very complete description ...

    If the C source code looks like

    doSomething("fo o.bar");

    .... then I think you're out of luck, unless you can muck around
    with the insides of doSomething(). But if it looks like

    char[] server = "foo.bar";
    ...
    doSomething(ser ver);

    .... you might be able to add `#define server "localhost" ' somewhere
    between the two lines -- it's dirty, but it might work. Or if the
    source looks like

    #define SERVER "foo.bar"
    ...
    doSomething(SER VER);

    .... you could insert `#undef SERVER' and `#define SERVER "localhost" '
    in between them -- equally dirty, just syntactically different.

    But all these are one-offs, hacks, and kludges. It seems to
    me that things like server addresses should usually not be hard-
    compiled into programs, but should be read from an input or taken
    from an environment variable or something of that sort. Would you
    want to use a Web browser whose proxy settings were factory-installed
    and unchangeable?

    --
    Eric.Sosman@sun .com

    Comment

    • Default User

      #3
      Re: Preprocessor macro substitution

      Eric Sosman wrote:
      Default User wrote:
      I've never really done too much stuff with macros. I have a program
      here where an target server address is automatically generated from
      WSDL. For testing, I want to use a localhost address.

      I could do it by overriding the endpoint explicitly, but I'd rather
      not. It would be cleaner to define a local testing environment.

      My first thought was just to use a #define, like:

      #define foo.bar localhost

      However, that's no good as foo.bar tain't a legal identifier with
      that '.' in it. Anyone have some clever preprocessor way to get
      what I want?
      >
      What you haven't told us is where you're starting: How do
      you get hold of the server address to begin with? "From WSDL"
      doesn't seem to me a very complete description ...
      I didn't want to get too deeply into the details, but evidently I was
      insufficiently thorough. The service address is specified in the WSDL.
      A utility processes this and generates some C headers and source files.
      Included in some places is the "endpoint" or target address. If you
      don't provide an overriding address, then this is the one that is used.
      So there will things like this:

      if (!soap_endpoint )
      soap_endpoint = "http://foo.bar:8080"

      As it is generated code, I can't change that. I can provide an
      endpoint, but that's fragile, as it then requires a manual update if
      the WSDL is changed.

      I can also do something like:

      soap_endpoint = END_POINT;

      Then for testing purposes:

      #ifdef TESTING
      #define END_POINT "http://localhost:8080"
      #else
      #define END_POINT NULL
      #end



      It's not as clean, but would work.



      Brian

      Comment

      • Gordon Burditt

        #4
        Re: Preprocessor macro substitution

        >I've never really done too much stuff with macros. I have a program
        >here where an target server address is automatically generated from
        >WSDL. For testing, I want to use a localhost address.
        Is WSDL the double-layer version of the Weasel Server?

        Host addresses are generally supplied as strings (DNS names or text
        IP addresses). You could do something like:

        #ifdef TEST
        #define SERVER "localhost"
        #else
        #define SERVER "weasel.server. gov"
        #endif

        Then you pass SERVER to some function that looks up a name and
        connects to it.
        >I could do it by overriding the endpoint explicitly, but I'd rather
        >not. It would be cleaner to define a local testing environment.
        It is also possible with many compilers to put macro definitions
        on the compiler command line and let a build script or Makefile
        supply the value. I don't know whether that will help in getting
        the server address from WSDL (whatever that is) to the C program.
        >My first thought was just to use a #define, like:
        >
        >#define foo.bar localhost
        A #define does not substitute something that is in double quotes.
        foo.bar is unlikely to be both a valid host name and a valid
        expression unless you contrived a structure declaration and variable
        name to make it work.
        >However, that's no good as foo.bar tain't a legal identifier with that
        >'.' in it. Anyone have some clever preprocessor way to get what I want?

        Comment

        Working...