bergquis@nms.gdc.portal.com (Brett Bergquist) (04/22/91)
Is there any way to expand an argument to a macro and at the same time surround it with quotes (making is a quoted string). For example if STRING is defined as: #define STRING test string Is there any way to define a macro "QUOTE" so that: char* s = QUOTE(STRING); while produce: char *s = "test string"; The reason that I need this is that the *%$# ESQL preprocessor that I am using needs table names unquoted, by my C applications need them quoted. I'm using SunOS 4.1.1's CPP. Thanks in advance. -- Brett M. Bergquist, Principal Engineer | "Remind me" ... "to write an General DataComm, Inc., | "article on the compulsive reading Middlebury, CT 06762 | of news." - Stranger in a Strange Land Email: bergquis@nms.gdc.portal.com or ...!portal!gdc!nms!bergquis
jhz@cunixa.cc.columbia.edu (Jennifer H. Zemsky) (04/22/91)
In article <230@wimpy.nms.gdc.portal.com> bergquis@nms.gdc.portal.com (Brett Bergquist) writes: >Is there any way to expand an argument to a macro and at the same time >surround it with quotes (making is a quoted string). For example if >STRING is defined as: > >#define STRING test string > >Is there any way to define a macro "QUOTE" so that: > >char* s = QUOTE(STRING); > >while produce: > >char *s = "test string"; > from k&r (2nd ed), 4.11.2: if ... a parameter is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replace by the actual argument. This can be combined with string concatenation to make, for example, a debugging print macro: #define dprint(expr) printf(#expr " = %g\n",expr) When this is invoked, as in dprint (x/y); the macro is expanded into printf("x/y" " = %g\n", x/y); and the strings are concatenated ... printf("x/y = %g\n",x/y); (end quote) you could try #define QUOTE(x) #x and see if it works. (note: the extra information was to head off some other questions.) >Thanks in advance. no prob. --hymie jhz@cunixa.cc.columbia.edu ------------------------------------------------------------------------------- note: the above information, knowledge, etc. is offered only on an as-is basis. the author takes no responsibility for innacuracy or for problems caused by implementing said information. veracity is not guaranteed. corrections welcome. -------------------------------------------------------------------------------
volpe@camelback.crd.ge.com (Christopher R Volpe) (04/22/91)
In article <1991Apr22.014237.16836@cunixf.cc.columbia.edu>, jhz@cunixa.cc.columbia.edu (Jennifer H. Zemsky) writes: |>In article <230@wimpy.nms.gdc.portal.com> bergquis@nms.gdc.portal.com (Brett Bergquist) writes: |>>Is there any way to expand an argument to a macro and at the same time |>>surround it with quotes (making is a quoted string). For example if |>>STRING is defined as: |>> |>>#define STRING test string |>> |>>Is there any way to define a macro "QUOTE" so that: |>> |>>char* s = QUOTE(STRING); |>> |>>while produce: |>> |>>char *s = "test string"; |>> |>you could try |>#define QUOTE(x) #x |> Won't work. Stringizing takes place on the unexpanded argument. Using your QUOTE, char *s = QUOTE(STRING); becomes char *s = "STRING"; which is not what he wanted. You must define an auxialliary macro which allows expansion to occur before stringizing. #define QUOTE(x) QUOTE_AUX(x) #define QUOTE_AUX(x) #x Now, QUOTE(STRING) produces "test string" -Chris ================== Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com
parks@compass.com (John Parks) (04/23/91)
Brett Bergquist writes:
Is there any way to expand an argument to a macro and at the same time
surround it with quotes (making is a quoted string).
Interesting question! If you have an ANSI CPP, it will take two macros
(here given as STR and QUOTE).
#define greeting hello Jennifer
#define STR(x) #x
#define QUOTE(x) STR(x)
QUOTE(greeting) will expand to "hello Jennifer"
The ANSI stringization operator stringizes an "unexpanded" actual parameter
list, hence a two-step method is needed to force the expansion.
--
John Parks
Compass Inc.
550 Edgewater Drive
Wakefield, MA 01880
ddddddd
sarima@tdatirv.UUCP (Stanley Friesen) (04/24/91)
In article <1991Apr22.014237.16836@cunixf.cc.columbia.edu> jhz@cunixa.cc.columbia.edu (Jennifer H. Zemsky) writes: <In article <230@wimpy.nms.gdc.portal.com> bergquis@nms.gdc.portal.com (Brett Bergquist) writes: <>Is there any way to define a macro "QUOTE" so that: <> <>char* s = QUOTE(STRING); <> <>will produce: <> <>char *s = "test string"; <from k&r (2nd ed), 4.11.2: <if ... a parameter is preceded by a # in the replacement text, the combination <will be expanded into a quoted string with the parameter replace by the actual <argument. This can be combined with string concatenation to make, for <example, a debugging print macro: < <#define dprint(expr) printf(#expr " = %g\n",expr) <When this is invoked, as in Except that Mr Bergquist explicitly stated he was using the SuOS 4.1 C compiler, which is *not*, I repeat, *not*, ANSI conformant. It does not support this ANSI-ism. (Sun has 'graciously' provided a seperate, unbundled ANSI compliant compiler). So folks, which way does Sun C blow? Which of the non-portable, pre-ANSI methods actually works with this compiler? it it: #define QUOTE(string) "string" or is it somehing else? -- --------------- uunet!tdatirv!sarima (Stanley Friesen)
bergquis@nms.gdc.portal.com (Brett Bergquist) (04/24/91)
Thanks to all that have responded. I only have the non-ANSI CPP so the stringize operator method will not work for me. I cannot seem to get the #define expanded before handing it to the QUOTE macro so I took the hard way out and stopped being lazy and defined two #defines (one without the quotes, and one with the quotes, ie. #define STRING this is the unqouoted string #define STRING_Q "this is the quoted string". -- Brett M. Bergquist, Principal Engineer | "Remind me" ... "to write an General DataComm, Inc., | "article on the compulsive reading Middlebury, CT 06762 | of news." - Stranger in a Strange Land Email: bergquis@nms.gdc.portal.com or ...!portal!gdc!nms!bergquis
dave@aspect.UUCP (Dave Corcoran) (04/30/91)
In article <230@wimpy.nms.gdc.portal.com>, bergquis@nms.gdc.portal.com (Brett Bergquist) writes: > Is there any way to expand an argument to a macro and at the same time > surround it with quotes (making is a quoted string). So I've got this fetish for m4 solutions! --------------------------------8<-------------------------------- define(string,`QUOTE_ME') define(quote_,`"$1"') char *q=quote_(string) char *s=quote_(Hey there hi there ho there) --------------------------------8<-------------------------------- -- David Corcoran -@@ uunet!aspect!dave ~ In a society where anything goes eventually everything will.
stachour@sctc.com (Paul Stachour) (04/30/91)
>In article <230@wimpy.nms.gdc.portal.com>, bergquis@nms.gdc.portal.com (Brett Bergquist) writes: >> Is there any way to expand an argument to a macro and at the same time >> surround it with quotes (making is a quoted string). I've had the same problem. I've not been able to solve it. I was recently sent a solution by Tom Plum, which follows. The explanation which follows the example is mine. The example works, the explanation why might be faulty. ----- Example ---- #define OPEN_OBJECT 12 #define NXSTR(x) #x /* no-expansion stringize */ #define STR(x) NXSTR(x) /* stringize after expansion */ asm(" move.l #" STR(OPEN_OBJECT) ",D0"),4 ); ----- Explanation ---- Aha! I see it now. When you do the two-level evaluation in the style: #define OPEN_OBJECT 12 #define NXSTR(x) #x /* no-expansion stringize */ #define STR(x) NXSTR(x) /* stringize after expansion */ asm(" move.l #" STR(OPEN_OBJECT) ",D0",4 ); the result is that the original text of: asm(" move.l #" STR(OPEN_OBJECT) ",D0",4 ); gets replaced after one level of evaluation with: asm(" move.l #" NXSTR(12) ",D0",4 ); which is then re-evaluated to give: asm(" move.l #" "12" ",D0",4 ); which the c-compile then treats as: asm(" move.l #12,D0",4 ); and passes the assembler: " move.l #12,D0" and that is what I want! ...Paul -- Paul Stachour SCTC, 1210 W. County Rd E, Suite 100 stachour@sctc.com Arden Hills, MN 55112 [1]-(612) 482-7467
bergquis@nms.gdc.portal.com (Brett Bergquist) (05/02/91)
In response to a question of how to surround a macro argument with quotes even if the argument is itself a macro, using the non-ANSI CPP that comes with SunOS 4.1, I received the following response: ]From gdc!uzlwg!zellweger.ch!eber Mon Apr 29 11:57:46 1991 ]From: eber@zellweger.ch (Urs Eberle) ]To: nms!bergquis ]Subject: Re: Need help with quoting and the CPP ] ]Hi Brett ] ]Your problem is easy to solve: do ]#define QUOTE(x) "\ ]x\ ]" ] ]That's all. Greetings from switzerland... ]========================================================================== ]Urs Eberle "La mejor defensa contra la logica ]P.O Box 29 es la ignorancia..." ]CH-8488 Turbenthal ]Switzerland ] ]Tel: ++41 19433514 ] ]e-mail: eber@zellweger.ch ]========================================================================== -- Brett M. Bergquist, Principal Engineer | "Remind me" ... "to write an General DataComm, Inc., | "article on the compulsive reading Middlebury, CT 06762 | of news." - Stranger in a Strange Land Email: bergquis@nms.gdc.portal.com or ...!portal!gdc!nms!bergquis