home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / FIXHELP.C < prev    next >
C/C++ Source or Header  |  1994-05-02  |  6KB  |  190 lines

  1. /* ------ fixhelp.c ------ */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "htree.h"
  7. #define FIXHELP
  8. #include "helpbox.h"
  9.  
  10. static FILE *helpfp;
  11. static char hline [160];
  12.  
  13. static struct helps *FirstHelp;
  14. static struct helps *LastHelp;
  15. static struct helps *ThisHelp;
  16. static char **Argv;
  17.  
  18. static void WriteText(char *);
  19.  
  20. /* ---- compute the displayed length of a help text line --- */
  21. static int HelpLength(char *s)
  22. {
  23.     int len = strlen(s);
  24.     char *cp = strchr(s, '[');
  25.     while (cp != NULL)    {
  26.         len -= 4;
  27.         cp = strchr(cp+1, '[');
  28.     }
  29.     cp = strchr(s, '<');
  30.     while (cp != NULL)    {
  31.         char *cp1 = strchr(cp, '>');
  32.         if (cp1 != NULL)
  33.             len -= (int) (cp1-cp)+1;
  34.         cp = strchr(cp1, '<');
  35.     }
  36.     return len;
  37. }
  38.  
  39. int FindHelp(char *nm)
  40. {
  41.     int hlp = 0;
  42.     struct helps *thishelp = FirstHelp;
  43.     while (thishelp != NULL)    {
  44.         if (strcmp(nm, thishelp->hname) == 0)
  45.             break;
  46.         hlp++;
  47.         thishelp = thishelp->NextHelp;
  48.     }
  49.     return thishelp ? hlp : -1;
  50. }
  51.  
  52. main(int argc, char *argv[])
  53. {
  54.     char *cp;
  55.     int HelpCount = 0;
  56.     long where;
  57.  
  58.     if (argc < 2)
  59.         return -1;
  60.     Argv = argv;
  61.     if ((helpfp = OpenHelpFile(argv[1], "r+b")) == NULL)
  62.         return -1;
  63.  
  64.  
  65.     *hline = '\0';
  66.     while (*hline != '<')    {
  67.         if (GetHelpLine(hline) == NULL)    {
  68.             fclose(helpfp);
  69.             return -1;
  70.         }
  71.     }
  72.     while (*hline == '<')   {
  73.         if (strncmp(hline, "<end>", 5) == 0)
  74.             break;
  75.  
  76.         /* -------- parse the help window's text name ----- */
  77.         if ((cp = strchr(hline, '>')) != NULL)    {
  78.             ThisHelp = calloc(1, sizeof(struct helps));
  79.             if (FirstHelp == NULL)
  80.                 FirstHelp = ThisHelp;
  81.             *cp = '\0';
  82.             ThisHelp->hname=malloc(strlen(hline+1)+1);
  83.             strcpy(ThisHelp->hname, hline+1);
  84.  
  85.             HelpFilePosition(&ThisHelp->hptr, &ThisHelp->bit);
  86.  
  87.             if (GetHelpLine(hline) == NULL)
  88.                 break;
  89.  
  90.             /* ------- build the help linked list entry --- */
  91.             while (*hline == '[')    {
  92.                 HelpFilePosition(&ThisHelp->hptr,
  93.                                             &ThisHelp->bit);
  94.                 /* ------ parse a comment ----- */
  95.                 if (strncmp(hline, "[*]", 3) == 0)    {
  96.                     ThisHelp->comment=malloc(strlen(hline+3)+1);
  97.                     strcpy(ThisHelp->comment, hline+3);
  98.                     if (GetHelpLine(hline) == NULL)
  99.                         break;
  100.                     continue;
  101.                 }
  102.                 /* ---- parse the <<prev button pointer ---- */
  103.                 if (strncmp(hline, "[<<]", 4) == 0)    {
  104.                     char *cp = strchr(hline+4, '<');
  105.                     if (cp != NULL)    {
  106.                         char *cp1 = strchr(cp, '>');
  107.                         if (cp1 != NULL)    {
  108.                             int len = (int) (cp1-cp);
  109.                             ThisHelp->PrevName=calloc(1,len);
  110.                             strncpy(ThisHelp->PrevName,
  111.                                 cp+1,len-1);
  112.                         }
  113.                     }
  114.                     if (GetHelpLine(hline) == NULL)
  115.                         break;
  116.                     continue;
  117.                 }
  118.                 /* ---- parse the next>> button pointer ---- */
  119.                 else if (strncmp(hline, "[>>]", 4) == 0)    {
  120.                     char *cp = strchr(hline+4, '<');
  121.                     if (cp != NULL)    {
  122.                         char *cp1 = strchr(cp, '>');
  123.                         if (cp1 != NULL)    {
  124.                             int len = (int) (cp1-cp);
  125.                             ThisHelp->NextName=calloc(1,len);
  126.                             strncpy(ThisHelp->NextName,
  127.                                             cp+1,len-1);
  128.                         }
  129.                     }
  130.                     if (GetHelpLine(hline) == NULL)
  131.                         break;
  132.                     continue;
  133.                 }
  134.                 else
  135.                     break;
  136.             }
  137.             ThisHelp->hheight = 0;
  138.             ThisHelp->hwidth = 0;
  139.             ThisHelp->NextHelp = NULL;
  140.  
  141.             /* ------ append entry to the linked list ------ */
  142.             if (LastHelp != NULL)
  143.                 LastHelp->NextHelp = ThisHelp;
  144.             LastHelp = ThisHelp;
  145.             HelpCount++;
  146.         }
  147.         /* -------- move to the next <helpname> token ------ */
  148.         if (GetHelpLine(hline) == NULL)
  149.             strcpy(hline, "<end>");
  150.         while (*hline != '<')    {
  151.             ThisHelp->hwidth =
  152.                 max(ThisHelp->hwidth, HelpLength(hline));
  153.             ThisHelp->hheight++;
  154.             if (GetHelpLine(hline) == NULL)
  155.                 strcpy(hline, "<end>");
  156.         }
  157.     }
  158.     /* --- append the help structures to the file --- */
  159.     fseek(helpfp, 0L, SEEK_END);
  160.     where = ftell(helpfp);
  161.     ThisHelp = FirstHelp;
  162.     fwrite(&HelpCount, sizeof(int), 1, helpfp);
  163.     while (ThisHelp != NULL)    {
  164.         ThisHelp->nexthlp = FindHelp(ThisHelp->NextName);
  165.         ThisHelp->prevhlp = FindHelp(ThisHelp->PrevName);
  166.         WriteText(ThisHelp->hname);
  167.         WriteText(ThisHelp->comment);
  168.         fwrite(&ThisHelp->hptr, sizeof(int)*5+sizeof(long), 1, helpfp);
  169.         ThisHelp = ThisHelp->NextHelp;
  170.     }
  171.     fwrite(&where, sizeof(long), 1, helpfp);
  172.     fclose(helpfp);
  173.     return 0;
  174. }
  175.  
  176. void BuildFileName(char *fn, const char *fname, const char *ext)
  177. {
  178.     strcpy(fn, Argv[1]);
  179.     strcat(fn, ext);
  180. }
  181.  
  182. static void WriteText(char *text)
  183. {
  184.     char *np = text ? text : "";
  185.     int len = strlen(np);
  186.     fwrite(&len, sizeof(int), 1, helpfp);
  187.     if (len)
  188.         fwrite(np, len+1, 1, helpfp);
  189. }
  190.