home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / xi-21 / loadkeymap.c next >
C/C++ Source or Header  |  1996-01-30  |  3KB  |  120 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/libraries.h>
  4. #include <devices/keymap.h>
  5. #include <dos/dos.h>
  6.  
  7. #include <clib/exec_protos.h>
  8. #include <clib/utility_protos.h>
  9. #include <clib/dos_protos.h>
  10.  
  11. #include <pragmas/exec_pragmas.h>
  12. #include <pragmas/utility_pragmas.h>
  13. #include <pragmas/dos_pragmas.h>
  14.  
  15. #include "loadkeymap.h"
  16.  
  17.  
  18. /*****************************************************************************/
  19.  
  20.  
  21. extern struct Library *SysBase;
  22. extern struct Library *UtilityBase;
  23. extern struct Library *DOSBase;
  24.  
  25.  
  26. /*****************************************************************************/
  27.  
  28.  
  29. /* Case-insensitive version of FindName() */
  30. static struct Node *FindNameNC(struct List *list, STRPTR name)
  31. {
  32. struct Node *node;
  33. WORD         result;
  34.  
  35.     node = list->lh_Head;
  36.     while (node->ln_Succ)
  37.     {
  38.         result = Stricmp(name,node->ln_Name);
  39.         if (result == 0)
  40.             return(node);
  41.  
  42.         node = node->ln_Succ;
  43.     }
  44.  
  45.     return(NULL);
  46. }
  47.  
  48.  
  49. /*****************************************************************************/
  50.  
  51.  
  52. struct KeyMap *LoadKeyMap(STRPTR name)
  53. {
  54. BPTR                   segment;
  55. struct KeyMapResource *kr;
  56. struct KeyMapNode     *kn;
  57. STRPTR                 base;
  58.  
  59.     kn      = NULL;
  60.     segment = NULL;
  61.  
  62.     /* open the keymap resource, in order to gain access to the keymap list */
  63.     if (kr = (struct KeyMapResource *)OpenResource("keymap.resource"))
  64.     {
  65.         segment = NULL;
  66.         base = FilePart(name);
  67.  
  68.         /* must access the list under Forbid() */
  69.         Forbid();
  70.  
  71.         /* is the keymap we want already on the keymap list? */
  72.         if (!(kn = (struct KeyMapNode *)FindNameNC(&kr->kr_List,base)))
  73.         {
  74.             /* if not on the keymap list, try loading it */
  75.             if (segment = LoadSeg(name))
  76.             {
  77.                 /* see if someone added it to the keymap list while we were
  78.                  * doing a LoadSeg() (which broke Forbid() )
  79.                  */
  80.                 if (!(kn = (struct KeyMapNode *)FindNameNC(&kr->kr_List,base)))
  81.                 {
  82.                     kn = (struct KeyMapNode *)((segment << 2) + sizeof(BPTR));
  83.  
  84.                     /* we've loaded a keymap file. Do a few sanity checks
  85.                      * to make sure it is a keymap, and not some bogus
  86.                      * load file
  87.                      */
  88.                     if (TypeOfMem(kn->kn_Node.ln_Name)
  89.                     &&  Stricmp(name,kn->kn_Node.ln_Name))
  90.                     {
  91.                         /* add to the system's keymap list */
  92.                         AddHead(&(kr->kr_List),(struct Node *)kn);
  93.                     }
  94.                     else
  95.                     {
  96.                         /* bogus load file! Get rid of it and fail */
  97.                         UnLoadSeg(segment);
  98.                         kn = NULL;
  99.                     }
  100.                 }
  101.                 else
  102.                 {
  103.                     /* the keymap was added to the list behind our back!
  104.                      * Free what was loaded and return happily
  105.                      */
  106.                     UnLoadSeg(segment);
  107.                 }
  108.             }
  109.         }
  110.  
  111.         /* get out of forbidden state */
  112.         Permit();
  113.     }
  114.  
  115.     if (kn)
  116.         return(&kn->kn_KeyMap);
  117.  
  118.     return(NULL);
  119. }
  120.