home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 337_01 / readhlp.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  4KB  |  164 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   READHLP.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <stdio.h>
  7. #include <io.h>
  8. #include <fcntl.h>
  9.  
  10. #if defined TURBOC
  11. #include <alloc.h>
  12. #endif
  13.  
  14. #if defined QUICKC
  15.  
  16. #include <malloc.h>
  17. #include <sys\types.h>
  18.  
  19. #endif
  20.  
  21. void read_help( void)
  22. {
  23. extern struct screen_structure scr;
  24. extern struct window_structure w[];
  25. extern struct help_structure hlp;
  26.  
  27. static int in_use=FALSE;
  28. int max_pages;
  29. int page;
  30. char ch;
  31. char ext;
  32.  
  33. /* save text attribute for existing window */
  34. char old_attr=scr.current;    
  35. int  old_bold_caps=scr.bold_caps; /* save old bold caps setting */
  36.  
  37. char *buffer;
  38. char far *buffer_ptr;
  39. char far *scrn_ptr;
  40. int buff_size;
  41. int header_size;
  42. int i,j;
  43.  
  44. int handle;
  45. long offset;
  46. struct header_struct{
  47.           int width;
  48.           int height;
  49.           }hlp_header;
  50.  
  51. if(in_use)return;  /* don't invoke help if help already active */
  52. if(hlp.page==0)return; /* return if page =0 (disable help) */
  53.  
  54. in_use=TRUE;       /* now mark in_use as true */
  55.  
  56. scr.bold_caps=FALSE; /* we don't want bold caps on */
  57.  
  58. page=hlp.page-1;
  59.  
  60.  
  61. handle= open(hlp.filename,O_RDWR|O_BINARY);
  62.  
  63. if (handle==(-1)) { /* if file does not exist */
  64.  win_center (40,5,STD_FRAME," Warning: ",hlp.frame_attr,\
  65.           hlp.interior_attr);
  66.  
  67.   cls();
  68.   print(1,1,"The help file ");print_here(hlp.filename);
  69.   print_here(" is not found");
  70.   print(1,3,"Press any key to continue.");
  71.   get_key(&ch,&ext);
  72.   win_delete_top();
  73.  
  74.   /* restore the attribute previously in use */
  75.   scr.current=old_attr; 
  76.   in_use=FALSE;         /* show that help is no longer active */
  77.   return;
  78. }
  79.  
  80. /* read the header */
  81. read(handle,(char *)&hlp_header,2*sizeof(int)); 
  82. offset=lseek(handle,(long)0,SEEK_END); /* how many bytes in file? */
  83. max_pages=(offset-(long)(2*sizeof(int)))/(long)
  84.           (hlp_header.width*hlp_header.height);
  85. if (page>=max_pages) page=max_pages-1;
  86.  
  87. alt_screen(ON);
  88. win_make (hlp.x,hlp.y,hlp_header.width,hlp_header.height,
  89.           STD_FRAME,hlp.message,hlp.frame_attr,hlp.interior_attr);
  90. alt_screen(OFF);
  91. cursor(NO_CURSOR);
  92.  
  93. buff_size=sizeof(char)*(hlp_header.width*hlp_header.height);
  94. header_size=sizeof(int)*2;
  95.  
  96. buffer=NULL;
  97. /* allocate memory for the help buffer */
  98. buffer=(char*)malloc(buff_size);  
  99.  
  100. /* here we begin the loop to read pages (PgUp,PgDn, or Esc) */
  101.  for(;;){
  102.  
  103.    /* load help page into buffer */
  104.    lseek(handle,(long)(header_size+ buff_size*page),SEEK_SET);
  105.    read(handle,buffer,buff_size);
  106.  
  107.    /* copy buffer to active window */
  108.  
  109.       /* scan the help window for characters */
  110.  
  111.       buffer_ptr=buffer;    /* set temp pointer = buffer pointer */
  112.       for(i=0;i<hlp_header.height;i++){  /* get each row */
  113.  
  114.          scrn_ptr=(char far *)(scr.buffer+(scr.top+i-1)*
  115.                   (scr.columns*2)+2*(scr.left-1));
  116.  
  117.           for(j=0;j<hlp_header.width;j++){
  118.  
  119.            /* copy char from buffer to screen */
  120.            *scrn_ptr=*buffer_ptr;      
  121.             buffer_ptr++;scrn_ptr+=2; /* increment both pointers */
  122.           }
  123.        }
  124.  
  125.    get_key(&ch,&ext);
  126.  
  127.    if(ext==PGUP) page--;    /* adjust page according to user input */
  128.    if (ext==PGDN) page++;
  129.    if (ch==ESCAPE) break;
  130.     if(page>max_pages-1)
  131.      page=max_pages-1;
  132.     if(page<0) page=0;
  133.  }
  134.  
  135. /* finish up before exit */
  136.  
  137. close(handle);
  138. if(buffer!=NULL) free(buffer);
  139.  
  140. win_delete_top();
  141.  
  142. /* restore the attribute previously in use */
  143. scr.current=old_attr;          
  144. scr.bold_caps= old_bold_caps;  /* restore bold_caps setting */
  145. in_use=FALSE;                  /* show help no longer in use */
  146. }
  147.  
  148.  
  149. void get_key(char *ch, char *ext)
  150. {
  151.   for(;;){
  152.    *ch=' ';*ext=' ';
  153.  
  154.    *ch=getch();      /* get the character */
  155.  
  156.     if(!*ch){       /* if the character is zero (a special key) */
  157.       *ext=getch(); /* get the extension */
  158.       if (*ext==F1) read_help();
  159.     }
  160.  
  161.     if (*ext!=F1) break;  /* if F1 pressed we must read again */
  162.   }
  163. }
  164.