H NOMAIN DEBUG(*YES) *T: Generic sort/search *O: CRTRPGMOD *O: DBGVIEW(*ALL) OPTIMIZE(*NONE) DEFINE(@SORT) * * SYNOPSIS: This module contains the generic sort/search procedure * @sort(). The variables in the @sort_control structure * (defined in the @SORT_P copybook) are exported from this * module. * * The @sort() procedure is invoked through either qsort() * or bsearch() - see the documentation for those procedures * for how to do this. * *===================================================================== D/COPY QRPGLESRC,@SORT_P D LOWER C 'abcdefghijklmnopqrstuvwxyz' D UPPER C 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' *===================================================================== P @sort B Export D PI 10I 0 D Elem1@ * Value D Elem2@ * Value *--------------------------------------------------------------------- D Elem1 S 32767A Based(Elem1@) D Elem2 S 32767A Based(Elem2@) D Comp1 S 32767A Inz D Comp2 S 32767A Inz *--------------------------------------------------------------------- * * Don't bother doing processing if an error has already occurred. * C If @sort_control.error > 0 C Exsr *PSSR C Endif * * First-time-round processing * C If @sort_control.count = 0 * Check order C If @sort_control.order <> 1 and C @sort_control.order <> -1 C Eval @sort_control.error = ESORTORDER C Exsr *PSSR C Endif * Check start C If @sort_control.start <= 0 or C @sort_control.start >= %size( Elem1 ) C Eval @sort_control.error = ESORTSTART C Exsr *PSSR C Endif * Check length C If @sort_control.length <= 0 or C @sort_control.length >= %size( Elem1 ) C Eval @sort_control.error = ESORTLENGTH C Exsr *PSSR C Endif * Check igncas C If @sort_control.igncas <> 0 and C @sort_control.igncas <> 1 C Eval @sort_control.error = ESORTIGNCAS C Exsr *PSSR C Endif * C Endif * * Increment the count by one * C Eval @sort_control.count = @sort_control.count + 1 * * Parse out the comparison data from each element * C Eval Comp1 = %subst( Elem1 : C @sort_control.start : C @sort_control.length ) C Eval Comp2 = %subst( Elem2 : C @sort_control.start : C @sort_control.length ) * * Convert to upper case if necessary * C If @sort_control.igncas = 1 C LOWER:UPPER Xlate Comp1 Comp1 C LOWER:UPPER Xlate Comp2 Comp2 C Endif * * Return the correct value depending on whether we are doing an * ascending or a descending search. * C Select C When Comp1 < Comp2 C Return -@sort_control.order C When Comp1 > Comp2 C Return @sort_control.order C Other C Return 0 C Endsl * C *PSSR Begsr C If @sort_control.error = 0 C Eval @sort_control.error = ESORTERROR C Endif C Return 0 C Endsr * P E *===================================================================== * inz_@sort(): Initialize the @sort_control structure. *===================================================================== P inz_@sort B Export D PI 10I 0 D P_Start 10I 0 Value D P_Length 10I 0 Value *--------------------------------------------------------------------- * C Clear @sort_control C Eval @sort_control.start = P_Start C Eval @sort_control.length = P_Length C Eval @sort_control.count = 0 C Eval @sort_control.order = 1 * C Return 0 * C *PSSR Begsr C Return -1 C Endsr * P E