Re: Comparing RECORD's
Posted in 1994
In article <CrJK72.CAy@wetware.com> cse@wetware.com (Scott Ellard) writes: >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) It's funny how the light of a new day affects your perspective. Please forgive the wasted bandwidth from my previous post. Here is a revised version of the code I posted last night. Scott /*------------------------------------------------------------------------------ * * 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; { char* *argvec; int i; if ((nargs <= 0) || ((nargs % 2) != 0)) { (void) fprintf(stderr, "fgl_reccmp: EINVAL = %d\\n", nargs); exit(1); } if ((argvec = (char**) malloc(nargs * sizeof(char**))) == 0) { (void) fprintf(stderr, "fgl_reccmp: ENOMEM\\n"); exit(1); } for (i = nargs-1; i >= 0; --i) { char buff[MAXARGSIZE+1]; /* +1 for the NULL */ char *p; popquote(buff, sizeof(buff)); if (*buff == 0) { argvec[i] = ""; continue; } /* trim trailing spaces */ for (p = buff + (sizeof(buff)-2); p >= buff && *p == ' '; --p) ; *(p+1) = 0; if ((argvec[i] = strdup(buff)) == 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) break; } retint((i == nargs) ? 0 : ++i); /* adjust from origin 0 to origin 1 */ for (nargs *= 2, i = 0; i < nargs; ++i) { if (*argvec[i] != 0) free(argvec[i]); } free((char*)argvec); return(1); }