home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / database / cdbms / clist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-01  |  3.7 KB  |  214 lines

  1. /* ----------------------- clist.c --------------------- */
  2.  
  3. #include <stdio.h>
  4. #include "cdata.h"
  5.  
  6. #define SCRNLINES 20
  7. #define PRNTLINES 55
  8.  
  9. int lct = 99;
  10. int clip;
  11.  
  12. /* ------------ list a record ----------- */
  13.  
  14. /* Send 2 lists; a list of elements in buffer and a list of
  15.  * elements to be listed. Also send address of the buffer.
  16.  * Can be used to list a data base file or an extract file.
  17.  */
  18.  
  19. void clist(fd, inter, fl, pl, bf, fn)
  20. FILE *fd;        /* output file */
  21. int inter;        /* true for user interaction & page breaks */
  22. int *fl;        /* file list  */
  23. int *pl;        /* print list */
  24. char *bf;        /* buffer     */
  25. char *fn;        /* file name  */
  26. {
  27.     char *ln, *cp, *mp, *malloc();
  28.     int width;
  29.     int lw = 0;
  30.     void test_eop();
  31.  
  32.     ln = malloc(epos(0, pl) + 1);
  33.     clip = ((!inter || isatty(fileno(fd))) ? 79 : 136);
  34.     if (inter)
  35.         test_eop(fd, fn, pl);
  36.     rcd_fill(bf, ln, fl, pl);
  37.     cp = ln;
  38.     if (*pl)    {
  39.         putc('\n', fd);
  40.         putc('\r', fd);
  41.         lct++;
  42.     }
  43.     while (*pl)    {
  44.         mp = elmask [(*pl) - 1];
  45.         width = hdlen(*pl++);
  46.         lw += width + 1;
  47.         if (lw >= clip)
  48.             break;
  49.         while (width--)        {
  50.             if (*mp && *mp != '_')
  51.                 putc(*mp, fd);
  52.             else if (*cp)    {
  53.                 putc(isprint(*cp) ? *cp : '?', fd);
  54.                 cp++;
  55.             }
  56.             else 
  57.                 putc(' ', fd);
  58.             if (*mp)
  59.                 mp++;
  60.         }
  61.         if (*pl)
  62.             putc(' ', fd);
  63.         cp++;
  64.     }
  65.     free(ln);
  66. }
  67.  
  68. /* ----------- test for end of page/screen --------- */
  69. void test_eop(fd, fn, pl)
  70. FILE *fd;
  71. char *fn;        /* file name */
  72. int *pl;        /* element list */
  73. {
  74.     void oflow();
  75.  
  76.     if (lct >= (isatty(fileno(fd)) ? SCRNLINES : PRNTLINES))
  77.         oflow(fd, TRUE, fn, pl);
  78. }
  79.  
  80.  
  81. /* -------------- top of page/screen ------------ */
  82. void oflow(fd, inter, fn, pl)
  83. FILE *fd;
  84. char *fn;        /* file name */
  85. int *pl;        /* element list */
  86. {
  87.     int width;
  88.     int *ll;
  89.     int ow = 0;
  90.     char msk [80];
  91.  
  92.     clip = ((!inter || isatty(fileno(fd))) ? 79 : 136);
  93.     ll = pl;
  94.     if (inter && lct < 99)    {
  95.         if (isatty(fileno(fd)))    {
  96.             printf("\n<cr> to continue...");
  97.             while (get_char() != '\r')
  98.                 ;
  99.         }
  100.         else
  101.             printf("\r\f");
  102.     }
  103.     lct = 0;
  104.     if (inter && isatty(fileno(fd)))
  105.         clear_screen();
  106.     if (inter)
  107.         fprintf(fd, "Filename: %s\n", fn);
  108.     while (*pl)    {
  109.         width = hdlen(*pl);
  110.         ow += width + 1;
  111.         if (ow >= clip)
  112.             break;
  113.         sprintf(msk, "%%-%d.%ds ", width, width);
  114.         fprintf(fd, msk, denames [(*pl++) - 1]);
  115.     }
  116.     ow = 0;
  117.     putc('\n', fd);
  118.     putc('\r', fd);
  119.     while (*ll)    {
  120.         width = hdlen(*ll++);
  121.         ow += width + 1;
  122.         if (ow >= clip)
  123.             break;
  124.         while (width--)
  125.             putc('-', fd);
  126.         putc(' ', fd);
  127.     }
  128. }
  129.  
  130. static int hdlen(el)
  131. int el;
  132. {
  133.     el--;
  134.     return strlen(elmask [el]) < strlen(denames [el]) ? 
  135.         strlen(denames [el]) : 
  136.         strlen(elmask [el]);
  137. }
  138.  
  139. /* ---------- isatty function
  140.             for the compilers that do not have it ------ */
  141. #if COMPILER == ECOC
  142. isatty(fd)
  143. {
  144.     int dev;
  145.  
  146.     dev = ioctl(fd, 0, 0);
  147.     if ((dev & 0x80) == 0)
  148.         return FALSE;
  149.     return (dev & 3);
  150. }
  151. #endif
  152.  
  153. #if COMPILER == CI_C86
  154. #include <dos.h>
  155. struct regval rg;
  156.  
  157. isatty(fd)
  158. {
  159.     rg.ax = 0x4400;
  160.     rg.bx = fd;
  161.     sysint(0x21, &rg, &rg);
  162.     if ((rg.dx & 0x80) == 0)
  163.         return FALSE;
  164.     return (rg.dx & 3);
  165. }
  166. #endif
  167.  
  168. #if COMPILER == DATALIGHT
  169. #include <dos.h>
  170. REGS rg;
  171.  
  172. int isatty(fd)
  173. int fd;
  174. {
  175.     rg.ax = 0x4400;
  176.     rg.bx = fd;
  177.     int86(0x21, &rg, &rg);
  178.     if ((rg.dx & 0x80) == 0)
  179.         return FALSE;
  180.     return (rg.dx & 3);
  181. }
  182. #endif
  183.  
  184. #if COMPILER == LETSC
  185. #include <dos.h>
  186. struct reg rg;
  187.  
  188. int isatty(fd)
  189. int fd;
  190. {
  191.     rg.r_ax = 0x4400;
  192.     rg.r_bx = fd;
  193.     intcall(&rg, &rg, 0x21);
  194.     if ((rg.r_dx & 0x80) == 0)
  195.         return FALSE;
  196.     return (rg.r_dx & 3);
  197. }
  198. #endif
  199.  
  200.  
  201. #if COMPILER == WIZARD
  202. int isatty(fd)
  203. int fd;
  204. {
  205.     int dev;
  206.  
  207.     dev = ioctl(fd, 0, 0, 0);
  208.     if ((dev & 0x80) == 0)
  209.         return FALSE;
  210.     return (dev & 3);
  211. }
  212. #endif
  213.  
  214.