appletdynamicarrays.c

Go to the documentation of this file.
00001 
00002 /*  $Id: appletdynamicarrays.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /* copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
00005 
00006 
00007 #include "appletmemory.h"
00008 #include "appletdynamicarrays.h"
00009 
00010 
00011 
00012 
00013 short sizedynamicarray (tydynamicarray *dynam) {
00014     
00015     if ((*dynam).hstorage == nil)
00016         return (0);
00017         
00018     return ((short) (GetHandleSize ((*dynam).hstorage) / (*dynam).elementsize));
00019     } /*sizedynamicarray*/
00020     
00021     
00022 boolean indexdynamicarray (short ix, tydynamicarray *dynam, void *pdata) {
00023     
00024     Handle h = (*dynam).hstorage;
00025     short elementsize = (*dynam).elementsize;
00026     char *p;
00027     
00028     if (ix >= sizedynamicarray (dynam)) /*index is 0-based*/
00029         return (false);
00030     
00031     p = ((char *) *(*dynam).hstorage) + (ix * elementsize);
00032     
00033     moveleft (p, pdata, elementsize);
00034     
00035     return (true);
00036     } /*indexdynamicarray*/
00037     
00038     
00039 void cleardynamicarrayelement (short ix, tydynamicarray *dynam) {
00040 
00041     Handle h = (*dynam).hstorage;
00042     short elementsize = (*dynam).elementsize;
00043     char *p;
00044     
00045     if (ix >= sizedynamicarray (dynam)) /*index is 0-based*/
00046         return;
00047     
00048     p = ((char *) *(*dynam).hstorage) + (ix * elementsize);
00049     
00050     clearbytes (p, elementsize);
00051     } /*cleardynamicarrayelement*/
00052     
00053     
00054 boolean addtodynamicarray (void *pdata, tydynamicarray *dynam) {
00055     
00056     Handle h = (*dynam).hstorage;
00057     short elementsize = (*dynam).elementsize;
00058     char *p;
00059     short ct, i;
00060     
00061     if (h == nil)
00062         return (newfilledhandle (pdata, elementsize, &(*dynam).hstorage));
00063         
00064     p = (char *) *h;
00065     
00066     ct = sizedynamicarray (dynam);
00067     
00068     for (i = 0; i < ct; i++) { /*look for an empty slot in the array*/
00069         
00070         long x;
00071         
00072         moveleft (p, &x, longsizeof (x));
00073         
00074         if (x == 0) { /*the first four bytes are 0, it's empty*/
00075         
00076             moveleft (pdata, p, elementsize);
00077             
00078             return (true);
00079             }
00080         
00081         p += (*dynam).elementsize;
00082         } /*for*/
00083     
00084     /*no empty slots, make the array bigger*/
00085     
00086     SetHandleSize (h, (ct + 1) * elementsize);
00087 
00088     if (MemError () != noErr)
00089         return (false);
00090         
00091     p = *h + (ct * elementsize);
00092     
00093     moveleft (pdata, p, elementsize);
00094     
00095     return (true);
00096     } /*addtodynamicarray*/
00097     
00098     
00099 boolean indynamicarray (long firstfourbytes, tydynamicarray *dynam, ptrchar *pdata) {
00100 
00101     Handle h = (*dynam).hstorage;
00102     short elementsize = (*dynam).elementsize;
00103     char *p;
00104     short ct, i;
00105 
00106     p = (char *) *h;
00107     
00108     ct = sizedynamicarray (dynam);
00109     
00110     for (i = 0; i < ct; i++) { 
00111         
00112         long x;
00113         
00114         moveleft (p, &x, longsizeof (x));
00115         
00116         if (x == firstfourbytes) { /*the first four bytes match*/
00117             
00118             *pdata = p;
00119             
00120             return (true);
00121             }
00122         
00123         p += (*dynam).elementsize;
00124         } /*for*/
00125     
00126     return (false);
00127     } /*indynamicarray*/
00128     
00129     
00130 boolean removefromdynamicarray (long firstfourbytes, tydynamicarray *dynam) {
00131     
00132     char *p;
00133     
00134     if (!indynamicarray (firstfourbytes, dynam, &p))
00135         return (false);
00136         
00137     clearbytes (p, (*dynam).elementsize);
00138     
00139     return (true);
00140     } /*removefromdynamicarray*/
00141     
00142     
00143 boolean disposedynamicarray (tydynamicarray *dynam) {
00144     
00145     disposehandle ((*dynam).hstorage);
00146     
00147     (*dynam).hstorage = nil;
00148     
00149     return (true);
00150     } /*disposedynamicarray*/
00151     
00152     
00153 

Generated on Wed May 31 18:19:40 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6