home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 126_01 / martz_jr.c < prev    next >
Text File  |  1985-03-10  |  10KB  |  341 lines

  1. /*********************************************************************\
  2. ** .---------------------------------------------------------------. **
  3. ** |                                                               | **
  4. ** |                                                               | **
  5. ** |         Copyright (c) 1981, 1982, 1983 by Eric Martz.         | **
  6. ** |                                                               | **
  7. ** |                                                               | **
  8. ** |       Permission is hereby granted to use this source         | **
  9. ** |       code only for non-profit purposes. Publication of       | **
  10. ** |       all or any part of this source code, as well as         | **
  11. ** |       use for business purposes is forbidden without          | **
  12. ** |       written permission of the author and copyright          | **
  13. ** |       holder:                                                 | **
  14. ** |                                                               | **
  15. ** |                          Eric Martz                           | **
  16. ** |                         POWER  TOOLS                          | **
  17. ** |                    48 Hunter's Hill Circle                    | **
  18. ** |                      Amherst MA 01002 USA                     | **
  19. ** |                                                               | **
  20. ** |                                                               | **
  21. ** `---------------------------------------------------------------' **
  22. \*********************************************************************/
  23.  
  24. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  25. /* MARTZLIB.C IS A LIBRARY OF FUNCTIONS OF GENERAL USE.
  26.  
  27.     MARTZ-JR.C CONTAINS:
  28.  
  29.         jindex(s,t)
  30.         jsubstr(s,t,x,y)
  31.         leftc1(c)
  32.         lefteq(s,match)
  33.         lower(s)
  34.         only(s,o)
  35.     char *pack (dest_buf, pt_dest_cnt, max_cnt, src_buf)
  36.         pad(buf, side, width, c)
  37.         reset()
  38.         reverse(s)
  39.         righteq(s, m)
  40.         rindex(s,k,maxpos)
  41.         ring(times)
  42.         rshift(buf,dist)
  43. */
  44. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  45. jindex(s,t)
  46.  
  47. /* Finds first occurrence of "t" in "s", and returns the index (position in
  48. "s" where "t" begins). Returns -1 if "t" is not found in "s"; returns 0 if "t"
  49. is a null string.
  50.  
  51. This function supplied by Jeremy Poole, HSCF at Harvard School of Public
  52. Health. */
  53.  
  54. char s[], t[];
  55. {       register char *p1, *p2, *p3;
  56.     if (!*t)
  57.         return(0);
  58.     for (p1 = s; *p1; p1++)
  59.         if (*p1 == *t)
  60.         {       for (p2 = p1+1, p3 = t+1; *p3 && *p2 == *p3; p2++, p3++);
  61.             if (!*p3)
  62.                 return(p1 - s);
  63.         }
  64.     return(-1);
  65. }
  66. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  67. jsubstr(s,t,x,y)
  68.  
  69. /* puts into s the substring of t from position x onward
  70. including y characters. if y < 0 then all characters past
  71. position x are included. Returns actual number of characters put
  72. into s.
  73.  
  74. This function supplied by Jeremy Poole, HSCF at Harvard School of Public
  75. Health. */
  76.  
  77. char s[], t[];
  78. int x, y;
  79. {    register i, j;
  80.     register char *pp;
  81.     for (j = 0, pp = t; *pp++; j++);
  82.     if (x < 0)
  83.         x = 0;
  84.     if (x > j)
  85.         x = j;
  86.     if (y < 0)
  87.         y = j;
  88.     for (i = 0, pp = &t[x]; i < y && (*s++ = *pp++); i++);
  89.     if (i == y)
  90.         *s = '\0';
  91.     return(i);
  92. }
  93. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  94. leftc1(c)
  95.  
  96. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  97.  
  98. /* ARGUMENT RETURNED IS THE SINGLE LEFTMOST NONBLANK CHARACTER
  99. OF THE NEXT INPUT LINE.  THIS FUNCTION MUST BE CALLED WITH
  100. AN ADDRESS ARGUMENT, SO IF CALLING ROUTINE DECLARES char c;
  101. THEN CALL SHOULD BE leftc1(&c).
  102. UPPER CASE IS CONVERTED TO LOWER. */
  103.  
  104.         char *c;
  105.         {
  106.         char s[MAXLINE];
  107.         int i,len;
  108.         len = gets(s);
  109.         /* LOOP TO STRIP OFF LEADING SPACES */
  110.         i = 0;
  111.         while (s[i] == ' ') ++i;
  112.         *c=s[i];
  113.         *c = (isupper(*c) ? tolower(*c) : *c);
  114. }
  115. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  116. lefteq(s,match)
  117.  
  118. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  119.  
  120. /* This function returns the number of characters in "match" which match the
  121. characters in "s" beginning with the first non-blank character in "s". */
  122.  
  123. /* Example: lefteq("  init","initialize") returns 4. */
  124.  
  125.     char *s, *match;
  126.     {
  127.     char *start;
  128.     while (isspace(*s)) s++;
  129.     start = s;
  130.     for (; *match; s++, match++) {
  131.         if (*s != *match) break;
  132.     }
  133.     return (s - start);
  134. }
  135. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  136. lower(s)
  137.  
  138. /* CONVERTS ALL UPPER CASE LETTERS IN s TO UPPER CASE */
  139.     char *s;
  140.     {
  141.     for (; *s; s++) *s = tolower(*s);
  142. }
  143. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  144. only(s,o)
  145.  
  146. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  147.  
  148. /* Returns TRUE if string "s" contains only characters found in
  149. string "o" (in any order) */
  150.  
  151.     char *s, *o;
  152.     {
  153.     char *p;
  154.     for (;*s;s++) {
  155.         for (p=o; *p; p++) {
  156.             if (*p == *s) goto ok;
  157.         }
  158.         return(FALSE);
  159. ok:        continue;
  160.     }
  161.     return(TRUE);
  162. }
  163. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  164. char *pack (dest_buf, pt_dest_cnt, max_cnt, src_buf)
  165.  
  166. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  167.  
  168. /* PACKS A NULL TERMINATED STRING ONTO THE END OF DEST_BUF BEGINNING AT
  169. DEST_CNT; EACH STRING PACKED ONTO THE END OF DEST_BUF IS DELIMITED FROM THE
  170. PRECEDING STRING WITH A NULL. THUS, A PACKED BUFFER MUST BE USED IN
  171. CONJUNCTION WITH AN ARRAY OF POINTERS TO STRINGS. PACK RETURNS THE POINTER TO
  172. THE STRING PACKED, OR ZERO UPON OVERFLOW OF DEST_BUF. */
  173.  
  174.     char *dest_buf, *src_buf;
  175.     int *pt_dest_cnt, max_cnt;
  176.     {
  177.     int newcnt;
  178.     char *pointer;
  179.     max_cnt -= 1;
  180.     newcnt = *pt_dest_cnt + strlen(src_buf) + 1;
  181.     if (newcnt > max_cnt) return (0);
  182.     pointer = dest_buf + *pt_dest_cnt;
  183.     strcpy(pointer, src_buf);
  184.     *pt_dest_cnt = newcnt;
  185.     return (pointer);
  186. }
  187. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  188. pad(buf, side, width, c)
  189.  
  190. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  191.  
  192. /* PADS STRING IN buf OUT TO width WITH SPACES, ADDING SPACES TO
  193. R OR L side. */
  194.  
  195.     char *buf, side, c;
  196.     int width;
  197.     {
  198.     int i, len, toadd, start;
  199.     len = strlen(buf);
  200.     toadd = width - len;
  201.     if (side EQ 'r' OR side EQ 'R') start = len;
  202.     else {
  203.         start = 0;
  204.         rshift(buf,toadd);
  205.     }
  206.     for (i=1; i<= toadd; i++)
  207.         buf[start++] = c;
  208.     if (side EQ 'r' OR side EQ 'R') buf[len+toadd] = NULL;
  209. }
  210. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  211. reset() {
  212.     int disk;
  213.  
  214. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  215. /* RESETS CP/M 2.2 DISK SYSTEM */
  216.  
  217.     disk = bdos(25, 0); /* GET CURRENT DISK */
  218.     bdos(13, 0);        /* RESET */
  219.     bdos(14, disk);    /* SELECT SAME DISK */
  220. }
  221. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  222. reverse(s)
  223.  
  224. /* The sequence of characters in the string "s" is reversed. */
  225.  
  226.         char *s;
  227.         {
  228.         int c, i, j;
  229.         for (i=0,j=strlen(s)-1;i<j;i++,j--) {
  230.                 c=s[i];
  231.                 s[i]=s[j];
  232.                 s[j]=c;
  233.         }
  234. }
  235. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  236. righteq(s, m)
  237.  
  238. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  239.  
  240. /* RETURNS THE NUMBER OF CHARACTERS AT THE RIGHT OF s WHICH MATCH THOSE IN
  241. m. EXAMPLE: righteq("FILE.SS", ".SS") RETURNS 3; righteq("FILE.RAP", "SNAP")
  242. RETURNS 2 */
  243.  
  244.     char *s, *m;
  245.     {
  246.     char *es, *em;
  247.     int count;
  248.     count = 0;
  249.  
  250.     /* RETURN 0 IF EITHER ARGUMENT IS A NULL STRING */
  251.     if (*s EQ NULL OR *m EQ NULL) return(count);
  252.  
  253.     /* SET END OF STRING POINTERS TO ENDS */
  254.     es = s;
  255.     em = m;
  256.     while (*es NE NULL) es++;
  257.     while (*em NE NULL) em++;
  258.     es--;
  259.     em--;
  260.  
  261.     /* COUNT MATCHES */    
  262.     while (*(es--) EQ *(em--)) {
  263.         count++;
  264.         if (es < s OR em < m) return(count);
  265.     }
  266.     return (count);
  267. }
  268. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  269. rindex(s,k,maxpos)
  270.  
  271. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  272.  
  273. /* FIND INDEX OF RIGHTMOST OCCURRENCE OF KEY STRING "K" IN STRING "S"
  274.    WHERE INDEX <= MAXPOS */
  275.  
  276. /* This function was written to break lines between words. For example, if
  277. lines of text are to be fitted onto a page width of 78 characters, you need to
  278. find the rightmost occurrence of " " in "s" which occurs at or before position
  279. 78. Thus, "rindex(s," ",78);". */
  280.  
  281.     char *s, *k;
  282.     int maxpos;
  283.     {
  284.     int start, i, is, ik;
  285.     if (maxpos < 0) maxpos = 0;
  286.     start = strlen(s)-1;
  287.     if (maxpos < start) start = maxpos;
  288.     for (i = start; i >= 0; i--) {
  289.         for (is=i,ik=0;k[ik];is++,ik++)
  290.             if (s[is] != k[ik]) goto decrement;
  291.         return(i);
  292. decrement:
  293.         continue;
  294.     }
  295.     return(ERROR);
  296. }
  297. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  298. ring(times)
  299.  
  300. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  301.  
  302. /* RINGS BELL times TIMES; IF TIMES IS ZERO, RINGS FOREVER */
  303.     int times;
  304.     {
  305.     int i, dec;
  306.     if (!times) dec = 0;
  307.     else dec = 1;
  308.     times++;
  309.     while(times -= dec) {
  310.         putchar(BELL);
  311.         for (i=1; i<=10000; i++); /* DELAY */
  312.     }
  313. }
  314. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  315. rshift(buf,dist)
  316.  
  317. /* Copyright (c) 1983 by Eric Martz, Amherst MA */
  318.  
  319. /* SHIFTS STRING IN buf dist POSITIONS TO THE RIGHT */
  320. /* EXAMPLE: rshift("abc",2) -> "ababc" */
  321.  
  322.     char *buf;
  323.     int dist;
  324.     {
  325.     int len, i;
  326.     len = strlen(buf);
  327.     for (i=len; i>=0; i--) {
  328.         buf[i+dist] = buf[i];
  329.     }
  330. }
  331. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  332. /*    END OF MARTZ-JR.C    */
  333. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  334. o decrement;
  335.         return(i);
  336. decrement:
  337.         continue;
  338.     }
  339.     return(ERROR);
  340. }
  341. /*++++++++++++++++++++