home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / graph / wndwboss / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-01  |  9.0 KB  |  253 lines

  1. /*
  2. ** HELP - Help driver for The Window Boss
  3. **
  4. ** Copyright (c) 1985 - Philip A. Mongelluzzo
  5. ** All rights reserved.
  6. **
  7. ** Comments:
  8. **
  9. **  Help files must be formatted properly, use GENINDEX to create the index
  10. **  file.
  11. **
  12. */
  13.  
  14. /* Header files */
  15.  
  16. #include "windows.h"
  17. #include "math.h"                       /* needed for atol */
  18.  
  19. /* Equates & definitions */
  20.  
  21. #define MAXKEY 255                      /* ISAM file length (# of keys) */
  22. #define FALSE 0                         /* lies */
  23. #define TRUE ~FALSE                     /* Science */
  24. #define RD "rb"                         /* file access mode */
  25.  
  26. #define K_PGUP 0x49                     /* a few scan codes */
  27. #define ESC    0x01
  28.  
  29. #if CI86                                /* Compiler specific stuff */
  30. #define atol atoi                       
  31. #endif
  32.  
  33. /* Function declarations */
  34.  
  35. extern FILE *fopen();                   /* Stream I/O */
  36. extern char *malloc();                  /* memory mgmt */
  37. extern char *fgets();                   /* file line input */
  38. extern char *strcpy();                  /* string copy */
  39. extern char *strcat();                  /* concat function */
  40. extern long atol();                     /* ascii to integer */
  41. extern int fseek();                     /* DASD seek */
  42. extern long ftell();                    /* where are we DASD */
  43.  
  44. static struct list {                    /* master list structure */
  45.   char *key;                            /* pointer to %info string */
  46.   long loc;                             /* file offset */
  47.   int type;                             /* type <0=toc; 1=page; 2=menu> */
  48. } master[MAXKEY];                       /* master ISAM structure */
  49.  
  50. static char *scrbuf[26];                /* screen buffer (array of ptrs) */
  51. static char line[80];                   /* line buffer */
  52. static int cpg, eoh;                    /* clear screen, end of help flags */
  53. static FILE *fp;                        /* stream file pointer */
  54.  
  55. static WINDOWPTR wn1;                   /* window pointer */
  56. static int row, col;                    /* original cursor location */
  57.  
  58. /*
  59. *************
  60. * help_init *
  61. *************
  62. */
  63.  
  64. help_init(help_file_name)               /* init help function */
  65. char *help_file_name;
  66. {
  67.   if(!help("@"))                        /* allocate the memory */
  68.     return(FALSE);                      /* return if error */
  69.   return(help(help_file_name));         /* setup help ISAM */
  70. }
  71.  
  72. /*
  73. ********
  74. * help *
  75. ********
  76. */
  77.  
  78. help(subject)                           /* help function */
  79. char *subject;                          /* help subject request */
  80. {
  81. char tmpname[30];                       /* temporary filename */
  82. char *p;                                /* scratch pointer */
  83. int i,j,k,l,m,n;                        /* scratch integers */
  84. int showpage();                         /* display page function */
  85. int showmenu();                         /* display menu function */
  86. static initflg = FALSE;                 /* init complete flag */
  87.  
  88.   switch(subject[0]) {                  /* on first char of subject ... */
  89.     case '%':                           /* subject page */
  90.       wn1 = wn_open(0, 0, 0, 78, 23, BLUE<<4 | WHITE, BLUE<<4 | WHITE);
  91.       if(!wn1) return(FALSE);           /* error return */
  92.       wn_wrap(wn1,FALSE);               /* no wrap thank you */
  93.       v_rcpos(0, &row, &col);           /* remember current location */
  94.       v_hidec();                        /* hide the physical cursor */
  95.       wn_locate(wn1,0,0);               /* home virtual cursor */
  96.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  97.       i=0;                              /* start search index */
  98.       while(i < MAXKEY) {               /* look for match in key table */
  99.         if(!strcmp(subject,master[i].key)) {
  100.           showpage(master[i].loc);      /* display all pages */
  101.           return(TRUE);                 /* return .. all is well */
  102.         }
  103.         i++;
  104.       }
  105.       wn_printf(wn1," Sorry... No info on %s\n", subject);
  106.       wn_printf(wn1," Press any key to continue...");
  107.       v_getch();
  108.       wn_close(wn1);
  109.       return(FALSE);                    /* no match !! */
  110.       break;
  111.     case '$':                           /* menu page */
  112.       break;                            /* sometime in the future */
  113.     case '@':                           /* Initialize (allocate memory) */
  114.       for(i=0; i<MAXKEY; i++) {
  115.         master[i].key = malloc(25);     /* keys can be 25 chars long */
  116.         if(!master[i].key) return(FALSE);
  117.       }
  118.       for(i=0; i<25; i++) {             /* allocate memory for screen */
  119.         scrbuf[i] =  malloc(81);        /* buffer */
  120.         if(!scrbuf[i]) return(FALSE);
  121.       }
  122.       initflg = TRUE;
  123.       return(TRUE);
  124.       break;
  125.     default:                            /* assume help filename */
  126.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  127.       strcpy(tmpname,subject);          /* form filename from root */
  128.       strcat(tmpname,".ndx");
  129.       if(!(fp = fopen(tmpname,RD))) {   /* index file cant be found ! */
  130.         printf("Can't find index file\n");
  131.         printf("Press any key to continue...");
  132.         v_getch();
  133.         return(FALSE);
  134.       }
  135.       i = 0;                            /* read and process key file */
  136.       while(fgets(line,80,fp)) {        /* build the master index table */
  137.         strcpy(master[i].key, line);
  138.         if(line[0] != '%')              /* ignore all but subects */
  139.           continue;
  140.         fgets(line,80,fp);
  141.         master[i].loc = atol(line);
  142.         i++;
  143.       }
  144.       fclose(fp);                       /* close index file */
  145.       strcpy(tmpname, subject);         /* form help file name from root */
  146.       strcat(tmpname, ".hlp");          /* open and leave open ! */
  147.       if(!(fp = fopen(tmpname, RD))) return(FALSE);
  148.       return(TRUE);
  149.       break;
  150.   }
  151. }
  152.  
  153. /*
  154. ************
  155. * showpage *
  156. ************
  157. */
  158.  
  159. static showpage(offset)                 /* display page function */
  160. long offset;
  161. {
  162. int i,j;                                /* scratch integers */
  163. long pages[MAXKEY];                     /* stack of pages */
  164. int pndx;                               /* stack pointer */
  165. int keyflg;                             /* vaild key flag */
  166.  
  167.   pndx = 0;                             /* init stack pointer */
  168.   pages[0] = offset;                    /* initial page */
  169.  
  170.   while(TRUE) {                         /* till forever ... */
  171.     i = filbuf(offset);                 /* fill the buffer */
  172.     for(j=0; j<i; j++)                  /* display buffer */
  173.       wn_puts(wn1,j,0,scrbuf[j]);
  174.     wn1->style |= BOLD;                 /* toggle bold on */
  175.     if(eoh) 
  176.       wn_puts(wn1, 22, 0, " End of help, PgUp for previous screen, any other key to continue...");
  177.     else
  178.       wn_puts(wn1, 22, 0, " Esc to quit help, PgUp for previous screen, any other key to continue...");
  179.     wn1->style ^= BOLD;                 /* toggle bold off */
  180.     keyflg = FALSE;                     /* valid key flag */
  181.     do {                                /* loop till vaild key */
  182.       switch (v_getch() >> 8) {         /* based on user input */
  183.         case K_PGUP:                    /* do all the right things */
  184.           pndx--;
  185.           if(pndx <0) {
  186.             wn_close(wn1);
  187.             v_locate(0, row, col);
  188.             return;
  189.           }
  190.           offset = pages[pndx];
  191.           keyflg = TRUE;
  192.           eoh = FALSE;                  /* force clear screen */
  193.           cpg = TRUE;                   /* and cancel end of help flag */
  194.           break;
  195.         case ESC:
  196.           wn_close(wn1);
  197.           v_locate(0, row, col);
  198.           return;
  199.         default:
  200.           offset = pages[++pndx] = ftell(fp);
  201.           keyflg = TRUE;
  202.           break;
  203.       }
  204.     } while (!keyflg);                  /* loop till valid key */
  205.     if(cpg) wn_clr(wn1);
  206.     if(eoh) {
  207.       wn_close(wn1);
  208.       v_locate(0, row, col);
  209.       return;
  210.     }
  211.   }
  212. }
  213.  
  214. /*
  215. **********
  216. * filbuf *
  217. **********
  218. */
  219. static filbuf(offset)                   /* fill screen buffer */
  220. long offset;
  221. {
  222. int i,j;
  223. char *p, *p1;
  224.  
  225.   i=0;                                  /* initialize */
  226.   cpg = eoh = FALSE;                    /* assume nothing */
  227.   fseek(fp, offset, 0);                 /* position file */
  228.   do {
  229.     fgets(line, 80, fp);                /* fetch a line */
  230.       cpg = !strcmp(".cp\r\n", line);   /* lite clear screen and */
  231.       eoh = !strcmp("*END*\r\n",line);  /* end of help flags */
  232.       if(cpg || eoh) break;
  233.       p = &line[0];                     /* a long way around to */
  234.       p1 = scrbuf[i];                   /* filter the carriage */
  235.       while (*p) {                      /* returns !! */
  236.         switch (*p) {
  237.           case '\r':
  238.           case '\n':
  239.             p++;
  240.             break;
  241.           default:
  242.             *p1++ = *p++;
  243.             break;
  244.         }
  245.       }
  246.       *p1 = NULL;
  247.       i++;
  248.   } while (TRUE);
  249.   return(i);                            /* return the # of lines on page */
  250. }
  251.  
  252. /* End */
  253.