XrdOuca2x.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                          X r d O u c a 2 x . c c                           */
00004 /*                                                                            */
00005 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*       All Rights Reserved. See XrdInfo.cc for complete License Terms       */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 //           $Id: XrdOuca2x.cc 32231 2010-02-05 18:24:46Z ganis $
00012 
00013 const char *XrdOuca2xCVSID = "$Id: XrdOuca2x.cc 32231 2010-02-05 18:24:46Z ganis $";
00014 
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <sys/stat.h>
00018 #include <errno.h>
00019 
00020 #ifdef WIN32
00021 #include "XrdSys/XrdWin32.hh"
00022 #endif
00023 #include "XrdOuc/XrdOuca2x.hh"
00024 
00025 /******************************************************************************/
00026 /*                                   a 2 i                                    */
00027 /******************************************************************************/
00028 
00029 int XrdOuca2x::a2i(XrdSysError &Eroute, const char *emsg, const char *item,
00030                                              int *val, int minv, int maxv)
00031 {
00032     char *eP;
00033 
00034     if (!item || !*item)
00035        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00036 
00037     errno = 0;
00038     *val  = strtol(item, &eP, 10);
00039     if (errno || *eP)
00040        {Eroute.Emsg("a2x", emsg, item, "is not a number");
00041         return -1;
00042        }
00043     if (*val < minv) 
00044        return Emsg(Eroute, emsg, item, "may not be less than %d", minv);
00045     if (maxv >= 0 && *val > maxv)
00046        return Emsg(Eroute, emsg, item, "may not be greater than %d", maxv);
00047     return 0;
00048 }
00049  
00050 /******************************************************************************/
00051 /*                                  a 2 l l                                   */
00052 /******************************************************************************/
00053 
00054 int XrdOuca2x::a2ll(XrdSysError &Eroute, const char *emsg, const char *item,
00055                                 long long *val, long long minv, long long maxv)
00056 {
00057     char *eP;
00058 
00059     if (!item || !*item)
00060        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00061 
00062     errno = 0;
00063     *val  = strtoll(item, &eP, 10);
00064     if (errno || *eP)
00065        {Eroute.Emsg("a2x", emsg, item, "is not a number");
00066         return -1;
00067        }
00068     if (*val < minv) 
00069        return Emsg(Eroute, emsg, item, "may not be less than %lld", minv);
00070     if (maxv >= 0 && *val > maxv)
00071        return Emsg(Eroute, emsg, item, "may not be greater than %lld", maxv);
00072     return 0;
00073 }
00074 
00075 /******************************************************************************/
00076 /*                                  a 2 f m                                   */
00077 /******************************************************************************/
00078 
00079 int XrdOuca2x::a2fm(XrdSysError &Eroute, const char *emsg, const char *item,
00080                                               int *val, int minv, int maxv)
00081 {  int rc, num;
00082    if ((rc = a2fm(Eroute, emsg, item, &num, minv))) return rc;
00083    if ((*val | maxv) != maxv) 
00084       {Eroute.Emsg("a2fm", emsg, item, "is too inclusive.");
00085        return -1;
00086       }
00087 
00088    *val = 0;
00089    if (num & 0100) *val |= S_IXUSR; // execute permission: owner
00090    if (num & 0200) *val |= S_IWUSR; // write permission:   owner
00091    if (num & 0400) *val |= S_IRUSR; // read permission:    owner
00092    if (num & 0010) *val |= S_IXGRP; // execute permission: group
00093    if (num & 0020) *val |= S_IWGRP; // write permission:   group
00094    if (num & 0040) *val |= S_IRGRP; // read permission:    group
00095    if (num & 0001) *val |= S_IXOTH; // execute permission: other
00096    if (num & 0002) *val |= S_IWOTH; // write permission:   other
00097    if (num & 0004) *val |= S_IROTH; // read permission:    other
00098    return 0;
00099 }
00100 
00101 int XrdOuca2x::a2fm(XrdSysError &Eroute, const char *emsg, const char *item,
00102                                               int *val, int minv)
00103 {
00104     if (!item || !*item)
00105        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00106 
00107     errno = 0;
00108     *val  = strtol(item, (char **)NULL, 8);
00109     if (errno)
00110        {Eroute.Emsg("a2x", emsg, item, "is not an octal number");
00111         return -1;
00112        }
00113     if (!(*val & minv))
00114        {Eroute.Emsg("a2x", emsg, item, "is too exclusive");;
00115         return -1;
00116        }
00117     return 0;
00118 }
00119  
00120 /******************************************************************************/
00121 /*                                  a 2 s p                                   */
00122 /******************************************************************************/
00123 
00124 int XrdOuca2x::a2sp(XrdSysError &Eroute, const char *emsg, const char *item,
00125                                 long long *val, long long minv, long long maxv)
00126 {
00127     char *pp, buff[120];
00128     int i;
00129 
00130     if (!item || !*item)
00131        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00132 
00133     i = strlen(item);
00134     if (item[i-1] != '%') return a2sz(Eroute, emsg, item, val, minv, maxv);
00135 
00136     errno = 0;
00137     *val  = strtoll(item, &pp, 10);
00138 
00139     if (errno || *pp != '%')
00140        {Eroute.Emsg("a2x", emsg, item, "is not a number");
00141         return -1;
00142        }
00143 
00144     if (maxv < 0) maxv = 100;
00145 
00146     if (*val > maxv)
00147        {sprintf(buff, "may not be greater than %lld%%", maxv);
00148         Eroute.Emsg("a2x", emsg, item, buff);
00149         return -1;
00150        }
00151 
00152     if (minv < 0) minv = 0;
00153 
00154     if (*val > maxv)
00155        {sprintf(buff, "may not be less than %lld%%", minv);
00156         Eroute.Emsg("a2x", emsg, item, buff);
00157         return -1;
00158        }
00159 
00160     *val = -*val;
00161     return 0;
00162 }
00163 
00164 /******************************************************************************/
00165 /*                                  a 2 s z                                   */
00166 /******************************************************************************/
00167 
00168 int XrdOuca2x::a2sz(XrdSysError &Eroute, const char *emsg, const char *item,
00169                                 long long *val, long long minv, long long maxv)
00170 {   long long qmult;
00171     char *eP, *fP = (char *)item + strlen(item) - 1;
00172 
00173     if (!item || !*item)
00174        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00175 
00176          if (*fP == 'k' || *fP == 'K') qmult = 1024LL;
00177     else if (*fP == 'm' || *fP == 'M') qmult = 1024LL*1024LL;
00178     else if (*fP == 'g' || *fP == 'G') qmult = 1024LL*1024LL*1024LL;
00179     else if (*fP == 't' || *fP == 'T') qmult = 1024LL*1024LL*1024LL*1024LL;
00180     else                              {qmult = 1; fP++;}
00181     errno = 0;
00182     *val  = strtoll(item, &eP, 10) * qmult;
00183     if (errno || eP != fP)
00184        {Eroute.Emsg("a2x", emsg, item, "is not a number");
00185         return -1;
00186        }
00187     if (*val < minv) 
00188        return Emsg(Eroute, emsg, item, "may not be less than %lld", minv);
00189     if (maxv >= 0 && *val > maxv)
00190        return Emsg(Eroute, emsg, item, "may not be greater than %lld", maxv);
00191     return 0;
00192 }
00193  
00194 /******************************************************************************/
00195 /*                                  a 2 t m                                   */
00196 /******************************************************************************/
00197 
00198 int XrdOuca2x::a2tm(XrdSysError &Eroute, const char *emsg, const char *item, int *val,
00199                           int minv, int maxv)
00200 {   int qmult;
00201     char *eP, *fP = (char *)item + strlen(item) - 1;
00202 
00203     if (!item || !*item)
00204        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00205 
00206          if (*fP == 's' || *fP == 'S') qmult = 1;
00207     else if (*fP == 'm' || *fP == 'M') qmult = 60;
00208     else if (*fP == 'h' || *fP == 'H') qmult = 60*60;
00209     else if (*fP == 'd' || *fP == 'D') qmult = 60*60*24;
00210     else                              {qmult = 1; fP++;}
00211 
00212     errno = 0;
00213     *val  = strtoll(item, &eP, 10) * qmult;
00214     if (errno || eP != fP)
00215        {Eroute.Emsg("a2x", emsg, item, "is not a number");
00216         return -1;
00217        }
00218     if (*val < minv) 
00219        return Emsg(Eroute, emsg, item, "may not be less than %d", minv);
00220     if (maxv >= 0 && *val > maxv)
00221        return Emsg(Eroute, emsg, item, "may not be greater than %d", maxv);
00222     return 0;
00223 }
00224 
00225 /******************************************************************************/
00226 /*                                  a 2 v p                                   */
00227 /******************************************************************************/
00228 
00229 int XrdOuca2x::a2vp(XrdSysError &Eroute, const char *emsg, const char *item,
00230                                              int *val, int minv, int maxv)
00231 {
00232     char *pp;
00233 
00234     if (!item || !*item)
00235        {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
00236 
00237     errno = 0;
00238     *val  = strtol(item, &pp, 10);
00239 
00240     if (!errno && *pp == '%')
00241        {if (*val < 0)
00242            {Eroute.Emsg("a2x", emsg, item, "may not be negative.");
00243             return -1;
00244            }
00245         if (*val > 100)
00246            {Eroute.Emsg("a2x", emsg, item, "may not be greater than 100%.");
00247             return -1;
00248            }
00249            else {*val = -*val; return 0;}
00250        }
00251 
00252     if (*val < minv) 
00253        return Emsg(Eroute, emsg, item, "may not be less than %d", minv);
00254     if (maxv >= 0 && *val > maxv)
00255        return Emsg(Eroute, emsg, item, "may not be greater than %d", maxv);
00256     return 0;
00257 }
00258 
00259 /******************************************************************************/
00260 /*                       P r i v a t e   M e t h o d s                        */
00261 /******************************************************************************/
00262   
00263 int XrdOuca2x::Emsg(XrdSysError &Eroute, const char *etxt1, const char *item,
00264                                          const char *etxt2, int val)
00265 {char buff[256];
00266  sprintf(buff, etxt2, val);
00267  Eroute.Emsg("a2x", etxt1, item, buff);
00268  return -1;
00269 }
00270 
00271 int XrdOuca2x::Emsg(XrdSysError &Eroute, const char *etxt1, const char *item,
00272                                          const char *etxt2, long long val)
00273 {char buff[256];
00274  sprintf(buff, etxt2, val);
00275  Eroute.Emsg("a2x", etxt1, item, buff);
00276  return -1;
00277 }

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