home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / _STR2MAP.C < prev    next >
Text File  |  1984-12-31  |  3KB  |  75 lines

  1. /*  File   : _str2map.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 20 April 1984
  4.     Defines: _map_vec[], _str2map().
  5.  
  6.     _str2map(option, from, to) constructs a translation table.  If  from
  7.     or to is NullS, the same string is used as last time, so if you want
  8.     to translate a whole lot of strings using the same mapping you don't
  9.     have to reconstruct it each time.  The options are
  10.  
  11.     0: initialise the map to the identity function,
  12.        then map each from[i] to the corresponding to[i].
  13.        If to[] is shorter than from[], its last character is
  14.        repeated as often as needed.
  15.  
  16.     1: as 0, but don't initialise the map.
  17.  
  18.     2: initialise the map to send every character to to[0],
  19.        then map each from[i] to itself.
  20.  
  21.     For example, to build a map which forces letters to lower case but
  22.     sends everything else to blank, call
  23.  
  24.     _str2map(2, "abcdefghijklmnopqrstuvwxyz", " ");
  25.     _str2map(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz");
  26.  
  27.     Only strtrans() and strntrans() in this package  call  _str2map;  if
  28.     you  want  to  build your own maps this way you can "fool" them into
  29.     using it, as when the two strings are NullS they  don't  change  the
  30.     map.   As an extra-special dubious *hack*, _map_vec has an extra NUL
  31.     character at the end, so after calling _str2map(0, "", ""), you  can
  32.     use  _map_vec+1 as a string of the 127 non-NUL characters (or if the
  33.     _AlphabetSize is 256, of the 255 non-NUL characters).
  34. */
  35.  
  36. #include "strings.h"
  37. #include "_str2map.h"
  38.  
  39. static    _char_    *oldFrom = "?";
  40. static    char    *oldTo   = "?";
  41.  
  42. char    _map_vec[_AlphabetSize+1];
  43.  
  44. void _str2map(option, from, to)
  45.     int option;
  46.     register _char_ *from;
  47.     register char *to;
  48.     {
  49.     register int i, c;
  50.  
  51.     if (from == NullS && to == NullS) return;
  52.     if (from == NullS) from = oldFrom; else oldFrom = from;
  53.     if (to   == NullS) to   = oldTo;   else oldTo   = to;
  54.     switch (option) {
  55.         case 0:
  56.         for (i = _AlphabetSize; --i >= 0; _map_vec[i] = i) ;
  57.         case 1:
  58.         while (i = *from++) {
  59.             _map_vec[i] = *to++;
  60.             if (!*to) {
  61.             c = *--to;
  62.             while (i = *from++) _map_vec[i] = c;
  63.             return;
  64.             }
  65.         }
  66.         return;
  67.         case 2:
  68.         c = *to;
  69.         for (i = _AlphabetSize; --i >= 0; _map_vec[i] = c) ;
  70.         while (c = *from++) _map_vec[c] = c;
  71.         return;
  72.     }
  73.     }
  74.