home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / UT_XLATE.C < prev   
Encoding:
C/C++ Source or Header  |  1990-07-10  |  1.4 KB  |  93 lines

  1. /*********************
  2.  *
  3.  *  ut_xlate.c - translate functions.
  4.  *
  5.  *  Purpose: This file contains table translate functions.
  6.  *
  7.  *  Blackstar C Function Library
  8.  *  (c) Copyright 1985,1989 Sterling Castle Software
  9.  *
  10.  *******/
  11.  
  12. #include <string.h>
  13. #include "blackstr.h"
  14.  
  15.  
  16. /********
  17.  *
  18.  *   ut_xlateb(byte,table) - translate byte in table
  19.  *
  20.  **/
  21.  
  22. char ut_xlateb(char byte, char *table)
  23. {
  24.     while(*table) {
  25.     if(*table++==byte)
  26.         return(*table);
  27.     else    table++;
  28.     }
  29.     return(byte);
  30. }
  31.  
  32.  
  33. /********
  34.  *
  35.  *   ut_xtable(str,table) - look up string in table
  36.  *
  37.  **/
  38.  
  39. int ut_xtable(char *str, char *table[])
  40. {
  41.     int i=0;
  42.  
  43.     while(*table) {
  44.     if(!strcmpi(str,*table))
  45.         return(i);
  46.     else ++i,++table;
  47.     }
  48.     return(ERROR);
  49. }
  50.  
  51.  
  52. /********
  53.  *
  54.  *   ut_xlprox(str,table) - look up closest match in table
  55.  *
  56.  **/
  57.  
  58. int ut_xlprox(char *str, char *table[])
  59. {
  60.     int xv[255],tv,i;
  61.  
  62.     tv= 0;
  63.     i=0;
  64.     while(*table) {
  65.     xv[i] = st_patmai(str,*table);  /* # chars matched */
  66.     ++i;
  67.     ++table;
  68.     }
  69.     while(i--) {
  70.     if(xv[i] > xv[tv])              /* find highest match value */
  71.         tv = i;
  72.     }
  73.     return(xv[tv]>=0 ? tv: -1);
  74. }
  75.  
  76.  
  77. /********
  78.  *
  79.  *   ut_xlstr(str,table) - translate string from table
  80.  *
  81.  **/
  82.  
  83. char *ut_xlstr(char *str, char *table[])
  84. {
  85.     int i;
  86.  
  87.     if((i=ut_xlprox(str,table)) != -1)
  88.     strcpy(str,table[i]);
  89.     else
  90.     *str = NUL;
  91.     return(str);
  92. }
  93.