home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_01 / v7n1094a.txt < prev    next >
Text File  |  1988-10-10  |  4KB  |  189 lines

  1. /*
  2.  *  browse.c - Very simple file browser.
  3.  *
  4.  *  Syntax:
  5.  *      browse file1 file2 ...
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. #define MAX_LINE    (80)
  11. #define PAGE_SIZE   (20)
  12.  
  13.  
  14.     main(argc, argv)
  15. char    **argv;
  16. {
  17. int     i, c;
  18. FILE    *fin;
  19.  
  20.     s_init();       /* initialize screen package */
  21.  
  22.     for(i = 1; i < argc; ++i)
  23.         if((fin=fopen(argv[i], "r")) == NULL)
  24.             {
  25.             c   = toupper(
  26.                 pause( "Can't open file for input. Continue (Y/n)? ", "yYnN" )
  27.                          );
  28.             if(c == 'N')
  29.                 break;
  30.             }
  31.         else if(!browse(fin))
  32.             break;
  33.  
  34.     s_fini();       /* wrap up screen package */
  35. }
  36.  
  37. int     browse(fin)
  38. FILE    *fin;
  39. {
  40. char    line[MAX_LINE];
  41. int     c, nlines = 1, eof = 0, addlines = PAGE_SIZE;
  42.  
  43.     s_clear();  /* clear screen */
  44.  
  45.     for(;;)
  46.         {
  47.         if(addlines)    /* wants to see more    */
  48.             if(eof)
  49.                 putchar( 7 );   /* beep */
  50.             else
  51.                 {
  52.                 while(fgets(line, MAX_LINE, fin))
  53.                     {
  54.                     if(nlines > PAGE_SIZE)
  55.                         s_scroll();
  56.                     else
  57.                         ++nlines;
  58.                     s_move(nlines-1, 0);
  59.                     s_puts(line);
  60.                     if(--addlines <= 0)
  61.                         break;
  62.                     }
  63.                 if(addlines)    /* hit end of file  */
  64.                     {
  65.                     eof = 1, addlines = 0;
  66.                     s_hilite(1);
  67.                     s_puts("End-of-File");
  68.                     s_hilite(0);
  69.                     }
  70.                 }
  71.  
  72.         c   = pause("BROWSE: next File, Page, Line, or Quit (F,P,L,Q)? ",
  73.                     "fFpPlLqQ");
  74.         c   = toupper(c);
  75.         if(c == 'F')
  76.             {
  77.             fclose(fin);
  78.             return( 1 );
  79.             }
  80.         else if(c == 'P')
  81.             addlines    = PAGE_SIZE;
  82.         else if(c == 'L')
  83.             addlines    = 1;
  84.         else if(c == 'Q')
  85.             {
  86.             fclose(fin);
  87.             return( 0 );
  88.             }
  89.         else
  90.             putchar( 7 );   /* beep */
  91.         }
  92. }
  93.  
  94. int     pause(prompt, chars)
  95. char    *prompt, *chars;
  96. {
  97. int     c, row, col;
  98.  
  99.     s_getpos(&row, &col);
  100.  
  101.     s_move(0, 0);
  102.     s_hilite(1);
  103.     s_puts(prompt);
  104.     s_flush();
  105.     while( !strchr(chars, (c=s_getc())) )
  106.         putchar(7); /* beep */
  107.     s_hilite(0);
  108.     s_move(row, col);
  109.     return( c );
  110. }
  111.  
  112. #ifdef __ZTC__
  113. #include <disp.h>
  114. /*
  115.  *  Most of these could be macros, if efficiency were paramount.
  116.  */
  117. int     s_init()    { disp_open(); }
  118. int     s_fini()    { disp_close(); }
  119. int     s_move(r,c) { disp_move(r,c); }
  120. int     s_flush()   { disp_flush(); }
  121. int     s_scroll()  { disp_scroll(1,0,0,PAGE_SIZE,79,DISP_NORMAL); }
  122. int     s_getc()    { return(getch()); }
  123.  
  124. int     s_puts(s)
  125. char    *s;
  126. {
  127. disp_printf("%s", s);
  128. }
  129.  
  130. int     s_getpos(rowp, colp)
  131. int     *rowp, *colp;
  132. {
  133. extern  int     disp_cursorrow, disp_cursorcol;
  134.  
  135.     *rowp   = disp_cursorrow;
  136.     *colp   = disp_cursorcol;
  137. }
  138.  
  139. int     s_hilite(on)
  140. {
  141.     if(on)
  142.         disp_setattr(DISP_REVERSEVIDEO);
  143.     else
  144.         disp_setattr(DISP_NORMAL);
  145. }
  146.  
  147. int     s_clear()
  148. {
  149.     disp_move(0, 0);
  150.     disp_eeop();
  151. }
  152. #else
  153. #include <curses.h>
  154. /*
  155.  *  Most of these could be macros, if efficiency were paramount.
  156.  */
  157. int     s_init()    { initscr(); noecho(); raw(); scrollok(stdscr, 1); }
  158. int     s_fini()    { endwin(); }
  159. int     s_move(r,c) { move(r,c); }
  160. int     s_flush()   { refresh(); }
  161. int     s_scroll()  { scroll(stdscr); }
  162. int     s_getc()    { return(getch()); }
  163.  
  164. int     s_puts(s)
  165. char    *s;
  166. {
  167. printw("%s", s);
  168. }
  169.  
  170. int     s_getpos(rowp, colp)
  171. int     *rowp, *colp;
  172. {
  173.     getyx(stdscr, *rowp, *colp);
  174. }
  175.  
  176. int     s_hilite(on)
  177. {
  178.     if(on)
  179.         standout();
  180.     else
  181.         standend();
  182. }
  183.  
  184. int     s_clear()
  185. {
  186.     clear();
  187. }
  188. #endif
  189.