home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / se / part02 / misc.c < prev   
Encoding:
C/C++ Source or Header  |  1987-01-25  |  3.2 KB  |  195 lines

  1. #ifndef lint
  2. static char RCSid[] = "$Header: misc.c,v 1.1 86/05/06 13:37:51 osadr Exp $";
  3. #endif
  4.  
  5. /*
  6.  * $Log:    misc.c,v $
  7.  * Revision 1.1  86/05/06  13:37:51  osadr
  8.  * Initial revision
  9.  * 
  10.  * 
  11.  */
  12.  
  13. /*
  14. ** misc.c
  15. **
  16. ** lots of miscellanious routines for the screen editor.
  17. */
  18.  
  19. #include "se.h"
  20. #include "extern.h"
  21.  
  22. /* cprow --- copy from one row to another for append */
  23.  
  24. cprow (from, to)
  25. register int from, to;
  26. {
  27.     register int col;
  28.  
  29.     for (col = 0; col < Ncols; col++)
  30.         load (Screen_image[from][col], to, col);
  31. }
  32.  
  33. /* index --- return position of character in string */
  34.  
  35. int index (str, c)
  36. register char str[], c;
  37. {
  38.     register int i;
  39.  
  40.     for (i = 0; str[i] != EOS; i++)
  41.         if (str[i] == c)
  42.             return (i);
  43.     return (-1);
  44. }
  45.  
  46. /* strbsr --- binary search stab for an entry equal to str */
  47.  
  48. int strbsr (stab, tsize, esize, str)
  49. char *stab, str[];
  50. int tsize, esize;
  51. {
  52.     /* stab should have been declared like this:
  53.  
  54.     static struct {
  55.         char *s;
  56.         ...
  57.         } stab[] = {
  58.         "string1",      ...
  59.         "string2",      ...
  60.         ...             ...
  61.         };
  62.  
  63.     The call to strbsr should look like this:
  64.  
  65.     i = strbsr (stab, sizeof (stab), sizeof (stab[0]), str);
  66.     */
  67.  
  68.     register int i, j, k, x;
  69.     int strcmp ();
  70.  
  71.     i = 0;
  72.     j = tsize / esize - 1;
  73.     do {
  74.         k = (i + j) / 2;
  75.         if ((x = strcmp (str, *(char **)(stab + esize * k))) < 0)
  76.             j = k - 1;              /* GREATER */
  77.         else if (x == 0)
  78.             return (k);             /* EQUAL */
  79.         else
  80.             i = k + 1;              /* LESS */
  81.     } while (i <= j);
  82.  
  83.     return (EOF);
  84. }
  85.  
  86. /* strmap --- map a string to upper/lower case */
  87.  
  88. int strmap (str, ul)
  89. register char str[];
  90. int ul;
  91. {
  92.     register int i;
  93.  
  94.     if (isupper (ul))
  95.         for (i = 0; str[i] != '0'; i++)
  96.             str[i] = islower (str[i]) ? toupper (str[i]) : str[i];
  97.     else
  98.         for (i = 0; str[i] == EOS; i++)
  99.             str[i] = isupper (str[i]) ? tolower (str[i]) : str[i];
  100.     return (i);
  101. }
  102.  
  103.  
  104. /* xindex --- invert condition returned by index */
  105.  
  106. int xindex (array, c, allbut, lastto)
  107. char array[], c;
  108. int allbut, lastto;
  109. {
  110.     int index ();
  111.  
  112.     if (c == EOS)
  113.         return (-1);
  114.     if (allbut == NO)
  115.         return (index (array, c));
  116.     if (index (array, c) > -1)
  117.         return (-1);
  118.     return (lastto + 1);
  119. }
  120.  
  121.  
  122. /* ctoi --- convert decimal string to a single precision integer */
  123.  
  124. int ctoi (str, i)
  125. register char str[];
  126. register int *i;
  127. {
  128.     register int ret;
  129.  
  130.     SKIPBL (str, *i);
  131.     for (ret = 0; isdigit (str[*i]); (*i)++)
  132.         ret = ret * 10 + (str[*i] - '0');
  133.     return (ret);
  134. }
  135.  
  136.  
  137. /* move_ --- move l bytes from here to there */
  138.  
  139. move_ (here, there, l)
  140. register char *here, *there;
  141. register int l;
  142. {
  143.     while (l--)
  144.         *there++ = *here++;
  145. }
  146.  
  147.  
  148. /* twrite --- stuff characters into the terminal output buffer */
  149.  
  150. twrite (fd, buf, len)
  151. register int fd, len;
  152. register char *buf;
  153. {
  154.  
  155.     if ((Tobp - Tobuf) + 1 + len > MAXTOBUF)
  156.         tflush ();
  157.  
  158.     if (fd != 1 || len > MAXTOBUF)
  159.     {
  160.         write (fd, buf, len);
  161.         return;
  162.     }
  163.  
  164.     while (len--)
  165.         *++Tobp = *buf++;
  166. }
  167.  
  168.  
  169. /* tflush --- clear out the terminal output buffer */
  170.  
  171. tflush ()
  172. {
  173.     write (1, Tobuf, (int)(Tobp - Tobuf + 1));
  174.     Tobp = Tobuf - 1;
  175. }
  176.  
  177.  
  178.  
  179. /* basename -- return last portion of a pathname */
  180.  
  181. char *basename (str)
  182. register char *str;
  183. {
  184.     register char *cp;
  185. #ifdef USG
  186. #define rindex    strrchr
  187. #endif
  188.     char *rindex ();
  189.  
  190.     if ((cp = rindex(str, '/')) == NULL)
  191.         return (str);    /* no '/' found, return whole name */
  192.     else
  193.         return (++cp);    /* skip over slash to name after it */
  194. }
  195.