Converting to ANSI C++ : Problem
Posted in 2000
Rudy asked how to convert old K&R-style ESQL/C function definitions (with the PARAMETER host-variable DECLARE SECTION after the function header) to ANSI C/C++ prototype style, since after moving to prototypes the parameters were no longer usable as host variables (:placeholders). Answers: the documented ESQL/C syntax puts the whole EXEC SQL BEGIN/END DECLARE SECTION with PARAMETER declarations inside the function's parentheses (Art Kagel, Tino; note each variable on its own line if using C++ default values). Jonathan Leffler preferred a cleaner alternative: use a normal prototype and copy the arguments into host variables declared in a DECLARE SECTION at the top of the body, so code parsers still understand the function. Rudy confirmed the in-parentheses form worked under ESQL 9.30 (but not 7.23 on HP 10.20). Pointers to esql++ for C++ compilation were also given.
Auto-generated by DrWatson from the posts below — may be imperfect; read the full thread.
Topics: General Discussion
Guys, We are in the process of converting our application to ANSI C++. I'm having difficulty in figuring out how to convert code related to the function parameters, where parameters are going to be used as Informix placeholders. Specifically, ANSI C++ expects the variable definition to be within the parentheses following the function name. So, if I had a function int junk (foo) int foo; { return (foo++); } I would make it ANSI C++ compliant by converting it to int junk (int foo) { return (foo++) } How would I convert the following code to be ANSI C++ compliant? long getrow(sysdesc) EXEC SQL BEGIN DECLARE SECTION; PARAMETER char *sysdesc; EXEC SQL END DECLARE SECTION; { long exp_chk(); sprintf(statement, "FETCH %s", sysdesc); EXEC SQL fetch sel_curs using sql descriptor :sysdesc; return((exp_chk(statement)) == 100 ? 0 : 1); } Any pointers welcome. Rudy
rferdy@americasm01.nt.com (Rudy Fernandes) wrote in <39A57721.EC912CB5@americasm01.nt.com>: >We are in the process of converting our application to ANSI C++. I'm >having difficulty in figuring out how to convert code related to the >function parameters, where parameters are going to be used as Informix >placeholders. Specifically, ANSI C++ expects the variable definition to >be within the parentheses following the function name. As you no doubt have noticed, ESQL/C has problems with C++. You have three options. You may be able to coerce your version of ESQL to compile C++ code by naming the files *.ecpp. For the caveats associated with that, see #2. Second, you can give Johnathon Leffler's esql++ script a try (see http://www.iiug.org/members/memb_software/archive/esql_c++) [caveat: I have not personally tried it.] He also has further information there about what versions of ESQL/C may or may not compile C++ code. The last option is to just retain the existing ESQL/C code, continue compiling it with esql and a C compiler, and call C accessor functions from your C++ member functions. -- William Harris william@carsinfo.com
william@carsinfo.com (William Harris) wrote in <8o40ac$8bl$1@malgudi.oar.net>: >Second, you can give Johnathon Leffler's esql++ script a try (see >http://www.iiug.org/members/memb_software/archive/esql_c++) [caveat: I >have not personally tried it.] He also has further information there >about what versions of ESQL/C may or may not compile C++ code. Whoops, his name is Jonathan, not Johnathon. A case of fingers typing outrunning my brain, especially when I had the esql_c++ page up and was staring right at his name. :-) -- William Harris william@carsinfo.com
William Harris wrote: > rferdy@americasm01.nt.com (Rudy Fernandes) wrote in > <39A57721.EC912CB5@americasm01.nt.com>: > > >We are in the process of converting our application to ANSI C++. I'm > >having difficulty in figuring out how to convert code related to the > >function parameters, where parameters are going to be used as Informix > >placeholders. Specifically, ANSI C++ expects the variable definition to > >be within the parentheses following the function name. > > As you no doubt have noticed, ESQL/C has problems with C++. Actually, my problem appears to be more with ANSI than with C++. We have been compiling with the C++ compiler for some time. But, switching to ANSI has changed the way arguments are defined. Without ANSI, the following works, long getrow(sysdesc) EXEC SQL BEGIN DECLARE SECTION; PARAMETER char *sysdesc; EXEC SQL END DECLARE SECTION; { ... } Under ANSI, sysdesc has to be defined as follows long getrow (char * sysdesc) { ... } The trouble is, ESQL no longer allows sysdesc to be used as an Informix placeholder. Rudy
Thanks a ton, Tino. This is exactly what I need. Rudy BTW, HP10.20's esql v7.23 did not support it, but (thankfully) v9.30 did. -----Original Message----- Rudy, Page 1-30 of the ESQL/C manual has this construct: int * foo ( EXEC SQL BEGIN DECLARE SECTION; PARAMETER char s[20], PARAMETER int id, PARAMETER int (*f)(double) EXEC SQL END DECLARE SECTION; ) { EXEC SQL SELECT fname into :s FROM customer WHERE customer_num = :id; } I have never used it, it looks very awkward to me, but it might solve your problem. HTH Tino
See below (from ESQLC Programmers Manual ~p 1-29): Rudy Fernandes wrote: > > Guys, > > We are in the process of converting our application to ANSI C++. I'm > having difficulty in figuring out how to convert code related to the > function parameters, where parameters are going to be used as Informix > placeholders. Specifically, ANSI C++ expects the variable definition to > be within the parentheses following the function name. > > So, if I had a function > > int junk (foo) > int foo; > { > return (foo++); > } > > I would make it ANSI C++ compliant by converting it to > > int junk (int foo) > { > return (foo++) > } > > How would I convert the following code to be ANSI C++ compliant? > Just split the opening and closing parentheses of the function onto separate lines with the complete EXEC SQL DECLARE section between. long getrow( EXEC SQL BEGIN DECLARE SECTION; PARAMETER char *sysdesc; EXEC SQL END DECLARE SECTION; ) { long exp_chk(); sprintf(statement, "FETCH %s", sysdesc); EXEC SQL fetch sel_curs using sql descriptor :sysdesc; return((exp_chk(statement)) == 100 ? 0 : 1); } Just be careful, if you are using C++ default values each host variable declaration must be on a separate line even if the datatypes are the same because ESQL/C stops parsing a line after finding an initializer in a declaration! Art S. Kagel > Any pointers welcome. > Rudy
Rudy Fernandes wrote: > Thanks a ton, Tino. This is exactly what I need. > -----Original Message----- > Page 1-30 of the ESQL/C manual has this construct: > > int * foo ( EXEC SQL BEGIN DECLARE SECTION; > PARAMETER char s[20], > PARAMETER int id, > PARAMETER int (*f)(double) > EXEC SQL END DECLARE SECTION; > ) > { > EXEC SQL SELECT fname into :s FROM customer > WHERE customer_num = :id; > > } > > I have never used it, it looks very awkward > to me, but it might solve your problem. You've seen the official answer (twice). I find that syntax too loathsome to consider using it in my own code, so what I do is: int *foo(char *p_s, int p_id) { EXEC SQL BEGIN DECLARE SECTION; char *s = p_s; char *id = p_id; EXEC SQL END DECLARE SECTION; ... It has its own inelegancies, but at least it leaves the syntax of the function definition in a form where code parsers (cscope, or any GUI IDE system) can both read and understand it. If they are strict, most of them will barf on the stuff with EXEC SQL in the actual parameter list. -- Yours, Jonathan Leffler (Jonathan.Leffler@Informix.com) #include <disclaimer.h> Guardian of DBD::Informix v1.00.PC1 -- http://www.perl.com/CPAN "I don't suffer from insanity; I enjoy every minute of it!"