Re: Comparing RECORD's
Posted in 1994
In article <CrDCow.E4D@candle.pha.pa.us> root@candle.pha.pa.us (Bruce Momjian) writes: >If I have two structures with idenical fields, is there an easy way to >see if the values in each of the fields is the same, without comparing >each and every field (with NULL checks too)? > >I need this because I am using a INPUT ARRAY statement, but want to >record the date each row changes. I am going to store the record in a >BEFORE ROW statement, and compare in an AFTER ROW statement. > >Is there a better way to do this? > >-- >Bruce Momjian | 830 Blythe Avenue >root@candle.pha.pa.us | Drexel Hill, Pennsylvania 19026 > + If your life is a hard drive, | (610) 353-9879(w) > + Christ can be your backup. | (610) 853-3000(h) I wondered what a 4GL record comparison function might look like using only published interfaces. Here is one possibility I put together very quickly. (so be gentle :) Scott (Rom10:9) /*------------------------------------------------------------------------------ * * FUNCTION: fgl_reccmp.c * * AUTHOR: C. S. Ellard * * PURPOSE: compare two 4gl records for equality. * * USAGE: LET N = fgl_reccmp(rec1.*, rec2.*) * * N == 0, records compare equal * N != 0, records compare not equal at field number N * NOTES: * * 1. Nulls compare equal. * 2. Trailing spaces are not significant. * 3. Assumes Ifmx string conversions create equal strings for numerics. * 4. BLOBS aren't handled. * *------------------------------------------------------------------------------ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXARGSIZE (512) /* adjust to largest string expected */ int fgl_reccmp(nargs) int nargs; { static int argmax; static char* *argvec; int i; if ((nargs % 2) != 0) { (void) fprintf(stderr, "fgl_reccmp: EINVAL = %d\\n", nargs); exit(1); } if (nargs > argmax) /* need to create/grow argvec */ { size_t size = nargs * sizeof(char**); if (argvec == 0) argvec = (char**) malloc(size); else argvec = (char**) realloc((char*) argvec, size); if (argvec == 0) { (void) fprintf(stderr, "fgl_reccmp: ENOMEM\\n"); exit(1); } for (i = argmax; i < nargs; ++i) argvec[i] = 0; argmax = nargs; } for (i = nargs-1; i >= 0; --i) { char buff[MAXARGSIZE+1]; /* +1 for the NULL */ char *p; popquote(buff, sizeof(buff)); /* and trim trailing spaces */ for (p = buff + (sizeof(buff)-2); p >= buff && *p == ' '; --p) ; *(p+1) = 0; if (argvec[i] != 0) free(argvec[i]); argvec[i] = (*buff ? strdup(buff) : ""); if (argvec[i] == 0) { (void) fprintf(stderr, "fgl_reccmp: ENOMEM\\n"); exit(1); } } for (nargs /= 2, i = 0; i < nargs; ++i) { if (strcmp(argvec[i], argvec[i+nargs]) != 0) { pushint(++i); /* adjust from origin 0 to origin 1 */ return(1); } } pushint(0); return(1); }