comma operator
John Plocher
plocher%sally at Sun.COM
Tue Aug 1 17:18:04 AEST 1989
In <918 at helios.toronto.edu> dooley at helios.physics.utoronto.ca (Kevin Dooley) writes:
>Can anybody tell me if there is a usage where the comma is 'better'.
The only place I ever had a real use for the comma operator was when working with
a 3rd party C-ISAM library which had many functions and a large set of error returns.
Call the lib functions copen(), cclose(), cinsert(), cfind()...
These routines would return a value X, where on success X > 0 and was then
used as a magic cookie. If X < 0 then there were 15 or so error conditions
that could have happened - all but 4 or 5 because of stupid programmer errors
(The routines used structures that HAD to be initialized *just so* &$&%#$%# ).
Output of all this is like:
Trace: copen(structure, FLAG|FLAG, "Filename", blah) => DB_NOTFOUND (File not found)
Trace: ccreate(structure,...) => DB_OK
I actually used __LINE__ and __FILE__ in the ERROR() macro so the output was
more useful still.
-John Plocher
------------------------------------------------------------------------------------
#ifdef DEBUG
int ERROR_CC;
log(funct, string)
char *funct, *string;
{
fprintf(stderr,"Trace: %s => %s\n", funct,string);
}
char *errorname( error_cc )
int error_cc;
{
if (error_cc > 0) return itoa(error_cc);
else if (error_cc == DB_OK) return "DB_OK";
else if (error_cc == DB_NOTFOUND) return "DB_NOTFOUND (File not found)";
else if (error_cc == DB_BADSTRUCT)return "DB_BADSTRUCT (Invalid DB Struct)";
else if ...
}
/* Relies on NON-ANSI C Preprocessor behavior ! */
#define ERROR(funct) (log("funct", errorname(ERROR_CC=funct)), ERROR_CC)
/*
** Call funct() and assign the ret val to ERROR_CC
** Send this value to errorname() so we get an english description of the error
** Send the (string representation) of the function and this english to log()
** Finally, give the poor user the ret value we saved in ERROR_CC
*/
#else /* not DEBUG */
#define ERROR(funct) funct
#endif /* if DEBUG else endif */
main() {
int dbfd;
init( &structure );
dbfd = ERROR(copen(structure, FLAG|FLAG, "Filename", blah));
if (dbfd == DB_NOTFOUND)
dbfd = ERROR(ccreate(structure,...));
if (dbfd < 0)
bomb();
}
More information about the Comp.lang.c
mailing list