src/vattr.c

Go to the documentation of this file.
00001 /*
00002  * vattr.c -- Manages the user-defined attributes. 
00003  */
00004 
00005 #include "copyright.h"
00006 #include "config.h"
00007 
00008 #include "copyright.h"
00009 #include "mudconf.h"
00010 #include "vattr.h"
00011 #include "alloc.h"
00012 #include "htab.h"
00013 #include "externs.h"
00014 
00015 static void fixcase(char *);
00016 static char *store_string(char *);
00017 
00018 /*
00019  * Allocate space for strings in lumps this big. 
00020  */
00021 
00022 #define STRINGBLOCK 1000
00023 
00024 /*
00025  * Current block we're putting stuff in 
00026  */
00027 
00028 static char *stringblock = (char *) 0;
00029 
00030 /*
00031  * High water mark. 
00032  */
00033 
00034 static int stringblock_hwm = 0;
00035 
00036 void vattr_init(void)
00037 {
00038         hashinit(&mudstate.vattr_name_htab, 65536);
00039 }
00040 
00041 VATTR *vattr_find(char *name)
00042 {
00043         register VATTR *vp;
00044 
00045         if(!ok_attr_name(name))
00046                 return (NULL);
00047 
00048         vp = (VATTR *) hashfind(name, &mudstate.vattr_name_htab);
00049 
00050         /*
00051          * vp is NULL or the right thing. It's right, either way. 
00052          */
00053         return (vp);
00054 }
00055 
00056 VATTR *vattr_alloc(char *name, int flags)
00057 {
00058         int number;
00059 
00060         if(((number = mudstate.attr_next++) & 0x7f) == 0)
00061                 number = mudstate.attr_next++;
00062         anum_extend(number);
00063         return (vattr_define(name, number, flags));
00064 }
00065 
00066 VATTR *vattr_define(char *name, int number, int flags)
00067 {
00068         VATTR *vp;
00069 
00070         /*
00071          * Be ruthless. 
00072          */
00073 
00074         if(strlen(name) > VNAME_SIZE)
00075                 name[VNAME_SIZE - 1] = '\0';
00076 
00077         fixcase(name);
00078         if(!ok_attr_name(name))
00079                 return (NULL);
00080 
00081         if((vp = vattr_find(name)) != NULL)
00082                 return (vp);
00083 
00084         vp = (VATTR *) malloc(sizeof(VATTR));
00085 
00086         vp->name = store_string(name);
00087         vp->flags = flags;
00088         vp->number = number;
00089 
00090         hashadd(vp->name, (int *) vp, &mudstate.vattr_name_htab);
00091 
00092         anum_extend(vp->number);
00093         anum_set(vp->number, (ATTR *) vp);
00094         return (vp);
00095 }
00096 
00097 void do_dbclean(dbref player, dbref cause, int key)
00098 {
00099         VATTR *vp;
00100         dbref i;
00101         int notfree;
00102 
00103         for(vp = (VATTR *) hash_firstentry(&mudstate.vattr_name_htab);
00104                 vp != NULL;
00105                 vp = (VATTR *) hash_nextentry(&mudstate.vattr_name_htab)) {
00106                 notfree = 0;
00107 
00108                 DO_WHOLE_DB(i) {
00109                         if(atr_get_raw(i, vp->number) != NULL) {
00110                                 notfree = 1;
00111                                 break;
00112                         }
00113                 }
00114 
00115                 if(!notfree) {
00116                         anum_set(vp->number, NULL);
00117                         hashdelete(vp->name, &mudstate.vattr_name_htab);
00118                         free((char *) vp);
00119                 }
00120         }
00121         notify(player, "Database cleared of stale attribute entries.");
00122 }
00123 
00124 void vattr_delete(char *name)
00125 {
00126         VATTR *vp;
00127         int number;
00128 
00129         fixcase(name);
00130         if(!ok_attr_name(name))
00131                 return;
00132 
00133         number = 0;
00134 
00135         vp = (VATTR *) hashfind(name, &mudstate.vattr_name_htab);
00136 
00137         if(vp) {
00138                 number = vp->number;
00139                 anum_set(number, NULL);
00140                 hashdelete(name, &mudstate.vattr_name_htab);
00141                 free((char *) vp);
00142         }
00143 
00144         return;
00145 }
00146 
00147 VATTR *vattr_rename(char *name, char *newname)
00148 {
00149         VATTR *vp;
00150 
00151         fixcase(name);
00152         if(!ok_attr_name(name))
00153                 return (NULL);
00154 
00155         /*
00156          * Be ruthless. 
00157          */
00158 
00159         if(strlen(newname) > VNAME_SIZE)
00160                 newname[VNAME_SIZE - 1] = '\0';
00161 
00162         fixcase(newname);
00163         if(!ok_attr_name(newname))
00164                 return (NULL);
00165 
00166         vp = (VATTR *) hashfind(name, &mudstate.vattr_name_htab);
00167 
00168         if(vp)
00169                 vp->name = store_string(newname);
00170 
00171         return (vp);
00172 }
00173 
00174 VATTR *vattr_first(void)
00175 {
00176         return (VATTR *) hash_firstentry(&mudstate.vattr_name_htab);
00177 }
00178 
00179 VATTR *vattr_next(VATTR * vp)
00180 {
00181         if(vp == NULL)
00182                 return (vattr_first());
00183 
00184         return ((VATTR *) hash_nextentry(&mudstate.vattr_name_htab));
00185 }
00186 
00187 static void fixcase(char *name)
00188 {
00189         char *cp = name;
00190 
00191         while (*cp) {
00192                 *cp = ToUpper(*cp);
00193                 cp++;
00194         }
00195 
00196         return;
00197 }
00198 
00203 static char *store_string(char *str)
00204 {
00205         int len;
00206         char *ret;
00207 
00208         len = strlen(str);
00209 
00210         /*
00211          * If we have no block, or there's not enough room left in the * * *
00212          * current one, get a new one. 
00213          */
00214 
00215         if(!stringblock || (STRINGBLOCK - stringblock_hwm) < (len + 1)) {
00216                 stringblock = (char *) malloc(STRINGBLOCK);
00217                 if(!stringblock)
00218                         return ((char *) 0);
00219                 stringblock_hwm = 0;
00220         }
00221         ret = stringblock + stringblock_hwm;
00222         StringCopy(ret, str);
00223         stringblock_hwm += (len + 1);
00224         return (ret);
00225 }

Generated on Mon May 28 04:25:26 2007 for BattletechMUX by  doxygen 1.4.7