Logo Search packages:      
Sourcecode: libnl version File versions  Download package

long nl_size2int ( const char *  str  ) 

Convert a character string to a size

  • str size encoded as character string
Converts the specified size as character to the corresponding number of bytes.

Supported formats are:

  • b,kb/k,m/mb,gb/g for bytes
  • bit,kbit/mbit/gbit

Returns:
The number of bytes or -1 if the string is unparseable

Definition at line 197 of file utils.c.

{
      char *p;
      long l = strtol(str, &p, 0);
      if (p == str)
            return -1;

      if (*p) {
            if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
                  l *= 1024;
            else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
                  l *= 1024*1024*1024;
            else if (!strcasecmp(p, "gbit"))
                  l *= 1024*1024*1024/8;
            else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
                  l *= 1024*1024;
            else if (!strcasecmp(p, "mbit"))
                  l *= 1024*1024/8;
            else if (!strcasecmp(p, "kbit"))
                  l *= 1024/8;
            else if (!strcasecmp(p, "bit"))
                  l /= 8;
            else if (strcasecmp(p, "b") != 0)
                  return -1;
      }

      return l;
}


Generated by  Doxygen 1.6.0   Back to index