home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / prctclxf.sit / Sources / ArrayList / Sources / ListGlobals.c < prev    next >
Text File  |  1990-08-27  |  4KB  |  139 lines

  1. /*
  2. Copyright ⌐ 1990 Ari I. Halberstadt
  3.  
  4. This file is part of a free program. For precise terms of distribution
  5. and use please see the files in the folder ╥Legal╙ accompanying this
  6. software. If you make any modifications to this file you must add the
  7. date, your name, and a description of what you have done to the
  8. revision history below. This notice and the copyright notice must
  9. remain intact in all copies of this file. The files in the folder
  10. ╥Legal╙ must be available to anyone receiving copies of this file.
  11.  
  12. Add initials and name here:
  13. Initials    Name
  14. AIH        Ari I. Halberstadt
  15.  
  16. Revision History (date format is mm-dd-yy):
  17. Date      Name  Comments
  18. 07-11-90     AIH   Version 0.9
  19. */
  20.  
  21. /*
  22.     Functions for managing the global data used by the program. The data
  23.     are stored between invocations in a HyperCard global variable. Each
  24.     time the XFCN is executed, it calls InitGlobalData, which creates
  25.     the global data and stores a handle to it in the global variable,
  26.     or, if the data already exist, simply gets the handle. When the
  27.     global data are first created the function does some one time
  28.     sanity checks, such as ensuring that all required resources
  29.     are available.
  30. */
  31.  
  32. #include <MyHeaders>
  33. #include "ListMain.h"
  34. #include "rsrclib.h"
  35.  
  36. static OSErr init_global_data(ResID procID, XCmdBlockPtr param, GlobalData *gdataPtr);
  37.  
  38. /*--------------------------------------------------------------------*/
  39. /* Initialize the global data */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. OSErr InitGlobalData(XCmdBlockPtr param, Handle rsrc, GlobalData *gdataPtr)
  43. {
  44.     ResID procID;        /* ID of the proc resource */
  45.     GlobalData gdata;    /* the global data */
  46.     OSErr err = noErr;
  47.     
  48.     *gdataPtr = NULL;
  49.     
  50.     /* get ID of this code resource (this is the "base" id) */
  51.     if (rsrc) {
  52.         err = rsrc_get_id(rsrc, &procID);
  53.         if (err) return(err);
  54.     }
  55.     else
  56.         procID = ARRAYLIST_XFCN_ID; /* use default (useful when debugging) */
  57.  
  58.     /* get handle to global data from HyperCard global variable */
  59.  
  60.     gdata = (GlobalData) GetGlobalToNum(param, ARRAYLIST_GLOBAL_NAME);
  61.  
  62.     if (! gdata) {
  63.         /* Create new global data and save in a HyperCard global variable.
  64.            This is also a chance to do first-time initializations */
  65.         err = init_global_data(procID, param, &gdata);
  66.         if (err)
  67.             return(err);
  68.         SetGlobalToNum(param, ARRAYLIST_GLOBAL_NAME, (long) gdata);
  69.     }
  70.     else if ((**gdata).chksum != CHKSUM)
  71.         return(ERR_ALST_CHKSUM);
  72.  
  73.     /* Reinitialize any volatile fields in the global data that may have
  74.         changed while the code resource wasn't executing. Especially
  75.         important are function pointers, since their addresses may
  76.         change every time the code resource is loaded into memory. */
  77.     (**gdata).param = param;
  78.     
  79.     *gdataPtr = gdata;
  80.     return(noErr);
  81. }
  82.  
  83. /* get the table of arrays */
  84. ListTableType GetTable(GlobalData gdata)
  85. {
  86.     ListTableType    table;
  87.     
  88.     table = (**gdata).table;
  89.     /* we update the table to make sure all of its fields are valid */
  90.     ltable_update(table);
  91.     return(table);
  92. }
  93.  
  94. /* return the actual ID of the resource with the given internal ID */
  95. ResID GetRsrcID(GlobalData gdata, ResID internal_id)
  96. {
  97.     return((**gdata).map[internal_id]);
  98. }
  99.  
  100. /* create and initialize the global data */
  101. static OSErr init_global_data(ResID procID,
  102.                                     XCmdBlockPtr param,
  103.                                     GlobalData *gdataPtr)
  104. {
  105.     ListTableType table;
  106.     GlobalData    gdata;
  107.     OSErr    err = noErr;
  108.  
  109.     *gdataPtr = NULL;
  110.     gdata = (GlobalData) halloc(sizeof(GlobalDataStruct));
  111.     if ((err=MemError()) == noErr) {
  112.  
  113.         /* create table of the arrays */
  114.         err = ltable_new(&table);
  115.  
  116.         if (err == noErr) {
  117.  
  118.             /* initialize resource map */
  119.             err = rsrc_build_map(procID, (**gdata).map);
  120.  
  121.             if (! err) {
  122.  
  123.                 /* fill in the rest of the fields */
  124.                 
  125.                 (**gdata).chksum = CHKSUM;
  126.                 (**gdata).table = table;
  127.                 (**gdata).param = param;
  128.                 (**gdata).baseID = procID;
  129.                 (**gdata).error = noErr;
  130.                 *gdataPtr = gdata;
  131.                 return(noErr);
  132.             }
  133.             ltable_dispose(table);
  134.         }
  135.         hfree(gdata);
  136.     }
  137.     return(err);
  138. }
  139.