mux/src/htab.cpp

Go to the documentation of this file.
00001 
00022 #include "copyright.h"
00023 #include "autoconf.h"
00024 #include "config.h"
00025 #include "externs.h"
00026 
00039 void hashreset(CHashTable *htab)
00040 {
00041     htab->ResetStats();
00042 }
00043 
00052 #pragma pack(1)
00053 static struct
00054 {
00055     void *pData;
00056     char  aKey[LBUF_SIZE+125];
00057 } htab_rec;
00058 #pragma pack()
00059 
00076 void *hashfindLEN(const void *pKey, size_t nKey, CHashTable *htab)
00077 {
00078     if (  pKey == NULL
00079        || nKey <= 0)
00080     {
00081         return NULL;
00082     }
00083 
00084     UINT32 nHash = HASH_ProcessBuffer(0, pKey, nKey);
00085 
00086     HP_DIRINDEX iDir = HF_FIND_FIRST;
00087     iDir = htab->FindFirstKey(nHash);
00088     while (iDir != HF_FIND_END)
00089     {
00090         HP_HEAPLENGTH nRecord;
00091         htab->Copy(iDir, &nRecord, &htab_rec);
00092         size_t nTarget = nRecord - sizeof(int *);
00093 
00094         if (  nTarget == nKey
00095            && memcmp(pKey, htab_rec.aKey, nKey) == 0)
00096         {
00097             return htab_rec.pData;
00098         }
00099         iDir = htab->FindNextKey(iDir, nHash);
00100     }
00101     return NULL;
00102 }
00103 
00123 int hashaddLEN(const void *pKey, size_t nKey, void *pData, CHashTable *htab)
00124 {
00125     if (  pKey == NULL
00126        || nKey <= 0)
00127     {
00128         return -1;
00129     }
00130 
00131     UINT32 nHash = HASH_ProcessBuffer(0, pKey, nKey);
00132 
00133     htab_rec.pData = pData;
00134     memcpy(htab_rec.aKey, pKey, nKey);
00135     size_t nRecord = nKey + sizeof(void *);
00136     htab->Insert((HP_HEAPLENGTH)nRecord, nHash, &htab_rec);
00137     return 0;
00138 }
00139 
00153 void hashdeleteLEN(const void *pKey, size_t nKey, CHashTable *htab)
00154 {
00155     if (  pKey == NULL
00156        || nKey <= 0)
00157     {
00158         return;
00159     }
00160 
00161     UINT32 nHash = HASH_ProcessBuffer(0, pKey, nKey);
00162 
00163     HP_DIRINDEX iDir = htab->FindFirstKey(nHash);
00164     while (iDir != HF_FIND_END)
00165     {
00166         HP_HEAPLENGTH nRecord;
00167         htab->Copy(iDir, &nRecord, &htab_rec);
00168         size_t nTarget = nRecord - sizeof(int *);
00169 
00170         if (  nTarget == nKey
00171            && memcmp(pKey, htab_rec.aKey, nKey) == 0)
00172         {
00173             htab->Remove(iDir);
00174         }
00175         iDir = htab->FindNextKey(iDir, nHash);
00176     }
00177 }
00178 
00188 void hashflush(CHashTable *htab)
00189 {
00190     htab->Reset();
00191 }
00192 
00193 /*
00194  * ---------------------------------------------------------------------------
00195  * * hashreplLEN: replace the data part of a hash entry.
00196  */
00197 
00198 bool hashreplLEN(const void *str, size_t nStr, void *pData, CHashTable *htab)
00199 {
00200     if (  str == NULL
00201        || nStr <= 0)
00202     {
00203         return false;
00204     }
00205 
00206     UINT32 nHash = HASH_ProcessBuffer(0, str, nStr);
00207 
00208     HP_DIRINDEX iDir = htab->FindFirstKey(nHash);
00209     while (iDir != HF_FIND_END)
00210     {
00211         HP_HEAPLENGTH nRecord;
00212         htab->Copy(iDir, &nRecord, &htab_rec);
00213         size_t nTarget = nRecord - sizeof(int *);
00214 
00215         if (  nTarget == nStr
00216            && memcmp(str, htab_rec.aKey, nStr) == 0)
00217         {
00218             htab_rec.pData = pData;
00219             htab->Update(iDir, nRecord, &htab_rec);
00220             return true;
00221         }
00222         iDir = htab->FindNextKey(iDir, nHash);
00223     }
00224     return false;
00225 }
00226 
00227 void hashreplall(const void *old, void *new0, CHashTable *htab)
00228 {
00229     HP_HEAPLENGTH nRecord;
00230     for (  HP_DIRINDEX iDir = htab->FindFirst(&nRecord, &htab_rec);
00231            iDir != HF_FIND_END;
00232            iDir = htab->FindNext(&nRecord, &htab_rec))
00233     {
00234         if (htab_rec.pData == old)
00235         {
00236             htab_rec.pData = new0;
00237             htab->Update(iDir, nRecord, &htab_rec);
00238         }
00239     }
00240 }
00241 
00242 /*
00243  * Returns the key for the first hash entry in 'htab'.
00244  */
00245 
00246 void *hash_firstentry(CHashTable *htab)
00247 {
00248     HP_HEAPLENGTH nRecord;
00249     HP_DIRINDEX iDir = htab->FindFirst(&nRecord, &htab_rec);
00250     if (iDir != HF_FIND_END)
00251     {
00252         return htab_rec.pData;
00253     }
00254     return NULL;
00255 }
00256 
00257 void *hash_nextentry(CHashTable *htab)
00258 {
00259     HP_HEAPLENGTH nRecord;
00260     HP_DIRINDEX iDir = htab->FindNext(&nRecord, &htab_rec);
00261     if (iDir != HF_FIND_END)
00262     {
00263         return htab_rec.pData;
00264     }
00265     return NULL;
00266 }
00267 
00268 void *hash_firstkey(CHashTable *htab, int *nKeyLength, char **pKey)
00269 {
00270     HP_HEAPLENGTH nRecord;
00271     HP_DIRINDEX iDir = htab->FindFirst(&nRecord, &htab_rec);
00272     if (iDir != HF_FIND_END)
00273     {
00274         *nKeyLength = nRecord-sizeof(int *);
00275         *pKey = htab_rec.aKey;
00276         return htab_rec.pData;
00277     }
00278     *nKeyLength = 0;
00279     *pKey = NULL;
00280     return NULL;
00281 }
00282 
00283 void *hash_nextkey(CHashTable *htab, int *nKeyLength, char **pKey)
00284 {
00285     HP_HEAPLENGTH nRecord;
00286     HP_DIRINDEX iDir = htab->FindNext(&nRecord, &htab_rec);
00287     if (iDir != HF_FIND_END)
00288     {
00289         *nKeyLength = nRecord-sizeof(int *);
00290         *pKey = htab_rec.aKey;
00291         return htab_rec.pData;
00292     }
00293     *nKeyLength = 0;
00294     *pKey = NULL;
00295     return NULL;
00296 }
00297 
00298 /*
00299  * ---------------------------------------------------------------------------
00300  * * search_nametab: Search a name table for a match and return the flag value.
00301  */
00302 
00303 bool search_nametab(dbref player, NAMETAB *ntab, char *flagname, int *pflag)
00304 {
00305     NAMETAB *nt;
00306     for (nt = ntab; nt->name; nt++)
00307     {
00308         if (minmatch(flagname, nt->name, nt->minlen))
00309         {
00310             if (check_access(player, nt->perm))
00311             {
00312                 *pflag = nt->flag;
00313                 return true;
00314             }
00315             else
00316             {
00317                 *pflag = -2;
00318                 return false;
00319             }
00320         }
00321     }
00322     *pflag = -1;
00323     return false;
00324 }
00325 
00326 /*
00327  * ---------------------------------------------------------------------------
00328  * * find_nametab_ent: Search a name table for a match and return a pointer to it.
00329  */
00330 
00331 NAMETAB *find_nametab_ent(dbref player, NAMETAB *ntab, char *flagname)
00332 {
00333     NAMETAB *nt;
00334 
00335     for (nt = ntab; nt->name; nt++)
00336     {
00337         if (minmatch(flagname, nt->name, nt->minlen))
00338         {
00339             if (check_access(player, nt->perm))
00340             {
00341                 return nt;
00342             }
00343         }
00344     }
00345     return NULL;
00346 }
00347 
00348 /*
00349  * ---------------------------------------------------------------------------
00350  * * display_nametab: Print out the names of the entries in a name table.
00351  */
00352 
00353 void display_nametab(dbref player, NAMETAB *ntab, char *prefix, bool list_if_none)
00354 {
00355     NAMETAB *nt;
00356     bool got_one = false;
00357     char *buf = alloc_lbuf("display_nametab");
00358     char *bp = buf;
00359 
00360     safe_str(prefix, buf, &bp);
00361     for (nt = ntab; nt->name; nt++)
00362     {
00363         if (  God(player)
00364            || check_access(player, nt->perm))
00365         {
00366             safe_chr(' ', buf, &bp);
00367             safe_str(nt->name, buf, &bp);
00368             got_one = true;
00369         }
00370     }
00371     *bp = '\0';
00372     if (  got_one
00373        || list_if_none)
00374     {
00375         notify(player, buf);
00376     }
00377     free_lbuf(buf);
00378 }
00379 
00380 /* ---------------------------------------------------------------------------
00381  * interp_nametab: Print values for flags defined in name table.
00382  */
00383 
00384 void interp_nametab(dbref player, NAMETAB *ntab, int flagword,
00385     const char *prefix, const char *true_text, const char *false_text)
00386 {
00387     bool bFirst = true;
00388     char *buf = alloc_lbuf("interp_nametab");
00389     char *bp = buf;
00390 
00391     safe_str(prefix, buf, &bp);
00392     for (NAMETAB *nt = ntab; nt->name; nt++)
00393     {
00394         if (  God(player)
00395            || check_access(player, nt->perm))
00396         {
00397             if (!bFirst)
00398             {
00399                 safe_chr(';', buf, &bp);
00400                 bFirst = false;
00401             }
00402             safe_chr(' ', buf, &bp);
00403             safe_str(nt->name, buf, &bp);
00404             safe_str("...", buf, &bp);
00405             if ((flagword & nt->flag) != 0)
00406             {
00407                 safe_str(true_text, buf, &bp);
00408             }
00409             else
00410             {
00411                 safe_str(false_text, buf, &bp);
00412             }
00413         }
00414     }
00415     *bp = '\0';
00416     notify(player, buf);
00417     free_lbuf(buf);
00418 }
00419 
00420 /* ---------------------------------------------------------------------------
00421  * listset_nametab: Print values for flags defined in name table.
00422  */
00423 
00424 void listset_nametab(dbref player, NAMETAB *ntab, int flagword, char *prefix, bool list_if_none)
00425 {
00426     char *buf = alloc_lbuf("listset_nametab");
00427     char *bp = buf;
00428 
00429     safe_str(prefix, buf, &bp);
00430 
00431     NAMETAB *nt = ntab;
00432     bool got_one = false;
00433     while (nt->name)
00434     {
00435         if (  ((flagword & nt->flag) != 0)
00436            && (  God(player)
00437               || check_access(player, nt->perm)))
00438         {
00439             safe_chr(' ', buf, &bp);
00440             safe_str(nt->name, buf, &bp);
00441             got_one = true;
00442         }
00443         nt++;
00444     }
00445     *bp = '\0';
00446     if (  got_one
00447        || list_if_none)
00448     {
00449         notify(player, buf);
00450     }
00451     free_lbuf(buf);
00452 }
00453 
00454 /* ---------------------------------------------------------------------------
00455  * cf_ntab_access: Change the access on a nametab entry.
00456  */
00457 
00458 CF_HAND(cf_ntab_access)
00459 {
00460     NAMETAB *np;
00461     char *ap;
00462 
00463     for (ap = str; *ap && !mux_isspace(*ap); ap++)
00464     {
00465         ; // Nothing.
00466     }
00467     if (*ap)
00468     {
00469         *ap++ = '\0';
00470     }
00471 
00472     while (mux_isspace(*ap))
00473     {
00474         ap++;
00475     }
00476 
00477     for (np = (NAMETAB *) vp; np->name; np++)
00478     {
00479         if (minmatch(str, np->name, np->minlen))
00480         {
00481             return cf_modify_bits(&(np->perm), ap, pExtra, nExtra, player, cmd);
00482         }
00483     }
00484     cf_log_notfound(player, cmd, "Entry", str);
00485     return -1;
00486 }

Generated on Mon May 28 04:40:10 2007 for MUX by  doxygen 1.4.7