home *** CD-ROM | disk | FTP | other *** search
/ Dream 48 / Amiga_Dream_48.iso / Atari / c / libs / xaes_new.lzh / COOKIE.C < prev    next >
C/C++ Source or Header  |  1994-12-27  |  4KB  |  182 lines

  1. /********************************************************************
  2.  *                                                                1.01*
  3.  *    XAES: Cookie Management                                            *
  4.  *    by Ken Hollis                                                    *
  5.  *                                                                    *
  6.  *    Copyright (c) 1994, Bitgate Software.  All Rights Reserved.        *
  7.  *                                                                    *
  8.  *    Christian Grunenberg's original cookiejar routines were buggy    *
  9.  *    as hell.  These were his originals.  I have yet to change it so    *
  10.  *    'add' cookie works.  For now, get cookie routines work fine...    *
  11.  *                                                                    *
  12.  ********************************************************************/
  13.  
  14. #include <stddef.h>
  15. #include <tos.h>
  16. #include "xaes.h"
  17.  
  18. /*
  19.  *    This returns the base address of the cookie jar
  20.  */
  21. GLOBAL long *get_cookiejar(void)
  22. {
  23.     register long old_stack, *jar;
  24.  
  25.     old_stack = (long) Super(0L);
  26.     jar = *((long **) 0x5A0L);
  27.     Super((long *) old_stack);
  28.     return(jar);
  29. }
  30.  
  31. /*
  32.  *    This creates a cookie ID and returns it in the cookie value.
  33.  */
  34. GLOBAL void create_cookie(COOKIE *cookie, long id, long value)
  35. {
  36.     *((long *) cookie)++ = id;
  37.     *((long *) cookie) = value;
  38. }
  39.  
  40. /*
  41.  *    This searches for a cookie with the 'id' name, and it returns
  42.  *    the value (via address, how sweet) of the cookie jar as a nice
  43.  *    typedef.
  44.  */
  45. GLOBAL COOKIE *find_cookie(long id)
  46. {
  47.     long *jar;
  48.  
  49.     if (((long *)jar = get_cookiejar()) == 0)
  50.         return(0);
  51.  
  52.     while(*jar) {
  53.         if(*jar == id) return((COOKIE *)jar);
  54.         jar += 2;
  55.     }
  56.  
  57.     return(0);
  58. }
  59.  
  60. /*
  61.  *    Creates a new cookie jar entry
  62.  */
  63. GLOBAL int new_cookie(COOKIE *ent)
  64. {
  65.     register long *cookiejar = get_cookiejar();
  66.     register int act_row = 0;
  67.  
  68.     if (cookiejar) {
  69.         while(*cookiejar) {
  70.             cookiejar += 2;
  71.             act_row++;
  72.         }
  73.  
  74.         if (act_row < cookiejar[1]) {
  75.             cookiejar[2] = cookiejar[0];
  76.             cookiejar[3] = cookiejar[1];
  77.  
  78.             *cookiejar++ = *((long *) ent)++;
  79.             *cookiejar = *((long *) ent);
  80.             return(TRUE);
  81.         }
  82.     }
  83.     return(FALSE);
  84. }
  85.  
  86. /*
  87.  *    This searches for a cookie and makes sure it exists.
  88.  */
  89. GLOBAL BOOL get_cookie (long cookie, long *value)
  90. {
  91.     register long *cookiejar = get_cookiejar();
  92.     
  93.     if (cookiejar)
  94.     {
  95.         while (*cookiejar)
  96.         {
  97.             if (*cookiejar==cookie)
  98.             {
  99.                 if (value)
  100.                     *value = *++cookiejar;
  101.                 return(TRUE);
  102.             }
  103.             cookiejar += 2;
  104.         }
  105.     }
  106.     return(FALSE);
  107. }
  108.  
  109. /*
  110.  *    This is a little simpler version of the routine above stated.
  111.  */
  112. GLOBAL int locate_cookie(long cookie)
  113. {
  114.     if ((find_cookie(cookie)) != NULL)
  115.         return TRUE;
  116.     else
  117.         return FALSE;
  118. }
  119.  
  120. /*
  121.  *    This allows you to eat a cookie from the cookie jar.
  122.  */
  123. GLOBAL void remove_cookie(long cookie_id)
  124. {
  125.     register long *cookiejar = get_cookiejar();
  126.  
  127.     if (cookiejar) {
  128.         while (*cookiejar && cookiejar[1]!=cookie_id)
  129.             cookiejar += 2;
  130.  
  131.         if (*cookiejar)
  132.             do {
  133.                 *cookiejar++ = cookiejar[2];
  134.                 *cookiejar = cookiejar[2];
  135.             } while(*cookiejar);
  136.     }
  137. }
  138.  
  139. /*
  140.  *    This moves the address of the entire cookie jar to another
  141.  *    address that you hopefully mallocated, and reserved.
  142.  */
  143. GLOBAL void move_cookiejar(long *dest, long size)
  144. {
  145.     register long old_stack, *cookiejar, *dest_cop;
  146.  
  147.     old_stack = (long) Super(0L);
  148.     cookiejar = *((long **) 0x5A0L);
  149.  
  150.     if (cookiejar) {
  151.         dest_cop = dest;
  152.  
  153.         do {
  154.             *dest++ = *cookiejar++;
  155.             *dest++ = *cookiejar++;
  156.         } while (*cookiejar);
  157.  
  158.         *dest++ = *cookiejar;
  159.         *dest = size;
  160.  
  161.         cookiejar = (long *) 0x5A0L;
  162.         *cookiejar = (long) dest_cop;
  163.     }
  164.  
  165.     Super((long *) old_stack);
  166. }
  167.  
  168. /*
  169.  *    This returns the size of the cookie jar.
  170.  */
  171. GLOBAL long cookie_size()
  172. {
  173.     register long *cookiejar = get_cookiejar();
  174.  
  175.     if (cookiejar) {
  176.         while(*cookiejar) cookiejar += 2;
  177.         return(*++cookiejar);
  178.     }
  179.  
  180.     return(0);
  181. }
  182.