nfs-ganesha 1.4

common_utils.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=4:tabstop=8:
00003  */
00004 
00010 #ifdef HAVE_CONFIG_H
00011 #include "config.h"
00012 #endif
00013 
00014 #include "common_utils.h"
00015 
00016 #include <strings.h>
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <ctype.h>
00020 
00029 int s_read_int(char *str)
00030 {
00031 
00032   int i;
00033   int out = 0;
00034 
00035   for(i = 0; str[i]; i++)
00036     {
00037 
00038       if((str[i] < '0') || (str[i] > '9'))
00039         return -1;              /* error */
00040       else
00041         {
00042           out *= 10;
00043           out += (int)(str[i] - '0');
00044         }
00045     }
00046 
00047   if(i == 0)
00048     return -1;
00049 
00050   return out;
00051 
00052 }
00053 
00062 int s_read_octal(char *str)
00063 {
00064 
00065   int i;
00066   int out = 0;
00067 
00068   for(i = 0; str[i]; i++)
00069     {
00070 
00071       if((str[i] < '0') || (str[i] > '7'))
00072         return -1;              /* error */
00073       else
00074         {
00075           out *= 8;
00076           out += (int)(str[i] - '0');
00077         }
00078     }
00079 
00080   if(i == 0)
00081     return -1;
00082 
00083   return out;
00084 
00085 }
00086 
00095 int s_read_int64(char *str, unsigned long long *out64)
00096 {
00097 
00098   int i;
00099   unsigned long long out = 0;
00100 
00101   if(!out64)
00102     return -1;
00103 
00104   for(i = 0; str[i]; i++)
00105     {
00106 
00107       if((str[i] < '0') || (str[i] > '9'))
00108         return -1;              /* error */
00109       else
00110         {
00111           out *= 10;
00112           out += (unsigned long long)(str[i] - '0');
00113         }
00114     }
00115 
00116   if(i == 0)
00117     return -1;
00118 
00119   *out64 = out;
00120 
00121   return 0;
00122 }
00123 
00124 int s_read_size(char *str, size_t * p_size)
00125 {
00126   int i;
00127   size_t out = 0;
00128 
00129   if(!p_size)
00130     return -1;
00131 
00132   for(i = 0; str[i]; i++)
00133     {
00134 
00135       if((str[i] < '0') || (str[i] > '9'))
00136         return -1;              /* error */
00137       else
00138         {
00139           out *= 10;
00140           out += (size_t) (str[i] - '0');
00141         }
00142     }
00143 
00144   if(i == 0)
00145     return -1;
00146 
00147   *p_size = out;
00148 
00149   return 0;
00150 
00151 }
00152 
00157 int StrToBoolean(char *str)
00158 {
00159   if(str == NULL)
00160     return -1;
00161 
00162   if(!strcasecmp(str, "1") || !strcasecmp(str, "TRUE") || !strcasecmp(str, "YES"))
00163     return 1;
00164 
00165   if(!strcasecmp(str, "0") || !strcasecmp(str, "FALSE") || !strcasecmp(str, "NO"))
00166     return 0;
00167 
00168   return -1;
00169 }
00170 
00187 int snprintmem(char *target, int tgt_size, caddr_t source, int mem_size)
00188 {
00189 
00190   unsigned char *c = '\0';      /* the current char to be printed */
00191   char *str = target;           /* the current position in target buffer */
00192   int wrote = 0;
00193 
00194   for(c = (unsigned char *)source; c < ((unsigned char *)source + mem_size); c++)
00195     {
00196       int tmp_wrote = 0;
00197 
00198       if(wrote >= tgt_size)
00199         {
00200           target[tgt_size - 1] = '\0';
00201           break;
00202         }
00203 
00204       tmp_wrote = snprintf(str, tgt_size - wrote, "%.2X", (unsigned char)*c);
00205       str += tmp_wrote;
00206       wrote += tmp_wrote;
00207 
00208     }
00209 
00210   return wrote;
00211 
00212 }
00213 
00214 /* test if a letter is hexa */
00215 #define IS_HEXA( c )  ( (((c) >= '0') && ((c) <= '9')) || (((c) >= 'A') && ((c) <= 'F')) || (((c) >= 'a') && ((c) <= 'f')) )
00216 
00217 /* converts an hexa letter */
00218 #define HEXA2BYTE( c ) ((unsigned char)(((c) >= '0') && ((c) <= '9')?((c) - '0'):\
00219                         (((c) >= 'A') && ((c) <= 'F')?((c)-'A'+10) :\
00220                         (((c) >= 'a') && ((c) <= 'f')?((c)-'a'+10) : /*error :*/ 0 ))))
00221 
00237 int sscanmem(caddr_t target, int tgt_size, const char *str_source)
00238 {
00239 
00240   unsigned char *p_mem;         /* the current byte to be set */
00241 
00242   const char *p_src;            /* pointer to the current char to be read. */
00243 
00244   int nb_read = 0;
00245 
00246   p_src = str_source;
00247 
00248   for(p_mem = (unsigned char *)target; p_mem < ((unsigned char *)target + tgt_size);
00249       p_mem++)
00250     {
00251 
00252       unsigned char tmp_val;
00253 
00254       /* we must read 2 bytes (written in hexa) to have 1 target byte value. */
00255       if((*p_src == '\0') || (*(p_src + 1) == '\0'))
00256         {
00257           /* error, the source string is too small */
00258           return -1;
00259         }
00260 
00261       /* they must be hexa values */
00262       if(!IS_HEXA(*p_src) || !IS_HEXA(*(p_src + 1)))
00263         {
00264           return -1;
00265         }
00266 
00267       /* we read hexa values. */
00268       tmp_val = (HEXA2BYTE(*p_src) << 4) + HEXA2BYTE(*(p_src + 1));
00269 
00270       /* we had them to the target buffer */
00271       (*p_mem) = tmp_val;
00272 
00273       p_src += 2;
00274       nb_read += 2;
00275 
00276     }
00277 
00278   return nb_read;
00279 
00280 }
00281 
00293 int find_space(char c)
00294 {
00295   return isspace(c);
00296 }                               /* find_space */
00297 
00309 int find_comma(char c)
00310 {
00311   return (c == ',') ? 1 : 0;
00312 }                               /* find_comma */
00313 
00325 int find_colon(char c)
00326 {
00327   return (c == ':') ? 1 : 0;
00328 }                               /* find_colon */
00329 
00341 int find_endLine(char c)
00342 {
00343   return (c == '\0' || c == '\n') ? 1 : 0;
00344 }                               /* find_endLine */
00345 
00357 int find_slash(char c)
00358 {
00359   return (c == '/') ? 1 : 0;
00360 }                               /* find_slash */