home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / se / part3 / misc.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.0 KB  |  183 lines

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