home *** CD-ROM | disk | FTP | other *** search
/ WordPerfect for Linux Bible / WP4LinuxBible.iso / sdk / wpx / code / wt / client / wtcltool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  6.0 KB  |  210 lines

  1. /* SOURCE FILE *****************************************************
  2.  * WTCLTOOL.C  -  WTAPI Sample Client Application Tools Menu Handler
  3.  *******************************************************************
  4.  *Copyright (C) 1993 WordPerfect Corp., All Rights Reserved
  5.  *******************************************************************/
  6. #include "wtclient.h"
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. #define WRITING        "Writing"
  12. #define TOOLS        "Tools"
  13. #define PATHEXE        "PathExe"
  14. #define MENUNAME    "MenuName"
  15. #define DESCNAME    "DescriptiveName"
  16. #define PARAMETERS    "Parameters"
  17. #define    LONGPROMPT    "LongPrompt"
  18. #define BTNBARTEXT    "ButtonBarText"
  19. #define BITMAPPATH    "BitmapPath"
  20. #define HELPFILE    "HelpFilePath"
  21. #define HELPKEY        "HelpFileKey"
  22.  
  23. static BOOL GetToolInfo(FILE *, TOOLRECORD *);
  24.  
  25. /*COMMENT***************************************************
  26. ;GetToolsInfo
  27. Title:    Get information for all tools in the wtapi.rc file
  28. In:        none
  29. Out:    tools - list of tool information from file
  30.         count - number of tools in list
  31. Xin:    none
  32. Xout:    none
  33. Return:    TRUE - okay, FALSE - failed to read tool information from file
  34. Notes:
  35. ***********************************************************/
  36. BOOL GetToolsInfo(TOOLRECORD *tools, int *count)
  37. {
  38.     FILE *fp;                    /* pointer to wtapi.rc file */
  39.     unsigned char text[256];    /* last string read from file */
  40.     int toolcnt = 0;            /* number of tools found in the file */
  41.     unsigned char found = 0;    /* 1 = found WRITING, 2 = found TOOLS also */
  42.     unsigned char *p;
  43.     int len;
  44.     long filepos;
  45.  
  46.     *count = 0;        /* initialize count */
  47.     fp = fopen("wtapi.rc", "r");
  48.     if (!fp) {
  49.         return FALSE;
  50.     }
  51.     /*
  52.      * Search for [WRITING TOOLS] header.
  53.      */
  54.     text[0] = NULL;
  55.     while (fscanf(fp, " %s ", text) != EOF) {
  56.         len = strlen(text);
  57.         if (text[len-1] == ']') {
  58.             text[len-1] = 0;
  59.         }
  60.         if ((found == 0) && (text[0] != '[')) {
  61.             continue;
  62.         } else if (strcmp(text+1, WRITING) == 0) {
  63.             found = 1;
  64.         } else if ((found == 1) && (strcmp(text, TOOLS) == 0)) {
  65.             found = 2;
  66.             break;
  67.         } else {
  68.             found = 0;        /* TOOLS must be next text after WRITING */
  69.         }
  70.     }
  71.     if (found != 2) {
  72.         return FALSE;        /* no tools found */
  73.     }
  74.     /*
  75.      * Build list of tools from file (convert "tool1=<optional text>" to
  76.      * "tool1").  Search for the information for each tool and retrieve it
  77.      * if found.  If not found, do not include that tool.
  78.      */
  79.     while ((fscanf(fp, " %s ", text) != EOF) && (text[0] != '[') &&
  80.             (toolcnt < MAXTOOLS))
  81.     {
  82.         /* find the next tool in the list */
  83.         if (p = strchr(text, '=')) {    /* should contain a '=' */
  84.             *p = NULL;                    /* truncate at '=' */
  85.             len = strlen(text);
  86.             strcpy(tools[toolcnt].tool, text);
  87.             /*
  88.              * Search for tool info in file.
  89.              */
  90.             filepos = ftell(fp);            /* remember where we are */
  91.             if (GetToolInfo(fp, &tools[toolcnt])) {
  92.                 toolcnt++;
  93.             }
  94.             fseek(fp, filepos, 0);        /* return to previous position */
  95.         }
  96.     }
  97.     *count = toolcnt;
  98.     fclose(fp);
  99.     return TRUE;
  100. } /* GetToolsInfo */
  101.  
  102. /*COMMENT***************************************************
  103. ;GetToolInfo
  104. Title:    Fill in tool record info for a single tool from the file.
  105. In:        fp - pointer to wtapi.rc file
  106. Out:    tool - pointer to tool record where tool info is to be put
  107. Xin:    none
  108. Xout:    none
  109. Return:    TRUE - okay, FALSE - failed to read tool information from file
  110. Notes:    The tool member of the TOOLRECORD structure should already contain
  111.         the name of the tool to search for.
  112. ***********************************************************/
  113. static BOOL GetToolInfo(FILE *fp, TOOLRECORD *tool)
  114. {
  115.     unsigned char *target = tool->tool;
  116.     unsigned char text[256];    /* last string read from file */
  117.     unsigned char *product = 0;    /* last product (tool) header read from file */    
  118.     unsigned char found = 0;    /* 0 = not found, 1 = found product */
  119.     unsigned char *left = 0;    /* left side of left=right pair */
  120.     unsigned char *right = 0;    /* right side of left=right pair */
  121.     unsigned char *curr = 0;    /* pointer to parameter currently being set */
  122.     unsigned char *p = 0;
  123.     int len;
  124.  
  125.     if (!*target) {
  126.         return FALSE;    /* no target tool specified */
  127.     }
  128.     /*
  129.      * Search to the beginning of the entries for the specified product.
  130.      * e.g. [WPSpeller]
  131.      */
  132.     text[0] = NULL;
  133.     while (fscanf(fp, " %s ", text) != EOF) {
  134.         len = strlen(text);
  135.         if ((text[0] != '[') || (text[len-1] != ']')) {
  136.             continue;
  137.         }
  138.         product = text + 1;        /* skip '[' */
  139.         text[len-1] = 0;        /* delete final ']' */
  140.         if (strcmp(product, target) == 0) {
  141.             found = 1;
  142.             break;
  143.         }
  144.     }
  145.     if (found != 1) {
  146.         return FALSE;        /* failed to find target product */
  147.     }
  148.     /*
  149.      * We are positioned at the info for the specified target product.
  150.      * Read in the information and store it in the tool record.
  151.      * Null out any missing parameters.
  152.      */
  153.     while ((fscanf(fp, " %s ", text) != EOF) && (text[0] != '[')) {
  154.         if ((p = strchr(text, '=')) != 0) {
  155.             left = text;
  156.             *p = NULL;
  157.             right = p + 1;
  158.             if (strcmp(left, PATHEXE) == 0) {
  159.                 strcpy(tool->exe, right);
  160.                 curr = tool->exe;
  161.             } else if (strcmp(left, MENUNAME) == 0) {
  162.                 /* Use '&' to locate mnemonic then delete '&' */
  163.                 if ((p = strchr(right, '&')) != 0) {
  164.                     tool->mne = *(p+1);        /* next char is mnemonic */
  165.                     strcpy(p, p+1);        /* shift later chars left */
  166.                 } else {
  167.                     tool->mne = 0;
  168.                 }
  169.                 strcpy(tool->menu, right);
  170.                 curr = tool->menu;
  171.             } else if (strcmp(left, DESCNAME) == 0) {
  172.                 strcpy(tool->desc, right);
  173.                 curr = tool->desc;
  174.             } else if (strcmp(left, PARAMETERS) == 0) {
  175.                 strcpy(tool->params, right);
  176.                 curr = tool->params;
  177.             } else if (strcmp(left, LONGPROMPT) == 0) {
  178.                 strcpy(tool->prompt, right);
  179.                 curr = tool->prompt;
  180.             } else if (strcmp(left, BTNBARTEXT) == 0) {
  181.                 /* ignore */
  182.                 curr = 0;
  183.             } else if (strcmp(left, BITMAPPATH) == 0) {
  184.                 /* ignore */
  185.                 curr = 0;
  186.             } else if (strcmp(left, HELPFILE) == 0) {
  187.                 /* ignore */
  188.                 curr = 0;
  189.             } else if (strcmp(left, HELPKEY) == 0) {
  190.                 /* ignore */
  191.                 curr = 0;
  192.             } else {
  193.                 /* ignore */
  194.                 curr = 0;
  195.             }
  196.         }
  197.         /*
  198.          * Assume any text not containing '=' belongs to the preceding
  199.          * parameter.
  200.          */
  201.         else {
  202.             if (curr) {
  203.                 strcat(curr, " ");    /* use space in place of newline */
  204.                 strcat(curr, text);
  205.             }
  206.         }
  207.     }
  208.     return TRUE;
  209. } /* GetToolInfo */
  210.