XrdCnsXref.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                         X r d C n s X r e f . c c                          */
00004 /*                                                                            */
00005 /* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010   
00011 //         $Id: XrdCnsXref.cc 35287 2010-09-14 21:19:35Z ganis $
00012 
00013 const char *XrdCnsXrefCVSID = "$Id: XrdCnsXref.cc 35287 2010-09-14 21:19:35Z ganis $";
00014 
00015 #include "XrdCns/XrdCnsXref.hh"
00016 
00017 /******************************************************************************/
00018 /*                               G l o b a l s                                */
00019 /******************************************************************************/
00020 
00021 char *XrdCnsXref::xIndex = (char *)"0123456789:;<=>?@"
00022                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
00023                                    "abcdefghijklmnopqrstuvwxyz{|}~";
00024 
00025 /******************************************************************************/
00026 /*                           C o n s t r u c t o r                            */
00027 /******************************************************************************/
00028   
00029 XrdCnsXref::XrdCnsXref(const char *dflt, int MTProt)
00030 {
00031    memset(yTable, 0, sizeof(yTable));
00032    isMT = MTProt;
00033    if (dflt) Default(dflt);
00034    availIdx = 1;
00035 }
00036 
00037 /******************************************************************************/
00038 /*                            D e s t r u c t o r                             */
00039 /******************************************************************************/
00040   
00041 XrdCnsXref::~XrdCnsXref()
00042 {
00043    int i;
00044 
00045    for (i = 0; i < yTSize; i++) if (yTable[i]) free(yTable[i]);
00046 }
00047 
00048 /******************************************************************************/
00049 /*                                   A d d                                    */
00050 /******************************************************************************/
00051   
00052 char XrdCnsXref::Add(const char *kval, char idx)
00053 {
00054    XrdSysMutexHelper xHelp(isMT ? &xMutex : 0);
00055    char *oldx, *xKey = strdup(kval);
00056    int i, j;
00057 
00058 // If a character was specified, try to use it.
00059 //
00060         if (idx) i = c2i(idx);
00061    else if ((oldx = xTable.Find(xKey))) {free(xKey); return *oldx;}
00062    else if ((i = availI()) < 0)         {free(xKey); return 0;}
00063 
00064 // Try to add the new entry
00065 //
00066    if (!(oldx = xTable.Add(xKey, xIndex+i, 0, Hash_keep))) yTable[i] = xKey;
00067       else if (*oldx != idx)
00068               {if ((j = c2i(*oldx)) >= 0)
00069                   {if (yTable[j])
00070                       {xTable.Del(yTable[j]); free(yTable[j]); yTable[j] = 0;}
00071                    xTable.Rep(xKey, xIndex+i, 0, Hash_keep);
00072                    yTable[j] = xKey;
00073                   }
00074               } else free(xKey);
00075 
00076 // Return the assigned character index
00077 //
00078    return xIndex[i];
00079 }
00080 
00081 /******************************************************************************/
00082 /*                               D e f a u l t                                */
00083 /******************************************************************************/
00084   
00085 char XrdCnsXref::Default(const char *kval)
00086 {
00087    return (kval ? Add(kval, xIndex[0]) : xIndex[0]);
00088 }
00089 
00090 /******************************************************************************/
00091 /*                                   K e y                                    */
00092 /******************************************************************************/
00093   
00094 char *XrdCnsXref::Key(char idc)
00095 {
00096    XrdSysMutexHelper xHelp(isMT ? &xMutex : 0);
00097    int i = c2i(idc);
00098 
00099    if (i >= 0 && yTable[i]) return yTable[i];
00100    return yTable[0];
00101 }
00102 
00103 /******************************************************************************/
00104 /*                                  F i n d                                   */
00105 /******************************************************************************/
00106   
00107 char XrdCnsXref::Find(const char *kval)
00108 {
00109    XrdSysMutexHelper xHelp(isMT ? &xMutex : 0);
00110    char *xdat;
00111 
00112    return ((xdat = xTable.Find(kval)) ? *xdat : char(0));
00113 }
00114 
00115 /******************************************************************************/
00116 /*                       P r i v a t e   M e t h o d s                        */
00117 /******************************************************************************/
00118 /******************************************************************************/
00119 /* Private:                       A v a i l I                                 */
00120 /******************************************************************************/
00121   
00122 int XrdCnsXref::availI()
00123 {
00124    int idn;
00125 
00126    for (idn = availIdx; idn < yTSize; idn++)
00127        if (!yTable[idn]) {availIdx = idn+1; return idn;}
00128 
00129    return -1;
00130 }
00131 
00132 /******************************************************************************/
00133 /* Private:                          c 2 i                                    */
00134 /******************************************************************************/
00135   
00136 int XrdCnsXref::c2i(char xCode)
00137 {
00138    int n = static_cast<int>(xCode);
00139 
00140    return (n <= 127 ? xCode - '0' : -1);
00141 }

Generated on Tue Jul 5 14:46:32 2011 for ROOT_528-00b_version by  doxygen 1.5.1