home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / COMPARE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-07-25  |  13.5 KB  |  347 lines

  1. (* TBTree13             Copyright (c)  1988            Dean H. Farwell II    *)
  2.  
  3. unit Compare;
  4.  
  5. (*****************************************************************************)
  6. (*                                                                           *)
  7. (*             D A T A   C O M P A R I S O N   R O U T I N E S               *)
  8. (*                                                                           *)
  9. (*****************************************************************************)
  10.  
  11. (* This unit contains two routines which will compare two values and
  12.    determine whether the first value is LESTHAN, EQUALTO, or GREATERTHAN the
  13.    second value.  The following defined Turbo Pascal types are supported:
  14.  
  15.                    Byte
  16.                    ShortInt
  17.                    Integer
  18.                    LongInt
  19.                    Word
  20.                    String (any sizes)
  21.                    Real
  22.                    Single
  23.                    Double
  24.                    Extended
  25.                    Comp
  26.  
  27.    Note - To use the last four above (8087 types) you must remove the brackets
  28.    found below in the code.  I commented these out to allow compilation on a
  29.    non 8087 machine.  After removing the brackets recompile!!
  30.  
  31.    This unit also contains three routines for determining if a substring
  32.    starts a target string, ends a target string, or is contained in a target
  33.    string.  These routines are placed in this unit, because the strings are
  34.    passed in as untyped parameters just like in the first two routines in
  35.    this unit.                                                                *)
  36.  
  37. (* Version Information
  38.  
  39.    Version 1.1 - Added SubstringCompare routine
  40.  
  41.                - Added ContainsSubstring routine
  42.  
  43.                - Added StartsWithSubstring routine
  44.  
  45.                - Added EndsWithSubstring routine
  46.  
  47.    Version 1.2 - No Changes
  48.  
  49.    Version 1.3 - No Changes                                                  *)
  50.  
  51.  
  52. (*\*)
  53. (*////////////////////////// I N T E R F A C E //////////////////////////////*)
  54.  
  55. interface
  56.  
  57. type
  58.     Comparison = (LESSTHAN,EQUALTO,GREATERTHAN);
  59.  
  60.     ValueType = (INVALIDVALUE,
  61.                  BYTEVALUE,
  62.                  SHORTINTVALUE,
  63.                  INTEGERVALUE,
  64.                  LONGINTVALUE,
  65.                  WORDVALUE,
  66.                  STRINGVALUE,
  67.                  REALVALUE,
  68.                  SINGLEVALUE,
  69.                  DOUBLEVALUE,
  70.                  EXTENDEDVALUE,
  71.                  COMPVALUE);
  72.  
  73. (*\*)
  74. (* This routine will compare two values and return the result of the comparison.
  75.    The result is of type Comparison and LESSTHAN, EQUALTO, or GREATERTHAN will
  76.    be returned.  The values compared must be of the same type.  Legal types are
  77.    those enumerated in the type ValueType.  The type of the values is passed in
  78.    as a parameter along with the values.
  79.  
  80.    note : the values must reside in a variable since a var parameter is used.
  81.    This is necessary since the address is needed to facilitate the use of this
  82.    routine with multiple types.                                              *)
  83.  
  84. function CompareValues(var paramValue1;
  85.                        var paramValue2;
  86.                        vType : ValueType) : Comparison;
  87.  
  88.  
  89. (* This routine will compare two values of type STRINGVALUE and look for a
  90.    partial match.  The first parameter (paramValue1) contains a substring which
  91.    will be searched for in paramValue2.  The search is only to see if
  92.    paramValue2 starts with substring paramValue1.  If paramValue2 starts with
  93.    paramValue1 then EQUALTO will be returned.  Otherwise if paramValue1 is
  94.    less that paramValue2 then LESSTHAN will be returned.  If paramValue1 is
  95.    greater that paramValue2 then GREATERTHAN will be returned.               *)
  96.  
  97. function SubstringCompare(var paramValue1;
  98.                           var paramValue2) : Comparison;
  99.  
  100.  
  101. (* This routine will check to see if the substring passed in as paramValue1
  102.    is contained in the string passed in as paramValue2.  It will return TRUE
  103.    if paramValue1 is contained in paramValue2 and FALSE otherwise.           *)
  104.  
  105. function ContainsSubstring(var paramValue1;
  106.                            var paramValue2) : Boolean;
  107.  
  108.  
  109. (* This routine will check to see if the substring passed in as paramValue1
  110.   starts the string passed in as paramValue2.  It will return TRUE if
  111.   paramValue1 starts paramValue2 and FALSE otherwise.                        *)
  112.  
  113. function StartsWithSubstring(var paramValue1;
  114.                              var paramValue2) : Boolean;
  115.  
  116.  
  117. (* This routine will check to see if the substring passed in as paramValue1
  118.   ends the string passed in as paramValue2.  It will return TRUE if
  119.   paramValue1 ends paramValue2 and FALSE otherwise.                          *)
  120.  
  121. function EndsWithSubstring(var paramValue1;
  122.                            var paramValue2) : Boolean;
  123.  
  124. (*\*)
  125. (*///////////////////// I M P L E M E N T A T I O N /////////////////////////*)
  126.  
  127. implementation
  128.  
  129. (* This routine will compare two values and return the result of the comparison.
  130.    The result is of type Comparison and LESSTHAN, EQUALTO, or GREATERTHAN will
  131.    be returned.  The values compared must be of the same type.  Legal types are
  132.    those enumerated in the type ValueType.  The type of the values is passed in
  133.    as a parameter along with the values.
  134.  
  135.    note : the values must reside in a variable since a var parameter is used.
  136.    This is necessary since the address is needed to facilitate the use of this
  137.    routine with multiple types.                                              *)
  138.  
  139. function CompareValues(var paramValue1;
  140.                        var paramValue2;
  141.                        vType : ValueType) : Comparison;
  142.  
  143. var
  144.     byteValue1        : Byte     absolute paramValue1;
  145.     byteValue2        : Byte     absolute paramValue2;
  146.     shortIntValue1    : ShortInt absolute paramValue1;
  147.     shortIntValue2    : ShortInt absolute paramValue2;
  148.     integerValue1     : Integer  absolute paramValue1;
  149.     integerValue2     : Integer  absolute paramValue2;
  150.     longIntValue1     : LongInt  absolute paramValue1;
  151.     longIntValue2     : LongInt  absolute paramValue2;
  152.     wordValue1        : Word     absolute paramValue1;
  153.     wordValue2        : Word     absolute paramValue2;
  154.     stringValue1      : String   absolute paramValue1;
  155.     stringValue2      : String   absolute paramValue2;
  156.     realValue1        : Real     absolute paramValue1;
  157.     realValue2        : Real     absolute paramValue2;
  158.     singleValue1      : Single   absolute paramValue1;
  159.     singleValue2      : Single   absolute paramValue2;
  160.     doubleValue1      : Double   absolute paramValue1;
  161.     doubleValue2      : Double   absolute paramValue2;
  162.     extendedValue1    : Extended absolute paramValue1;
  163.     extendedValue2    : Extended absolute paramValue2;
  164.     compValue1        : Comp     absolute paramValue1;
  165.     compValue2        : Comp     absolute paramValue2;
  166.  
  167.     begin
  168.     case vType of
  169.         BYTEVALUE :
  170.             begin
  171.             if byteValue1 < byteValue2 then CompareValues := LESSTHAN
  172.             else if byteValue1 = byteValue2 then CompareValues := EQUALTO
  173.             else CompareValues := GREATERTHAN;
  174.             end;
  175.         SHORTINTVALUE :
  176.             begin
  177.             if shortIntValue1 < shortIntValue2 then CompareValues := LESSTHAN
  178.             else if shortIntValue1 = shortIntValue2 then CompareValues :=EQUALTO
  179.             else CompareValues := GREATERTHAN;
  180.             end;
  181.         INTEGERVALUE :
  182.             begin
  183.             if integerValue1 < integerValue2 then CompareValues := LESSTHAN
  184.             else if integerValue1 = integerValue2 then CompareValues := EQUALTO
  185.             else CompareValues := GREATERTHAN;
  186.             end;
  187.         LONGINTVALUE :
  188.             begin
  189.             if longIntValue1 < longIntValue2 then CompareValues := LESSTHAN
  190.             else if longIntValue1 = longIntValue2 then CompareValues := EQUALTO
  191.             else CompareValues := GREATERTHAN;
  192.             end;
  193.         WORDVALUE :
  194.             begin
  195.             if wordValue1 < wordValue2 then CompareValues := LESSTHAN
  196.             else if wordValue1 = wordValue2 then CompareValues := EQUALTO
  197.             else CompareValues := GREATERTHAN;
  198.             end;
  199.         STRINGVALUE:
  200.             begin
  201.             if stringValue1 < stringValue2 then CompareValues := LESSTHAN
  202.             else if stringValue1 = stringValue2 then CompareValues := EQUALTO
  203.             else CompareValues := GREATERTHAN;
  204.             end;
  205.         REALVALUE :
  206.             begin
  207.             if realValue1 < realValue2 then CompareValues := LESSTHAN
  208.             else if realValue1 = realValue2 then CompareValues := EQUALTO
  209.             else CompareValues := GREATERTHAN;
  210.             end;
  211. {   The following types are only for 8087 - and are presently commented out
  212.     To use -- remove brackets and recompile --
  213.  
  214.         SINGLEVALUE :
  215.             begin
  216.             if singleValue1 < singleValue2 then CompareValues := LESSTHAN
  217.             else if singleValue1 = singleValue2 then CompareValues := EQUALTO
  218.             else CompareValues := GREATERTHAN;
  219.             end;
  220.         DOUBLEVALUE :
  221.             begin
  222.             if doubleValue1 < doubleValue2 then CompareValues := LESSTHAN
  223.             else if doubleValue1 = doubleValue2 then CompareValues := EQUALTO
  224.             else CompareValues := GREATERTHAN;
  225.             end;
  226.         EXTENDEDVALUE :
  227.             begin
  228.             if extendedValue1 < extendedValue2 then CompareValues := LESSTHAN
  229.             else if extendedValue1 = extendedValue2 then CompareValues :=EQUALTO
  230.             else CompareValues := GREATERTHAN;
  231.             end;
  232.         COMPVALUE :
  233.             begin
  234.             if compValue1 < compValue2 then CompareValues := LESSTHAN
  235.             else if compValue1 = compValue2 then CompareValues := EQUALTO
  236.             else CompareValues := GREATERTHAN;
  237.             end;
  238. }
  239.       end;                                        (* end of case statement *)
  240.     end;                                     (* end of CompareValues routine *)
  241.  
  242.  
  243. (* This routine will compare two values of type STRINGVALUE and look for a
  244.    partial match.  The first parameter (paramValue1) contains a substring which
  245.    will be searched for in paramValue2.  The search is only to see if
  246.    paramValue2 starts with substring paramValue1.  If paramValue2 starts with
  247.    paramValue1 then EQUALTO will be returned.  Otherwise if paramValue1 is
  248.    less that paramValue2 then LESSTHAN will be returned.  If paramValue1 is
  249.    greater that paramValue2 then GREATERTHAN will be returned.               *)
  250.  
  251. function SubstringCompare(var paramValue1;
  252.                           var paramValue2) : Comparison;
  253.  
  254. var
  255.     stringValue1 : String   absolute paramValue1;
  256.     stringValue2 : String   absolute paramValue2;
  257.  
  258.     begin
  259.     if Pos(stringValue2,stringValue1) = 1 then
  260.         begin
  261.         SubstringCompare := EQUALTO;
  262.         end
  263.     else
  264.         begin
  265.         if stringValue1 < stringValue2 then
  266.             begin
  267.             SubstringCompare := LESSTHAN;
  268.             end
  269.         else
  270.             begin
  271.             SubstringCompare := GREATERTHAN;
  272.             end;
  273.         end;
  274.     end;                                  (* end of SubstringCompare routine *)
  275.  
  276.  
  277. (* This routine will check to see if the substring passed in as paramValue1
  278.    is contained in the string passed in as paramValue2.  It will return TRUE
  279.    if paramValue1 is contained in paramValue2 and FALSE otherwise.           *)
  280.  
  281. function ContainsSubstring(var paramValue1;
  282.                            var paramValue2) : Boolean;
  283.  
  284. var
  285.     stringValue1 : String   absolute paramValue1;
  286.     stringValue2 : String   absolute paramValue2;
  287.  
  288.     begin
  289.     if Pos(stringValue1,stringValue2) > 0 then
  290.         begin
  291.         ContainsSubstring := TRUE;
  292.         end
  293.     else
  294.         begin
  295.         ContainsSubstring := FALSE;
  296.         end;
  297.     end;                                 (* end of ContainsSubstring routine *)
  298.  
  299.  
  300. (* This routine will check to see if the substring passed in as paramValue1
  301.   starts the string passed in as paramValue2.  It will return TRUE if
  302.   paramValue1 starts paramValue2 and FALSE otherwise.                        *)
  303.  
  304. function StartsWithSubstring(var paramValue1;
  305.                              var paramValue2) : Boolean;
  306.  
  307. var
  308.     stringValue1 : String   absolute paramValue1;
  309.     stringValue2 : String   absolute paramValue2;
  310.  
  311.     begin
  312.     if Pos(stringValue1,stringValue2) = 1 then
  313.         begin
  314.         StartsWithSubstring := TRUE;
  315.         end
  316.     else
  317.         begin
  318.         StartsWithSubstring := FALSE;
  319.         end;
  320.     end;                                (* end of StartsWithSubstring routine *)
  321.  
  322.  
  323. (* This routine will check to see if the substring passed in as paramValue1
  324.   ends the string passed in as paramValue2.  It will return TRUE if
  325.   paramValue1 ends paramValue2 and FALSE otherwise.                          *)
  326.  
  327. function EndsWithSubstring(var paramValue1;
  328.                            var paramValue2) : Boolean;
  329.  
  330. var
  331.     stringValue1 : String   absolute paramValue1;
  332.     stringValue2 : String   absolute paramValue2;
  333.  
  334.     begin
  335.     if Pos(stringValue1,stringValue2) =
  336.        (Length(stringValue2) - Length(stringValue1)) + 1 then
  337.         begin
  338.         EndsWithSubstring := TRUE;
  339.         end
  340.     else
  341.         begin
  342.         EndsWithSubstring := FALSE;
  343.         end;
  344.     end;                                 (* end of EndsWithSubstring routine *)
  345.  
  346. end.                                                  (* end of Compare unit *)
  347.