home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CMPSTR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  9KB  |  281 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /******************************************************************************
  4. * @(#) CMPSTR  - Comparing strings with variable sort order
  5. * @(#) TCMPSTR - Comparing strings with variable sort order and
  6. *                masking/truncation
  7. *
  8. *
  9. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  10. *1994-03-15/Bac
  11. *   TCMPSTR added
  12. *
  13. *******************************************************************************
  14. *1994/Erik Bachmann (E-mail: ebp@dde.dk)
  15. *
  16. * Released to public domain 27-Oct-95
  17. ******************************************************************************/
  18.  
  19. #include <string.h>                    /* strlen */
  20.  
  21. /**
  22. #define TEST
  23. **/
  24.  
  25. /*
  26.  /-------------------------------------\
  27. |          CMPSTR                       |------------------------------------|
  28. |\-------------------------------------/
  29. |
  30. | Comparing two strings with variable sort order
  31. |
  32. |
  33. | Examples of sort sequences found in SORTKEY.h
  34. |
  35. |
  36. |
  37. |----------------------------------------------------------------------------|
  38. | CALL:
  39. |    iOrder = cmpstr("abcæ¢å", "abcå¢æ",
  40. |                    (unsigned char *) CaseMatch) ;
  41. |
  42. | HEADER:
  43. |    sortkey.h
  44. |
  45. | GLOBALE VARIABLES:
  46. |    %
  47. |
  48. | ARGUMENTS:
  49. |    char *pszStr1      Pointer to first string
  50. |    char *pszStr2      Pointer to second string
  51. |    char *pszOrder     Pointer to table with sort sequences
  52. |
  53. | PROTOTYPE:
  54. |    int far cmpstr(unsigned char *pszStr1, unsigned char *pszStr2,
  55. |                   unsigned char *pszOrder) ;
  56. |
  57. | RETURN VALUE:
  58. |    -1          pszStr1 is first
  59. |    0           the two strings are identical (i sort order)
  60. |    1           pszStr2 is first
  61. |
  62. | MODULE:
  63. |    cmpstr.c
  64. |----------------------------------------------------------------------------|
  65. |1993-06-07/Bac   Length comparision uptimised (length calculation excluded)
  66.  
  67. |
  68. |----------------------------------------------------------------------------|
  69. |1993-04-10/Erik Bachmann
  70. \---------------------------------------------------------------------------|*/
  71.  
  72. int far cmpstr(unsigned char *pszStr1,
  73.                unsigned char *pszStr2,
  74.                unsigned char *pszOrder)
  75. {
  76.       int i = 0;
  77.  
  78.       /*---------------------------------------------------------*/
  79.  
  80.       while (('\0' != pszStr1[i]) && ('\0' != pszStr2[i]))
  81.       {
  82.             if (pszOrder[pszStr1[i]] < pszOrder[pszStr2[i]])
  83.                   return -1;        /* Char from first string is first  */
  84.  
  85.             if (pszOrder[pszStr1[i]] > pszOrder[pszStr2[i]])
  86.                   return 1;         /* Char from second string is first */
  87.  
  88.             i++;                    /* Next char                        */
  89.       }
  90.  
  91.       if (('\0' == pszStr1[i] ) && ('\0' != pszStr2[i]))
  92.             return -1;              /* First string is shortest         */
  93.  
  94.       if (('\0' != pszStr1[i] ) && ('\0' == pszStr2[i]))
  95.             return 1;               /* Second string is shortest        */
  96.  
  97.       return 0;                     /* They are identical               */
  98. }
  99.  
  100. /*
  101.  /-------------------------------------\
  102. |          TCMPSTR                      |------------------------------------|
  103. |\-------------------------------------/
  104. |
  105. | Comparing two strings with variable sort order and truncation/masking
  106. |
  107. |
  108. | Examples of sort sequences found in SORTKEY.h
  109. |
  110. |
  111. |
  112. |----------------------------------------------------------------------------|
  113. | CALL:
  114. |    iOrder = cmpstr("ab$æ¢å?", "abcå¢æ123",
  115. |                    (unsigned char *) CaseMatch, "$", "?") ;
  116. |
  117. | HEADER:
  118. |    sortkey.h
  119. |
  120. | GLOBALE VARIABLES:
  121. |    %
  122. |
  123. | ARGUMENTS:
  124. |    char *pszStr1      Pointer to first string
  125. |    char *pszStr2      Pointer to second string
  126. |    char *pszOrder     Pointer to table with sort sequences
  127. |
  128. | PROTOTYPE:
  129. |    int far cmpstr(unsigned char *pszStr1,  unsigned char *pszStr2,
  130. |                   unsigned char *pszOrder, unsigned char *pszMask,
  131. |                   unsigned char *pszTrunc) ;
  132. |
  133. | RETURN VALUE:
  134. |    -1          pszStr1 is first
  135. |    0           the two strings are identical (i sort order)
  136. |    1           pszStr2 is first
  137. |
  138. | MODULE:
  139. |    cmpstr.c
  140. |----------------------------------------------------------------------------|
  141. |
  142. |----------------------------------------------------------------------------|
  143. |1994-03-14/Erik Bachmann
  144. \---------------------------------------------------------------------------|*/
  145.  
  146. int far tcmpstr(unsigned char *pszStr1,
  147.                 unsigned char *pszStr2,
  148.                 unsigned char *pszOrder,
  149.                 unsigned char *pszMask,
  150.                 unsigned char *pszTrunc)
  151. {
  152.       int  i = 0;
  153.       char cTruncateFlag = 0;
  154.       int  iCompareLength;
  155.  
  156.       /*---------------------------------------------------------*/
  157.  
  158.       iCompareLength = strlen(pszStr1) - 1 ;
  159.                                     /* Find last charater i string 1    */
  160.  
  161.       if (strrchr(pszTrunc, pszStr1[iCompareLength])
  162.           && (0 <= iCompareLength))
  163.                                     /* While string is terminated by one */
  164.       {                             /* of the specified characters       */
  165.  
  166.             cTruncateFlag++;
  167.             iCompareLength--;
  168.       }
  169.  
  170.       while ((((0 != cTruncateFlag) && (i < iCompareLength)
  171.                || (0 == cTruncateFlag) && ('\0' != pszStr1[i])))
  172.              && ('\0' != pszStr2[i]))
  173.       {
  174.             if (0 != strchr(pszMask, pszStr1[i]))
  175.             {
  176.                   i++;
  177.                   continue;         /* If string masked: continue       */
  178.             }
  179.  
  180.             if (pszOrder[pszStr1[i]] < pszOrder[pszStr2[i]])
  181.                   return -1;        /* Char from first string is first  */
  182.  
  183.             if (pszOrder[pszStr1[i]] > pszOrder[pszStr2[i]])
  184.                   return 1;         /* Char from second string is first */
  185.  
  186.             i++;                    /* Next char                        */
  187.       }
  188.  
  189.       if ((('\0' == pszStr1[i]) && ('\0' != pszStr2[i]))
  190.           && (0 == cTruncateFlag))
  191.       {
  192.             return -1;              /* First string is shortest         */
  193.       }
  194.  
  195.       if ((('\0' != pszStr1[i]) && ('\0' == pszStr2[i]))
  196.           && 0 == cTruncateFlag)
  197.       {
  198.             return 1;               /* Second string is shortest        */
  199.       }
  200.  
  201.       return 0;                     /* They are identical               */
  202. }
  203.  
  204.  
  205. #ifdef TEST
  206.  
  207. #include "sortkey.h"
  208.  
  209. void state(int iOrder)
  210. {
  211.       if (0 == iOrder)
  212.             printf("\tequal");
  213.       else
  214.       {
  215.             if (0 <= iOrder)
  216.                   printf("\tString 1 is higher than string 2");
  217.             else  printf("\tString 1 is lower than string 2");
  218.       }
  219. }
  220.  
  221. void main(void)
  222. {
  223.       int iOrder = 0;
  224.       char  *str1a   = "ab$æ¢å?",
  225.             *str1b   = "ab$æ¢å",
  226.             *str1c   = "abcæ¢å?",
  227.             *str1d   = "ab$æ¢å",
  228.             *str1e   = "abcåæ¢",
  229.             *str1f   = "abcæ¢å_",
  230.  
  231.             *str2a   = "abcæ¢å123",
  232.             *str2b   = "abcæ¢å";
  233.  
  234.       printf("\n\n");
  235.  
  236.       /* 2a */
  237.  
  238.       iOrder = tcmpstr(str1a, str2a, (unsigned char *) CaseMatch, "$", "?");
  239.       printf("\n%10s / %10s", str1a, str2a);
  240.       state(iOrder);
  241.  
  242.       iOrder = tcmpstr(str1b, str2a, (unsigned char *) CaseMatch, "$", "?");
  243.       printf("\n%10s / %10s", str1b, str2a);
  244.       state(iOrder);
  245.  
  246.       iOrder = tcmpstr(str1c, str2a, (unsigned char *) CaseMatch, "$", "?");
  247.       printf("\n%10s / %10s", str1c, str2a);
  248.       state(iOrder);
  249.  
  250.       iOrder = tcmpstr(str1d, str2a, (unsigned char *) CaseMatch, "$", "?");
  251.       printf("\n%10s / %10s", str1d, str2a);
  252.       state(iOrder);
  253.  
  254.       iOrder = tcmpstr(str1e, str2a, (unsigned char *) CaseMatch, "$", "?");
  255.       printf("\n%10s / %10s", str1e, str2a);
  256.       state(iOrder);
  257.  
  258.       iOrder = tcmpstr(str1f, str2a, (unsigned char *) CaseMatch, "$", "?");
  259.       printf("\n%10s / %10s", str1f, str2a);
  260.       state(iOrder);
  261.  
  262.       /* 2b */
  263.  
  264.       iOrder = tcmpstr(str1a, str2b, (unsigned char *) CaseMatch, "$", "?");
  265.       printf("\n%10s / %10s", str1a, str2b);
  266.       state(iOrder);
  267.  
  268.       iOrder = tcmpstr(str1b, str2b, (unsigned char *) CaseMatch, "$", "?");
  269.       printf("\n%10s / %10s", str1b, str2b);
  270.       state(iOrder);
  271.  
  272.       iOrder = tcmpstr(str1c, str2b, (unsigned char *) CaseMatch, "$", "?");
  273.       printf("\n%10s / %10s", str1c, str2b);
  274.       state(iOrder);
  275.  
  276.       iOrder = tcmpstr(str1d, str2b, (unsigned char *) CaseMatch, "$", "?");
  277.       printf("\n%10s / %10s", str1d, str2b);
  278.       state(iOrder);
  279. }
  280. #endif
  281.