home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- #include <tos.h>
- #include <sysvar.h>
-
- #define JAR_INCR 15 /* Amount to increment jar size by (in cookies) */
- /* --------------------------------------------------------- */
- COOKIE *scan_jar(jar, target, size, num_cookies)
- /* Searches for target, and returns that cookie if it is found. */
- /* Otherwise, it returns the zero cookie, and sets size and num_cookies */
- /* appropriately. Returns NULL if no cookie jar exists. */
- COOKIE *jar;
- long target; /* Cookie to search for */
- int *size; /* Total size of jar */
- int *num_cookies; /* # cookies currently in jar (including the zero cookie) */
- {
- COOKIE *p;
- COOKIE *ret;
- *size = *num_cookies = 0;
- if (jar == NULL) return NULL;
-
- /* Count the cookies */
- ret = NULL;
- p = jar;
- while (TRUE) {
- (*num_cookies)++;
- if (p->c == 0) break;
- if (p->c == target) ret = p;
- p++;
- }
- if (ret == NULL) ret = p; /* Set to zero-cookie if the cookie no found */
-
- *size = (int)(p->v);
-
- return ret;
- }
- /* --------------------------------------------------------- */
- typedef char *charp;
- typedef charp (charpfunc)();
- BOOLEAN setcookie(cook, val, mem_alloc, copyover)
- /* Returns whether or not the cookie existed before */
- LONG cook; /* Cookie to set */
- LONG val; /* Value of cookie */
- charpfunc *mem_alloc; /* Memory allocator, if needed */
- /* If NULL, Malloc is used */
- BOOLEAN copyover; /* TRUE if setcookie should change the old cookie, if found */
- {
- COOKIE *p, *jar, *njar;
- int size; /* capacity of jar */
- int nsize; /* capacity of new jar */
- long nbsize; /* Number of BYTES to allocate for new jar */
- int num_cookies; /* # of cookies in jar */
- /* Get a pointer to the cookie */
- p = scan_jar(jar = (COOKIE *)peekl(p_cookies), cook, &size, &num_cookies);
-
- if (size == num_cookies) { /* No more room... */
- if (p == NULL) { /* We'll create a jar, so arrange to */
- unjar(); /* release it at reset time */
- }
-
- /* Calculate new jar size */
- nsize = size+JAR_INCR;
- nbsize = nsize*sizeof(COOKIE);
-
- /* Allocate and install new jar */
- njar = (mem_alloc == NULL ? Malloc(nbsize) : (*mem_alloc)(nbsize));
- pokel(p_cookies, njar); /* Install the new jar */
-
- /* Copy the old jar */
- while (--num_cookies > 0) *(njar++) = *(jar++);
-
- /* Install the new cookie */
- njar->c = cook;
- njar->v = val;
- njar++;
- njar->c = 0;
- njar->v = nsize;
- return FALSE;
- } else if (p->c == 0L) { /* Cookie didn't exist */
- (p + 1)->v = p->v;
- (p + 1)->c = 0L;
- p->v = val;
- p->c = cook;
- return FALSE;
- } else { /* Cookie found */
- if (copyover) p->v = val;
- return TRUE;
- }
- }
- /* --------------------------------------------------------- */
- #if 0
- main()
- {
- LONG c;
- for (c = 1; c < 100; c++) {
- setcookie(c, c, NULL);
- printf("Cookie: %ld\n", c);
- }
- printf("***********************************************************\n");
- term_res(0);
- }
- #endif
- /* --------------------------------------------------------- */
-