home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / dos / extra / source / browser / browser.hqx / Browser / bndl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-19  |  4.0 KB  |  173 lines

  1. #include <stdlib.h>
  2.  
  3. #include "go.h"
  4. #include "bndl.h"
  5.  
  6. #include "initicons.proto.h"
  7.  
  8. enum { HASH_SIZE = ICONTABLESIZE };
  9.  
  10. PRIVATE type_creator_link_t *hash_table[HASH_SIZE];
  11.  
  12. PRIVATE unsigned long
  13. hash_func (OSType type, OSType creator)
  14. {
  15.   unsigned long retval;
  16.  
  17.   retval = ((unsigned long) type + creator) % HASH_SIZE;
  18.   return retval;
  19. }
  20.  
  21. PRIVATE type_creator_link_t **
  22. hashpp (OSType type, OSType creator)
  23. {
  24.   type_creator_link_t **retval;
  25.  
  26.   for (retval = &hash_table[hash_func (type, creator)];
  27.        *retval && ((*retval)->type != type || (*retval)->creator != creator);
  28.        retval = &(*retval)->next)
  29.     ;
  30.   return retval;
  31. }
  32.  
  33. PRIVATE void
  34. insert_into_hash_table (OSType type, OSType creator, icn_sharp_hand icon)
  35. {
  36.   if (icon)
  37.     {
  38.       type_creator_link_t **linkpp;
  39.       
  40.       linkpp = hashpp (type, creator);
  41.       if (!*linkpp) /* currently we don't overwrite an existing match */
  42.         {
  43.           type_creator_link_t *newlink;
  44.  
  45.           newlink = malloc (sizeof (*newlink));
  46.           if (newlink)
  47.             {
  48.               newlink->type = type;
  49.               newlink->creator = creator;
  50.               newlink->icon = icon;
  51.               newlink->next = 0;
  52.               *linkpp = newlink;
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. PRIVATE bndl_section_t *
  59. extract_bndl_section (bndl_t **bndl_h, ResType code)
  60. {
  61.   bndl_section_t *retval;
  62.   int i, n_sects;
  63.  
  64.   n_sects = (*bndl_h)->n_sections_minus_1 + 1;
  65.   for (i = 0, retval = (*bndl_h)->section;
  66.        i < n_sects && retval->code != code;
  67.        ++i)
  68.     {
  69.       int n_mappings;
  70.       
  71.       n_mappings = retval->n_mappings_minus_1 + 1;
  72.       retval = (bndl_section_t *)
  73.         ((char *)retval + sizeof (bndl_section_t)
  74.                         + (n_mappings-1) * sizeof (local_mapping_t));
  75.                          /*^^^^^^^^^^^^*/
  76.                          /* we subtract one ANSI doesn't allow zero sized
  77.                             arrays */
  78.     }
  79.   if (i >= n_sects)
  80.     retval = 0;
  81.   return retval;
  82. }
  83.  
  84. /* still need read_icn_sharp */
  85.  
  86. PRIVATE fref_hand
  87. get_fref (INTEGER id)
  88. {
  89.   fref_hand retval;
  90.  
  91.   retval = (fref_hand) GetResource('FREF', id);
  92.   if (retval)
  93.     {
  94.       LoadResource ((Handle) retval);
  95.       if (GetHandleSize ((Handle) retval) < sizeof (**retval))
  96.         retval = 0;
  97.     }
  98.   return retval;
  99. }
  100.  
  101. PRIVATE icn_sharp_hand
  102. find_local_icon (INTEGER local_id, local_icn_sharp_t local_icns[],
  103.                  INTEGER n_icn)
  104. {
  105.   icn_sharp_hand retval;
  106.   int i;
  107.   
  108.   for (i = 0; i < n_icn && local_icns[i].local_id != local_id; ++i)
  109.     ;
  110.   retval = i < n_icn ? local_icns[i].icon : 0;
  111.   return retval;
  112. }
  113.  
  114. PUBLIC void
  115. process_bndl (bndl_t **bndl_h, OSType creator)
  116. {
  117.   SignedByte state;
  118.  
  119.   if (bndl_h)
  120.     {
  121.       state = HGetState ((Handle) bndl_h);
  122.       HLock ((Handle) bndl_h);
  123.       {
  124.         bndl_section_t *sectp;
  125.         int n_icn, n_fref;
  126.         local_icn_sharp_t *local_icns;
  127.         int i;
  128.     
  129.         sectp = extract_bndl_section (bndl_h, 'ICN#');
  130.         if (sectp)
  131.           {
  132.             n_icn = sectp->n_mappings_minus_1 + 1;
  133.             local_icns = malloc (n_icn * sizeof (*local_icns));
  134.             for (i = 0; i < n_icn ; ++i)
  135.               {
  136.                 local_icns[i].local_id = sectp->mapping[i].local_id;
  137.                 local_icns[i].icon = read_icn_sharp (sectp->mapping[i].resource_id);
  138.               }
  139.             sectp = extract_bndl_section (bndl_h, 'FREF');
  140.             if (sectp)
  141.               {
  142.                 n_fref = sectp->n_mappings_minus_1 + 1;
  143.                 for (i = 0; i < n_fref ; ++i)
  144.                   {
  145.                     icn_sharp_hand icon;
  146.                     fref_hand fref_h;
  147.             
  148.                     fref_h = get_fref (sectp->mapping[i].resource_id);
  149.                     if (fref_h)
  150.                       {
  151.                         icon = find_local_icon ((*fref_h)->local_id, local_icns, n_icn);
  152.                         insert_into_hash_table ((*fref_h)->type, creator, icon);
  153.                       }
  154.                   }
  155.                 }
  156.               free (local_icns);
  157.             }
  158.       }
  159.       HSetState ((Handle) bndl_h, state);
  160.     }
  161. }
  162.  
  163. PUBLIC icn_sharp_hand
  164. map_type_and_creator (OSType type, OSType creator)
  165. {
  166.   type_creator_link_t **linkpp;
  167.   icn_sharp_hand retval;
  168.  
  169.   linkpp = hashpp (type, creator);
  170.   retval = (*linkpp) ? (*linkpp)->icon : 0;
  171.   return retval;
  172. }
  173.