home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / Handlers.c < prev    next >
C/C++ Source or Header  |  1997-03-21  |  9KB  |  239 lines

  1. /* @(#)Z 1.4 com/src/cm/Handlers.c, odstorage, od96os2, odos29712d 97/03/21 17:19:37 (96/10/29 09:18:46) */
  2. /*====START_GENERATED_PROLOG======================================
  3.  */
  4. /*
  5.  *   COMPONENT_NAME: odstorage
  6.  *
  7.  *   CLASSES: none
  8.  *
  9.  *   ORIGINS: 82,27
  10.  *
  11.  *
  12.  *   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  13.  *   All Rights Reserved
  14.  *   Licensed Materials - Property of IBM
  15.  *   US Government Users Restricted Rights - Use, duplication or
  16.  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  17.  *       
  18.  *   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19.  *   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20.  *   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21.  *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  22.  *   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  23.  *   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  24.  *   OR PERFORMANCE OF THIS SOFTWARE.
  25.  */
  26. /*====END_GENERATED_PROLOG========================================
  27.  */
  28.  
  29. /*
  30.     File:        Handlers.c
  31.  
  32.     Contains:    Container Manager Handlers Routines
  33.  
  34.     Written by:    Ira L. Ruben
  35.  
  36.     Owned by:    Ed Lai
  37.  
  38.     Copyright:    ⌐ 1991-1994 by Apple Computer, Inc., all rights reserved.
  39.  
  40.     Change History (most recent first):
  41.  
  42.          <2>     8/26/94    EL        #1182319 Don't allocate memory for looking
  43.                                     up symbol
  44.          <2>      2/3/94    EL        Bento File names are now eight chars or
  45.                                     less.
  46.  
  47.     To Do:
  48. */
  49.  
  50. /*---------------------------------------------------------------------------*
  51.  |                                                                           |
  52.  |                            <<< Handlers.c >>>                             |
  53.  |                                                                           |
  54.  |                    Container Manager Handlers Routines                    |
  55.  |                                                                           |
  56.  |                               Ira L. Ruben                                |
  57.  |                                 11/18/91                                  |
  58.  |                                                                           |
  59.  |                  Copyright Apple Computer, Inc. 1991-1994                 |
  60.  |                           All rights reserved.                            |
  61.  |                                                                           |
  62.  *---------------------------------------------------------------------------*
  63.  
  64.  The routines in this file maintain handler/type name associations.  Handlers are defined
  65.  in terms of their type.  They may be retrieved by using that type.
  66. */
  67.  
  68.  
  69. #include <stddef.h>
  70. #include <string.h>
  71. #include <stdio.h>
  72.  
  73. #ifndef __CMTYPES__
  74. #include "CMTypes.h"
  75. #endif
  76. #ifndef __CM_API__
  77. #include "CMAPI.h"
  78. #endif
  79. #ifndef __SYMMGR__
  80. #include "SymTbMgr.h"      
  81. #endif
  82. #ifndef __SESSIONDATA__
  83. #include "Session.h"          
  84. #endif
  85. #ifndef __HANDLERS__
  86. #include "Handlers.h"
  87. #endif
  88. #ifndef __UTILITYROUTINES__
  89. #include "Utility.h"        
  90. #endif
  91.  
  92.                                                                     CM_CFUNCTIONS
  93.  
  94. /* The following generates a segment directive for Mac only due to 32K Jump Table             */
  95. /* Limitations.  If you don't know what I'm talking about don't worry about it.  The        */
  96. /* default is not to generate the pragma.  Theoritically unknown pragmas in ANSI won't    */
  97. /* choke compilers that don't recognize them.  Besides why would they be looked at if        */
  98. /* it's being conditionally skipped over anyway?  I know, famous last words!                        */
  99.  
  100. #if CM_MPW
  101. #pragma segment Handlers
  102. #endif
  103.  
  104.  
  105. /*-----------------------------------------------------------------*
  106.  | enterCompare - type name comparison routine for cmEnterSymbol() |
  107.  *-----------------------------------------------------------------*
  108.  
  109.  This routine is "sent" to cmEnterSymbol() to do the proper comparisons for handlers.  The
  110.  handler tree is based on their type names.  cmEnterSymbol() is a generic binary tree
  111.  routine that requires the comparsion to be supplied by its caller to decide on what
  112.  basis the tree is build.  Hence this routine!
  113.  
  114.  Note, this "static" is intentionally left to default memory model under DOS since it is
  115.  passed as a function pointer to cmEnterSymbol().
  116. */
  117.  
  118. static int enterCompare(const void *h1, const void *h2)
  119. {
  120.     return (strcmp((CM_CHAR *)((MetaHandlerPtr)h1)->typeName,
  121.                                  (CM_CHAR *)((MetaHandlerPtr)h2)->typeName));
  122. }
  123.  
  124. /*-------------------------------------------------------------------*
  125.  | lookupCompare - type name comparison routine for cmLookupSymbol() |
  126.  *-------------------------------------------------------------------*
  127.  
  128.  This routine is "sent" to cmLookupSymbol() to do the proper comparisons for global names.
  129.  The global name tree is based on the names (obviously).  cmLookupSymbol() is a generic
  130.  binary tree routine that requires the comparsion to be supplied by its caller to decide
  131.  on what basis the tree is build.  Hence this routine!
  132.  
  133.  Note, this "static" is intentionally left to default memory model under DOS since it is
  134.  passed as a function pointer to cmEnterSymbol().
  135. */
  136.  
  137. static int lookupCompare(const void *h1, const CM_UCHAR *name)
  138. {
  139.     return (strcmp((CM_CHAR *)((MetaHandlerPtr)h1)->typeName, (CM_CHAR *)name));
  140. }
  141.  
  142.  
  143. /*--------------------------------------------------*
  144.  | cmDefineMetaHandler - define a new "metahandler" |
  145.  *--------------------------------------------------*
  146.  
  147.  Define a new metahandler with the specifed type (a C string). The function returns a
  148.  pointer to the new handler or, if dup is true, a pointer to a previously defined entry.
  149.  
  150.  If NULL is returned then there was an allocation failure and the new type could not be
  151.  created.
  152.  
  153.  Note, the global data session pointer created by CMStartSession() is passed since
  154.  methandlers are global to all containers and thus the root of the metahandler symbol
  155.  table is kept as part of the session data.
  156. */
  157.  
  158. MetaHandlerPtr cmDefineMetaHandler(CMMetaHandler metaHandler, 
  159.                                                                       const CM_UCHAR *typeName,
  160.                                                                         CMBoolean *dup,
  161.                                                                      SessionGlobalDataPtr sessionData)
  162. {
  163.     MetaHandlerPtr h, newMetaHandler;
  164.     
  165.     if ((newMetaHandler = (MetaHandlerPtr)SessionMalloc(sizeof(MetaHandler) + strlen((CM_CHAR *)typeName))) != NULL) {
  166.         strcpy((CM_CHAR *)newMetaHandler->typeName, (CM_CHAR *)typeName);    /* fill in new entry        */
  167.         newMetaHandler->metaHandler = metaHandler;
  168.         
  169.         h = (MetaHandlerPtr)cmEnterSymbol(newMetaHandler, (void **)&SessionMetaHandlerTable, dup, enterCompare);
  170.         
  171.         if (*dup) SessionFree(newMetaHandler);                                            /* oops!                                */
  172.     } else {
  173.         h = NULL;
  174.         *dup = false;
  175.     }
  176.     
  177.     return (h);                                                                                                        /* return entry ptr            */
  178. }
  179.  
  180.  
  181. /*---------------------------------------------------------------*
  182.  | cmLookupMetaHandler - find a previously defined "metahandler" |
  183.  *---------------------------------------------------------------*
  184.  
  185.  Find a metahandler associated with the specified type.  If found, the HandlerPtr to the
  186.  found entry is returned (which includes the handler proc and type).  If not found NULL
  187.  is returned.
  188.  
  189.  Note, we allocate a temporary handler table entry here and then free it.   If the
  190.  allocation fails, SessionSuccess, a session status switch, is returned false.  Otherwise
  191.  SessionSuccess is true.
  192.     
  193.  We used to share the same compare routine for lookup and enter symbol, but that
  194.  involves allocation of temporary memory. That is not very inefficient. So now we
  195.  have separate compare routine for lookup and enter symbol to elimiate the temporary
  196.  memory allocation.
  197. */
  198.  
  199. MetaHandlerPtr cmLookupMetaHandler(const CM_UCHAR *typeName,
  200.                                                                      SessionGlobalDataPtr sessionData)
  201. {
  202.     sessionData->success = true;
  203.     return (MetaHandlerPtr)(cmLookupSymbol(typeName, SessionMetaHandlerTable, lookupCompare));
  204. }
  205.  
  206.  
  207. /*-------------------------------------------------------------*
  208.  | cmForEachMetaHandler - do some action on each "metahandler" |
  209.  *-------------------------------------------------------------*
  210.  
  211.  Do (call) the specified action for each defined metahandler in the current session. The
  212.  pointer to each metahandler entry is passed to the action routine along with a "refCon"
  213.  which the caller can use as a communication facility to convey additional info to the
  214.  action routine.
  215. */
  216.  
  217. void cmForEachMetaHandler(CMRefCon refCon, 
  218.                                                     void (*action)(MetaHandlerPtr aHandler, CMRefCon refCon),
  219.                                                     SessionGlobalDataPtr sessionData)    
  220. {
  221.     cmForEachSymbol(SessionMetaHandlerTable, refCon, (SymbolAction)action);
  222. }
  223.  
  224.  
  225. /*-------------------------------------------------*
  226.  | cmFreeAllMetaHandlers - free all "metahandlers" |
  227.  *-------------------------------------------------*
  228.  
  229.  This routine is called to remove the definitions of ALL previously defined metahandlers
  230.  for the current session.
  231. */
  232.  
  233. void cmFreeAllMetaHandlers(SessionGlobalDataPtr sessionData)
  234. {
  235.     cmFreeAllSymbols((void **)&SessionMetaHandlerTable, sessionData); /* just glue code        */
  236. }
  237.                                                           
  238.                                                             CM_END_CFUNCTIONS
  239.