home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / progjour / 1990 / 05 / resource.c < prev    next >
C/C++ Source or Header  |  1990-06-15  |  4KB  |  238 lines

  1. /* resource.c    support routines for string resources
  2.  *
  3.  *    author: vaughn vernon
  4.  *
  5.  *    (c) 1990 Aspen Scientific
  6.  *    All Rights Reserved
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #ifdef __TURBOC__
  12. #include <alloc.h>
  13. #else
  14. #include <malloc.h>
  15. #endif
  16.  
  17. #include "resource.h"
  18.  
  19. static int        lineEatSpace(unsigned char *fbuf);
  20. static StringResource *    makeResource(unsigned char *fbuf);
  21. static void        freeResource(StringResource *res);
  22.  
  23. /* LoadResources() loads a resource file into memory.
  24.  *
  25.  * returns a resource list pointer, or NULL if an error is found.
  26.  */
  27. StringResource *
  28. LoadResources(char *fileName)
  29. {
  30.     FILE        *fp = fopen(fileName, "r");
  31.     unsigned char    fbuf[BUFSIZ];
  32.     StringResource    *res = NULL, *cur, *tmp;
  33.  
  34.     if (fp == NULL)
  35.         return (NULL);
  36.  
  37.     while (fgets(fbuf, sizeof (fbuf), fp)) {
  38.  
  39.         if (lineEatSpace(fbuf) == 0)
  40.             continue;
  41.  
  42.         tmp = makeResource(fbuf);
  43.  
  44.         if (tmp == NULL) {
  45.             FreeResources(res);
  46.             return (NULL);
  47.         }
  48.  
  49.         if (res == NULL)
  50.             res = cur = tmp;
  51.         else {
  52.             cur->next = tmp;
  53.             cur = tmp;
  54.         }
  55.     }
  56.  
  57.     fclose(fp);
  58.     return (res);
  59. }
  60.  
  61. /* makeResource() creates a resource link on the free store.
  62.  *
  63.  * returns a pointer to the new link.
  64.  */
  65. StringResource *
  66. makeResource(unsigned char *fbuf)
  67. {
  68.     StringResource    *res;
  69.     unsigned char    *idName, *idValue, *string, *end;
  70.  
  71.     res = (StringResource *) malloc(sizeof (StringResource));
  72.  
  73.     if (res == NULL)
  74.         return (NULL);
  75.  
  76.     res->idName = res->string = NULL;
  77.  
  78.     /* parse the line... */
  79.  
  80.     idName = fbuf;
  81.     idValue = strchr(fbuf, '=');
  82.  
  83.     if (idName == NULL || idValue == NULL) {
  84.         freeResource(res);
  85.         return (NULL);
  86.     }
  87.  
  88.     *idValue++ = '\0';
  89.  
  90.     string = strchr(idValue, ',');
  91.  
  92.     if (string == NULL) {
  93.         freeResource(res);
  94.         return (NULL);
  95.     }
  96.  
  97.     *string++ = '\0';
  98.     if (*string != '\"') {
  99.         freeResource(res);
  100.         return (NULL);
  101.     }
  102.     ++string;
  103.     end = strrchr(string, '\"');
  104.  
  105.     if (end == NULL) {
  106.         freeResource(res);
  107.         return (NULL);
  108.     }
  109.  
  110.     *end = '\0';
  111.  
  112.     /* set up the link ... */
  113.     res->idName = (unsigned char *) malloc(strlen(idName) + 1);
  114.     res->string = (unsigned char *) malloc(strlen(string) + 1);
  115.  
  116.     if (res->idName == NULL || res->string == NULL) {
  117.         freeResource(res);
  118.         return (NULL);
  119.     }
  120.  
  121.     strcpy(res->idName, idName);
  122.     strcpy(res->string, string);
  123.  
  124.     res->value = (unsigned) atoi(idValue);
  125.     res->magic = _ResourceMagic;
  126.     res->next  = NULL;
  127.  
  128.     return (res);
  129. }
  130.  
  131. /* lineEatSpace() is your basic text line parser.
  132.  *
  133.  * returns number of "useable" characters; 0 if all white or comments.
  134.  */
  135. static int
  136. lineEatSpace(unsigned char *fbuf)
  137. {
  138.     register int    quote=0;
  139.     unsigned char    tmp[BUFSIZ+1];
  140.     unsigned char    *p1 = fbuf, *p2 = tmp;
  141.  
  142.     memset(tmp, 0, sizeof (tmp));
  143.  
  144.     while (*p1 && *p1 != '\n') {
  145.  
  146.         switch (*p1)
  147.         {
  148.         case    '#':
  149.             *p1 = '\0';
  150.             break;
  151.  
  152.         case    '\t':
  153.             ++p1;
  154.             continue;
  155.  
  156.         case    ' ':
  157.             if (!quote) {
  158.                 ++p1;
  159.                 continue;
  160.             }
  161.  
  162.             break;
  163.  
  164.         case    '\\':
  165.             ++p1;
  166.             break;
  167.  
  168.         case    '\'':
  169.             quote = (quote == 1 ? 0: quote == 2 ? 2:1);
  170.             break;
  171.  
  172.         case    '\"':
  173.             quote = (quote == 2 ? 0: quote == 1 ? 1:2);
  174.             break;
  175.  
  176.         default:
  177.             break;
  178.         }
  179.  
  180.         if (*p1 == '\0')
  181.             break;
  182.  
  183.         *p2++ = *p1++;
  184.     }
  185.  
  186.     memcpy(fbuf, tmp, BUFSIZ);
  187.  
  188.     return (strlen(fbuf));
  189. }
  190.  
  191. /* FreeResources() frees an entire resource list
  192.  */
  193. void
  194. FreeResources(StringResource *res)
  195. {
  196.     StringResource    *tmp;
  197.  
  198.     while (res) {
  199.         tmp = res->next;
  200.         freeResource(res);
  201.         res = tmp;
  202.     }
  203. }
  204.  
  205. /* freeResource() frees an individual resource
  206.  */
  207. void
  208. freeResource(StringResource *res)
  209. {
  210.     if (res == NULL)
  211.         return;
  212.  
  213.     if (res->idName)
  214.         free( res->idName );
  215.     if (res->string)
  216.         free( res->string );
  217.  
  218.     res->magic = 0;    /* no reuse */
  219.  
  220.     free( res );
  221. }
  222.  
  223. /* LookUpResource() finds a resource matching the given ID.
  224.  *
  225.  * returns the resource pointer, or NULL if not found.
  226.  */
  227. StringResource *
  228. LookUpResource(StringResource *res, unsigned id)
  229. {
  230.     while (res && res->magic == _ResourceMagic) {
  231.         if (res->value == id)
  232.             break;
  233.         res = res->next;
  234.     }
  235.  
  236.     return (res && res->magic == _ResourceMagic ? res:NULL);
  237. }
  238.