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

  1. /* pmklib.c -- personal library -- contents:    1984 Apr 29
  2.     floating-point dump
  3.     utility fcns - find punct, fill str, 
  4.         convert filename, hexdump
  5.     time difference
  6.     evaluate & plot functions
  7.     PM Krasno 17813 Kiowa Trail Los Gatos CA 95030
  8. */
  9.  
  10. #include "bdscio.h"
  11. #include "float.h"
  12. #define short char
  13.  
  14. fpdump (x) char *x ; {    /* value & hex dump */
  15.     printf ("%e: ", x) ; hexdump (x, 5) ;
  16. } /* fpdump */
  17.  
  18. /* some little utility functions */
  19. int punct (p, s) char *p, *s ;    { /* find punct char */
  20.     /* returns 1st char from p found in s (or '\0') */
  21.     char *ps ; 
  22.     for ( ; *s ; ++s)
  23.         for (ps = p ; *ps ; ++ps)
  24.             if ( *ps == *s ) return *ps ;
  25.     return 0 ;
  26. } /* punct */
  27.  
  28. fill (s, n, c) char *s, c ; int n ; { /* fill s with n c's */
  29.     while ( n-- ) *s++ = c ; *s = 0 ;
  30. } /* fill */
  31.  
  32. fix_name (t, f) char *t, *f ; { /* delete ' ', insert '.' */
  33.     int i ;
  34.     for (i=0 ; i < 8 ; ++i)
  35.         if ( (t[i] = *f++) == ' ' ) break ;
  36.     t[i++] = '.' ;
  37.     while ( *f == ' ' ) f++ ;
  38.     while ( ((t[i] = *f++) != ' ') && ++i < 12 )    ;
  39.     t[i] = '\0' ;
  40. } /* fix_name */
  41.  
  42. spread (t, f) byte *t, *f ;  { /* serarate name.ext */
  43.     int i ;    
  44.     for (i = 0 ; i < 8 ; ++i)
  45.         *t++ = ((*f == '.') || (*f == '\0')) ? 
  46.             ' ' : *f++ ;
  47.     if (*f == '.')    f++ ;
  48.     for ( ; i < 11; ++i)
  49.         *t++ = (*f == '\0') ? ' ' : *f++ ;
  50. } /* spread */
  51.  
  52. hexdump (l, n) byte *l ; int n ; { /* debug tool */
  53.     int i ;
  54.     while ( n )    {
  55.         printf ("%04x: ", l) ;  /* loc */
  56.         for (i = 16 ; i && n ; --i, --n)
  57.             printf ("%02x ", *l++) ;
  58.         printf ("\N") ;
  59.     } /* lines */
  60. } /* hexdump */
  61.  
  62. unsigned tim_dif (d, t1, t2) char *d, *t1, *t2 ; 
  63. /* time difference in seconds, up to 18hrs or so */ {
  64.     char *t ; int n ; unsigned dif ;
  65.     unsigned val[12] ; /* seconds, each digit-position */
  66.     val[6] = 36000 ;    val[7] =  3600 ; /* hrs */
  67.     val[8] =   600 ;    val[9] =    60 ; /* min */
  68.     val[10] =   10 ;    val[11] =    1 ; /* sec */
  69.      n = 0 ; /* digit counter */
  70.     while ( t1[n] && t1[n] == t2[n] ) /* leading zeroes */
  71.         ++n ; 
  72.     if ( t1[n] < t2[n] ) /* if negative, swap pointers */
  73.         { t = t1 ; t1 = t2 ; t2 = t  ; }
  74.     if ( n < 6 ) return 65535 ; /* error - day changed */
  75.     for (dif = 0 ; n < 12 ; ++n)
  76.         dif += val[n] * (t1[n] - t2[n]) ;    
  77.     return dif ;
  78. } /* tim_dif */
  79.  
  80. /* evaluate & plot function(s) */
  81.  
  82. comp_pts (npts, nfcns, x, y, llim, delta, f0, f1, f2, f3)
  83. int npts, nfcns ;    float *x, *y, *llim, *delta ;
  84. float *(*f0)(), *(*f1)(), *(*f2)(), *(*f3)() ;
  85. { /* compute array of function-values ; fcns is an array of 
  86. pointers to functions returning pointers to float */
  87.     int f, n, fmax ; 
  88.     float tx[1], *fpmult (), *itof (), (*fptr) (), x0[1] ;
  89.     
  90.     fmax = npts * nfcns ;    fpasg (x0, llim) ;
  91.     for (n = 0 ; n < npts ; ++n, ++x, ++y) {
  92.         fpadd (x, x0, fpmult (tx, delta, itof (tx, n))) ;
  93.         /* x[n] = x[0] + delta * n ; */
  94.         /* kludge - can't have array of fcn ptrs ! */
  95.         /* for (f = 0 ; f < nfcns ; f++)
  96.         y[f][n] = fcn[f] (x[n]) ;    */
  97.         (*f0) (y,        x) ; if ( nfcns == 1 ) continue ;
  98.         (*f1) (y+npts,   x) ; if ( nfcns == 2 ) continue ;
  99.         (*f2) (y+2*npts, x) ; if ( nfcns == 3 ) continue ;
  100.         (*f3) (y+3*npts, x) ; if ( nfcns == 4 ) continue ;
  101.     } /* point loop */
  102. } /* comp_pts */
  103.  
  104. #define _x 128    /* center of screen */
  105. #define _y 96
  106.  
  107. fpline (x1, y1, x2, y2, xs, ys, c) 
  108. float   *x1, *y1, *x2, *y2, *xs, *ys ; int c ; {
  109.     float sx1, sy1, sx2, sy2 ; int ftoir () ;
  110.     byte  bx1, by1, bx2, by2 ;
  111.     bx1 = _x + ftoir (fpmult (sx1, x1, xs)) ;
  112.     by1 = _y - ftoir (fpmult (sy1, y1, ys)) ;
  113.     bx2 = _x + ftoir (fpmult (sx2, x2, xs)) ;
  114.     by2 = _y - ftoir (fpmult (sy2, y2, ys)) ;
  115.     line (bx1, by1, bx2, by2, -1) ;
  116. } /* fpline */
  117.  
  118. plot_pts (npts, nfcns, x, y, scx, scy) int npts, nfcns ;
  119. float *x, *y, *scx, *scy ;    { /* plot points */
  120.     int n, f, fmax ;     byte bx, by, ftoir() ;
  121.     float *fpmult(), sy[1], sx[1] ;
  122.  
  123.     fmax = npts * nfcns ;
  124.     for (n = 0 ; n < npts ; ++n, ++x, ++y)    {
  125.         bx = ftoir (fpmult (sx, scx, x)) + _x; 
  126.         for (f = 0 ; f < fmax ; f += npts)    {
  127.         by = _y - ftoir (fpmult (sy, scy, y+f)) ;
  128.         set (bx, by, -1) ;
  129.         } /* fcn loop */
  130.     } /* plot loop */
  131. } /* plot_pts */
  132.  
  133. plot_lins (npts, nfcns, x, y, scx, scy) int npts, nfcns ;
  134. float *x, *y, *scx, *scy ;    { /* plot lines */
  135.     int n, f, fmax ;
  136.      byte bx1, bx2, by1, by2, ftoir() ;
  137.     float *fpmult(), t ;
  138.  
  139.     fmax = npts * nfcns ;
  140.     bx1 = _x + ftoir (fpmult (t, scx, x) ) ;
  141.     for (f = 0 ; f < fmax ; f += npts)
  142.         by1 = _y - ftoir (fpmult (t, scy, y+f) ) ;
  143.     for (n = 1 ; n < npts ; ++n, ++x, ++y)    {
  144.         bx2 = _x + ftoir (fpmult (t, scx, x) ) ;
  145.         for (f = 0 ; f < fmax ; f += npts)    {
  146.         by2 = _y - ftoir (fpmult (t, y+f, scy) ) ;
  147.         line (bx1, by1, bx2, by2, -1) ;
  148.         bx1 = bx2 ;    by1 = by2 ;    } /* fcn loop */
  149.     } /* plot loop */
  150. } /* plot_lins */
  151.  
  152. axes (x, y) { /* plot axis lines */
  153.     /* x,y in screen coords, relative to _x, _y */
  154.     line (x + _x, 0, x + _x, _y + _y -1, -1) ;
  155.     line (0, _y - y, _x + _x -1, _y - y, -1) ;
  156. } /* axes */
  157. #undef _x
  158. #undef _y
  159.  
  160.  for (f = 0 ; f