Example #1
0
isotopes_t *load_isotope_table(char *filename) {
    char *line, *line_split;
    char *columns[6];
    char **col;
    char *name=calloc(MAX_ELEMENT_NAME,sizeof(char));
    FILE *in_file=fopen(filename, "r");
    if(!in_file) {
        fprintf(stderr, "Could not load isotope table from file %s\n", filename);
        return NULL;
    }
    line=malloc(sizeof(char)*MASSES_LINE_LENGTH);
    if(!line) 
        return NULL;
    isotopes_t *isotopes=malloc(sizeof(isotopes_t));
    if(!isotopes)
        return NULL;
    isotopes->n_isotopes=0;
    isotopes->i = malloc(sizeof(isotope_t)*MASSES_MAX_ISOTOPES);
    if(!isotopes->i)
        return NULL;
    while(fgets(line, MASSES_LINE_LENGTH, in_file) != NULL) {
        line_split=line; /* strsep will screw up line_split, reset for every new line */
        for (col = columns; (*col = strsep(&line_split, " \t")) != NULL;)
            if (**col != '\0')
                if (++col >= &columns[6])
                    break;
        snprintf(name, MAX_ELEMENT_NAME, "%i-%s", (int)strtol(columns[2], NULL, 10), columns[3]);
        add_isotope_to_table(isotopes, strtoimax(columns[1], NULL, 10), strtoimax(columns[0], NULL, 10), strtoimax(columns[2], NULL, 10), name, strtod(columns[4], NULL)/1e6, strtod(columns[5], NULL)/1e2);
    }
    fclose(in_file);
    return isotopes;
}
Example #2
0
static void
slow_get_thread_bounds (guint8 *current, guint8 **staddr, size_t *stsize)
{
	char buff [1024];
	FILE *f = fopen ("/proc/self/maps", "r");
	if (!f)
		g_error ("Could not determine thread bounds, failed to open /proc/self/maps");

	while (fgets (buff, sizeof (buff), f)) {
		intmax_t low, high;
		char *ptr = buff;
		char *end = NULL;
		//each line starts with the range we want: f7648000-f7709000
		low = strtoimax (ptr, &end, 16);
		if (end) {
			ptr = end + 1; //skip the dash to make sure we don't get a negative number
			end = NULL;
			high = strtoimax (ptr, &end, 16);
		}
		if (end && low <= (intmax_t)(size_t)current && high > (intmax_t)(size_t)current) {
			*staddr = (guint8 *)(size_t)low;
			*stsize = (size_t)(high - low);
			fclose (f);
			return;
		}
	}
	g_error ("Could not determine thread bounds, failed to find current stack pointer in /proc/self/maps");
}
/*
 * Parse a range specification
 */
static int
parse_range(const char *spec, struct range *range)
{
	const char *delim;
	char *end;

	/* @ means invert the range */
	if (*spec == '@') {
		++spec;
		range->inverted = 1;
	} else {
		range->inverted = 0;
	}

	/* empty spec... */
	if (*spec == '\0')
		return (-1);

	if ((delim = strchr(spec, ':')) != NULL) {
		/*
		 * The Nagios plugin documentation says nothing about how
		 * to interpret ":N", so we disallow it.  Allowed forms
		 * are "~:N", "~:", "M:" and "M:N".
		 */
		if (delim - spec == 1 && *spec == '~') {
			range->lo = INTMAX_MIN;
		} else {
			range->lo = strtoimax(spec, &end, 10);
			if (end != delim)
				return (-1);
		}
		if (*(delim + 1) != '\0') {
			range->hi = strtoimax(delim + 1, &end, 10);
			if (*end != '\0')
				return (-1);
		} else {
			range->hi = INTMAX_MAX;
		}
	} else {
		/*
		 * Allowed forms are N
		 */
		range->lo = 0;
		range->hi = strtol(spec, &end, 10);
		if (*end != '\0')
			return (-1);
	}

	/*
	 * Sanity
	 */
	if (range->lo > range->hi)
		return (-1);

	range->defined = 1;
	return (0);
}
TEST(inttypes, strtoimax_EINVAL) {
  errno = 0;
  strtoimax("123", NULL, -1);
  ASSERT_EQ(EINVAL, errno);
  errno = 0;
  strtoimax("123", NULL, 1);
  ASSERT_EQ(EINVAL, errno);
  errno = 0;
  strtoimax("123", NULL, 37);
  ASSERT_EQ(EINVAL, errno);
}
Example #5
0
char * parseIt(char * text)
{
    char * target;

    if (!strstr(text, "\\x"))
    {
        target = strdup(text);
        return target;
    }

    char * hex = "0123456789abcdef";
    target = malloc(strlen(text) + 1);
    char * start = text;
    char * end = text;

    while (end = strstr(start, "\\x"))
    {
        unsigned char text[3] = {0};
        unsigned char value[2] = {0};

        strncat(target, start, end - start);
        text[0] = end[2];
        text[1] = end[3];
        value[0] =  strtoimax(text, NULL, 10);
        strcat(target, value);
//        strncat(target, start, end - start);
        start = end + 4;
    }
    strcat(target, start);
    return target;
}
Example #6
0
bool Nibbler::getInt (int& result)
{
  auto i = _cursor;

  if (i < _length)
  {
    if ((*_input)[i] == '-')
      ++i;
    else if ((*_input)[i] == '+')
      ++i;
  }

  // TODO Potential for use of find_first_not_of
  while (i < _length && Lexer::isDigit ((*_input)[i]))
    ++i;

  if (i > _cursor)
  {
    result = strtoimax (_input->substr (_cursor, i - _cursor).c_str (), NULL, 10);
    _cursor = i;
    return true;
  }

  return false;
}
int processArguments(int argc, char* argv[]) {

int i;
u_int temp;

for(i=1; i<argc; i++) {
if(strcmp(argv[i],"-h")==0) {
printHelp(argv[0],false);
return true;
}

if(strcmp(argv[i],"-p")==0) {
temp = strtoimax(argv[++i], NULL, 10);
if(temp == 0 || temp < MINPORT || temp > MAXPORT) {
printHelp(argv[0],true,"Port out of range\n");
return false;
}
port = temp;
} else if(strcmp(argv[i],"-d")==0) {
strncpy(baseDir,argv[++i],254);
} else if(strstr(argv[i],"-v")!=NULL) {
debugLevel = strlen(argv[i])-1;
} else {
printHelp(argv[0],true,"Unkown option\n");
return false;
}
}

debug(2,"%s","Configuraciones:");
debug(2,"Puerto:\t\t%u",port);
debug(2,"Base Directory:\t%s",baseDir);

return true;
}
Example #8
0
bool Nibbler::getHex (int& result)
{
  std::string::size_type i = mCursor;

  if (i < mLength)
  {
    if (mInput[i] == '-')
      ++i;
    else if (mInput[i] == '+')
      ++i;
  }

  // TODO Potential for use of find_first_not_of
  while (i < mLength && isxdigit (mInput[i]))
    ++i;

  if (i > mCursor)
  {
    result = strtoimax (mInput.substr (mCursor, i - mCursor).c_str (), NULL, 16);
    mCursor = i;
    return true;
  }

  return false;
}
Example #9
0
const int Config::getInteger (const std::string& key)
{
  if ((*this).find (key) != (*this).end ())
    return strtoimax ((*this)[key].c_str (), NULL, 10);

  return 0;
}
Example #10
0
static int
l_filter_reject_code(lua_State *L)
{
	uint64_t	id;
	const char	*s_id;
	uint32_t	action;
	uint32_t	code;
	const char	*line;

	if (lua_gettop(L) != 4)
		return (0);

	s_id = luaL_checklstring(L, 1, NULL);
	id = strtoimax(s_id, (char **)NULL, 10);
	action = luaL_checkinteger(L, 2);
	code = luaL_checkinteger(L, 3);
	line = luaL_checklstring(L, 4, NULL);

	switch (action) {
	case FILTER_FAIL:
	case FILTER_CLOSE:
		filter_api_reject_code(id, action, code, line);
		break;
	}

	return (0);
}
int main(int args, char* arg_v[]){

  if (args != 2) {
    printf("Usage: ./CLRS index_of_fibonacci\n");
    return 0;
  }
  char *endptr;
  int index = strtoimax(arg_v[1], &endptr, 10);
  if (*endptr != '\0') {
    printf("Usage: ./CLRS index_of_fibonacci\n\n");
    printf("       index_of_fibonacci need to be a number.[%s]\n", endptr);
    return 0;
  }

#if defined(__LP64__) || defined(_WIN64)
  // LP64 machine, OS X or Linux
  if (index > 93) {
    printf("ERROR: fib(%d) is larger than UINT64_MAX \n", index);
    return 0;
  }
#else
  // 32-bit machine, Windows or Linux or OS X
  if (index > 47) {
    printf("ERROR: fib(%d) is larger than UINT32_MAX \n", index);
    return 0;
  }
#endif

  doFibonacci_matrix(index);
  return 0;
}
Example #12
0
int64_t sysfs_value_read(char *path)
{
	char buffer[100];
	int64_t value;
	int fd = -1;
	int rc;

	if (path == NULL)
		return -1;

	fd = open(path, O_RDONLY);
	if (fd < 0)
		goto error;

	rc = read(fd, &buffer, sizeof(buffer));
	if (rc <= 0)
		goto error;

	value = (int64_t)strtoimax(buffer, NULL, 10);
	goto complete;

error:
	value = -1;

complete:
	if (fd >= 0)
		close(fd);

	return value;
}
Example #13
0
static void
read_negative_num (FILE *fp, intmax_t min_val, intmax_t *pval)
{
  int c;
  size_t i;
  char buf[INT_BUFSIZE_BOUND (intmax_t)];
  char *ep;
  buf[0] = '-';

  for (i = 1; ISDIGIT (c = getc (fp)); i++)
    {
      if (i == sizeof buf - 1)
	FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
      buf[i] = c;
    }

  if (c < 0)
    {
      if (ferror (fp))
	FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
      else
	FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
    }

  buf[i] = 0;
  errno = 0;
  *pval = strtoimax (buf, &ep, 10);
  if (c || errno || *pval < min_val)
    FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
}
Example #14
0
static bool
decompose_filename(const char *filename, struct partqueue *pq)
{
  const char *p;
  char *q;

  if (strspn(filename, "0123456789abcdef") != MD5HASHLEN ||
      filename[MD5HASHLEN] != '.')
    return false;
  q = nfmalloc(MD5HASHLEN + 1);
  memcpy(q, filename, MD5HASHLEN);
  q[MD5HASHLEN] = '\0';
  pq->info.md5sum= q;
  p = filename + MD5HASHLEN + 1;
  pq->info.maxpartlen = strtoimax(p, &q, 16);
  if (q == p || *q++ != '.')
    return false;
  p = q;
  pq->info.thispartn = (int)strtol(p, &q, 16);
  if (q == p || *q++ != '.')
    return false;
  p = q;
  pq->info.maxpartn = (int)strtol(p, &q, 16);
  if (q == p || *q)
    return false;
  return true;
}
Example #15
0
int parse_simple(char *start, char *end, Atom *result)
{
	char *p;
	int value = strtoimax(start, &p, 10);
	/*it is integer*/
	if(p == end) {
		result->type = ATOM_INTEGER;
		result->value.integer = value;
		//return true;
		return ERROR_OK;
	}

	/*nil or symbol*/
	char *buf = malloc(end - start + 1);
	p = buf;
	while(start != end) {
		*p++= *start++;
	}
	*p = '\0';
	/*convert to uppercase*/
	convertToUpperCase(buf);
	if(strcmp(buf, "NIL") == 0)
		*result = nil;
	else
		*result = make_symbol(buf);
	free(buf);
	return ERROR_OK;

}
Example #16
0
/*
 * Convert a string into an integer of type intmax_t.  Alow trailing spaces.
 */
intmax_t atomax(const char *s, int base)
{
	char *p;
	intmax_t r;

	errno = 0;
	r = strtoimax(s, &p, base);

	if (errno != 0)
		badnum(s);

	/*
	 * Disallow completely blank strings in non-arithmetic (base != 0)
	 * contexts.
	 */
	if (p == s && base)
		badnum(s);

	while (isspace((unsigned char)*p))
	      p++;

	if (*p)
		badnum(s);

	return r;
}
Example #17
0
/**
 * Get the specified numerical config value, subject to limits.
 */
intmax_t
r_init_confval(struct ndnr_handle *h, const char *key,
                     intmax_t lo, intmax_t hi, intmax_t deflt) {
    const char *s;
    intmax_t v;
    char *ep;
    
    if (!(lo <= deflt && deflt <= hi))
        abort();
    s = getenv(key);
    if (s != NULL && s[0] != 0) {
        ep = "x";
        v = strtoimax(s, &ep, 10);
        if (v != 0 || ep[0] == 0) {
            if (v > hi)
                v = hi;
            if (v < lo)
                v = lo;
            if (NDNSHOULDLOG(h, mmm, NDNL_FINEST))
                ndnr_msg(h, "Using %s=%jd", key, v);
            return(v);
        }
    }
    return (deflt);
}
Example #18
0
intmax_t
strtosysint (char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
{
  errno = 0;
  if (maxval <= INTMAX_MAX)
    {
      if (ISDIGIT (arg[*arg == '-']))
	{
	  intmax_t i = strtoimax (arg, arglim, 10);
	  intmax_t imaxval = maxval;
	  if (minval <= i && i <= imaxval)
	    return i;
	  errno = ERANGE;
	  return i < minval ? minval : maxval;
	}
    }
  else
    {
      if (ISDIGIT (*arg))
	{
	  uintmax_t i = strtoumax (arg, arglim, 10);
	  if (i <= maxval)
	    return represent_uintmax (i);
	  errno = ERANGE;
	  return maxval;
	}
    }

  errno = EINVAL;
  return 0;
}
Example #19
0
intmax_t get_arg(char *argv[], int pos)
{
	intmax_t val = strtoimax(argv[pos], NULL, 10);
	if (errno == EINVAL || errno == ERANGE)  {
		usage(argv, pos);
	}
	return val;
}
Example #20
0
int
main(int argc, char *argv[])
{
	int ch;
	FILE *fp;
	int first, linecnt = -1, eval = 0;
	off_t bytecnt = -1;
	char *ep;

	obsolete(argv);
	while ((ch = getopt(argc, argv, "n:c:")) != -1)
		switch(ch) {
		case 'c':
			bytecnt = strtoimax(optarg, &ep, 10);
			if (*ep || bytecnt <= 0)
				errx(1, "illegal byte count -- %s", optarg);
			break;
		case 'n':
			linecnt = strtol(optarg, &ep, 10);
			if (*ep || linecnt <= 0)
				errx(1, "illegal line count -- %s", optarg);
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (linecnt != -1 && bytecnt != -1)
		errx(1, "can't combine line and byte counts");
	if (linecnt == -1 )
		linecnt = 10;
	if (*argv) {
		for (first = 1; *argv; ++argv) {
			if ((fp = fopen(*argv, "r")) == NULL) {
				warn("%s", *argv);
				eval = 1;
				continue;
			}
			if (argc > 1) {
				(void)printf("%s==> %s <==\n",
				    first ? "" : "\n", *argv);
				first = 0;
			}
			if (bytecnt == -1)
				head(fp, linecnt);
			else
				head_bytes(fp, bytecnt);
			(void)fclose(fp);
		}
	} else if (bytecnt == -1)
		head(stdin, linecnt);
	else
		head_bytes(stdin, bytecnt);

	exit(eval);
}
Example #21
0
static void
_xml_end_cb(void *data, const char *el)
{
	XML_Parser p;

	p = data;

	if (!strcmp(el, "ic")) {
		if (_cur_ic == NULL)
			errx(1, "bogus IC end tag at line %jd",
			    (intmax_t) XML_GetCurrentLineNumber(p));
		STAILQ_INSERT_TAIL(&_iclist, _cur_ic, next);
		_cur_ic = NULL;
	} else if (!strcmp(el, "tp")) {
		if (_cur_tp == NULL)
			errx(1, "bogus TP end tag at line %jd",
			    (intmax_t) XML_GetCurrentLineNumber(p));
		assert(_cur_ic != NULL);
		_test_cnt++;
		_cur_tp->testnum = _test_cnt;
		STAILQ_INSERT_TAIL(&_cur_ic->tplist, _cur_tp, next);
		_cur_ic->tpcnt++;
		_cur_tp = NULL;
	} else if (!strcmp(el, "vc")) {
		if (_cur_vc == NULL)
			errx(1, "bogus VC end tag at line %jd",
			    (intmax_t) XML_GetCurrentLineNumber(p));
		if (_xml_data_pos == 0 && _cur_vc->vt != _VTYPE_STRING)
			errx(1, "VC element without value defined at line %jd",
			    (intmax_t) XML_GetCurrentLineNumber(p));
		_xml_data[_xml_data_pos] = '\0';
		switch (_cur_vc->vt) {
		case _VTYPE_INT:
			_cur_vc->v.i64 = strtoimax(_xml_data, NULL, 0);
			break;
		case _VTYPE_UINT:
			_cur_vc->v.u64 = strtoumax(_xml_data, NULL, 0);
			break;
		case _VTYPE_STRING:
			_cur_vc->v.str = strdup(_xml_data);
			if (_cur_vc->v.str == NULL)
				err(1, "strdup");
			break;
		case _VTYPE_BLOCK:
			driver_base64_decode(_xml_data, _xml_data_pos,
			    &_cur_vc->v.b.data, &_cur_vc->v.b.len);
			break;
		default:
			assert(0);
			break;
		}
		_xml_data_pos = 0;

		assert(_cur_tp != NULL);
		STAILQ_INSERT_TAIL(&_cur_tp->vclist, _cur_vc, next);
		_cur_vc = NULL;
	}
}
int processArguments(int argc, char* argv[]) {

	int i;
	u_int temp;
	
	if(argc < 3) {
		printHelp(argv[0],true,"Faltan Argumentos\n");
		return false;
	}	

    if(strcmp(argv[1],"-r")==0){
		mode = SERVER;
	} else if(strcmp(argv[1],"-t")==0) {
		mode = CLIENT;
	} else {
		printHelp(argv[0],true,"Unkown Mode\n");
		return false;
	}
	
	for(i=2; i<(argc-1); i++) {
		if(strcmp(argv[i],"-h")==0) {
			printHelp(argv[0],false);
			return true;
		}	
		
		if(strcmp(argv[i],"-p")==0) {
			temp = strtoimax(argv[++i], NULL, 10);
			if(temp == 0 || temp < MINPORT || temp > MAXPORT) {
				printHelp(argv[0],true,"Port out of range\n");
				return false;
			}
			port = temp;
		} else if(strcmp(argv[i],"-d")==0) {
			stpncpy(ip_dst,argv[++i],18);
		} else if(strstr(argv[i],"-v")!=NULL) {
			debugLevel = strlen(argv[i])-1;
		} else if(strcmp(argv[i],"-i")==0) {
			stpncpy(ip_listen,argv[++i],18);	
		} else {
			printHelp(argv[0],true,"Unkown option\n");
			return false;
		}	
	}
	
	stpncpy(filename,argv[i],254);

	debug(2,"%s","\tConfiguraciones:");
	if(mode == SERVER) {
		debug(2,"\t\tModo:\t%s","Servidor");
		debug(2,"\t\tListen on:\t%s:%u",ip_listen,port);
	} else if (mode == CLIENT) {	
		debug(2,"\t\tModo:\t%s","Cliente");
		debug(2,"\t\tDestino:\t%s:%u",ip_dst,port);
	}
	debug(2,"\t\tFileName:\t%s",filename);
	
	return true;
}
Example #23
0
void str_to_uint16(char *str, uint16_t *res, uint8_t base) {
  char *end;
  errno = 0;
  intmax_t val = strtoimax(str, &end, base);
  if (errno == ERANGE || val < 0 || val > UINT16_MAX ||
    end == str || *end != '\0')
    return;
  *res = (uint16_t) val;
}
Example #24
0
static int str_to_uint16(const char *str, uint16_t *res) {
    char *end;
    errno = 0;
    intmax_t val = strtoimax(str, &end, 10);
    if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == str || *end != '\0')
        return 0;
    *res = (uint16_t) val;
    return 1;
}
    static int netlink_init_interfaces_list(void)
    {
        int ret = -1;
        DIR  *netdir;
        struct dirent *de;
        char path[SYSFS_PATH_MAX];
        interface_info_t *intfinfo;
        int index;
        FILE *ifidx;
        #define MAX_FGETS_LEN 4
        char idx[MAX_FGETS_LEN+1];

        if ((netdir = opendir(SYSFS_CLASS_NET)) != NULL) {
             while ((de = readdir(netdir))) {
                if ((!strcmp(de->d_name, ".")) || (!strcmp(de->d_name, ".."))
                    ||(!strcmp(de->d_name, "lo")) || (!strcmp(de->d_name, "wmaster0")) ||
                    (!strcmp(de->d_name, "pan0")))
                    continue;
                snprintf(path, SYSFS_PATH_MAX,"%s/%s/phy80211", SYSFS_CLASS_NET, de->d_name);
                if (!access(path, F_OK))
                    continue;
                snprintf(path, SYSFS_PATH_MAX,"%s/%s/wireless", SYSFS_CLASS_NET, de->d_name);
                if (!access(path, F_OK))
                        continue;

                snprintf(path, SYSFS_PATH_MAX,"%s/%s/ifindex", SYSFS_CLASS_NET, de->d_name);
                if ((ifidx = fopen(path, "r")) != NULL) {
                    memset(idx, 0, MAX_FGETS_LEN+1);
                    if (fgets(idx,MAX_FGETS_LEN, ifidx) != NULL) {
                        index = strtoimax(idx, NULL, 10);
                    } else {
                        LOGE("Can not read %s", path);
                        continue;
                    }
                } else {
                    LOGE("Can not open %s for read", path);
                    continue;
                }
                /* make some room! */
                intfinfo = (interface_info_t *)malloc(sizeof(interface_info_t));
                if (intfinfo == NULL) {
                    LOGE("malloc in netlink_init_interfaces_table");
                    goto error;
                }
                /* copy the interface name (eth0, eth1, ...) */
                intfinfo->name = strndup((char *) de->d_name, SYSFS_PATH_MAX);
                intfinfo->i = index;
                LOGI("interface %s:%d found", intfinfo->name, intfinfo->i);
                add_int_to_list(intfinfo);
            }
            closedir(netdir);
        }
        ret = 0;

    error:
        return ret;
    }
Example #26
0
static inline int ini_process_short(const char* curr_name, const char* value, const char* option_name, short* target, ini_value_list_type* value_names) {
    if(strcasecmp(curr_name, option_name) == 0) {
        if (strcasecmp(value, "default") != 0) {
            int named_value = ini_get_named_value(value, value_names);
            *target = (named_value == INI_NO_VALID_NAME) ? ((short) strtoimax(value, NULL, 0)) : ((short) named_value);
        }
        return 1; // finished; don't look for more possible options that curr_name can be
    }
    return 0; // not the right option; should check another option_name
}
int main()
{
    {
    int8_t  i1 = 0;
    int16_t i2 = 0;
    int32_t i3 = 0;
    int64_t i4 = 0;
    }
    {
    uint8_t  i1 = 0;
    uint16_t i2 = 0;
    uint32_t i3 = 0;
    uint64_t i4 = 0;
    }
    {
    int_least8_t  i1 = 0;
    int_least16_t i2 = 0;
    int_least32_t i3 = 0;
    int_least64_t i4 = 0;
    }
    {
    uint_least8_t  i1 = 0;
    uint_least16_t i2 = 0;
    uint_least32_t i3 = 0;
    uint_least64_t i4 = 0;
    }
    {
    int_fast8_t  i1 = 0;
    int_fast16_t i2 = 0;
    int_fast32_t i3 = 0;
    int_fast64_t i4 = 0;
    }
    {
    uint_fast8_t  i1 = 0;
    uint_fast16_t i2 = 0;
    uint_fast32_t i3 = 0;
    uint_fast64_t i4 = 0;
    }
    {
    intptr_t  i1 = 0;
    uintptr_t i2 = 0;
    intmax_t  i3 = 0;
    uintmax_t i4 = 0;
    }
    {
    imaxdiv_t  i1 = {0};
    }
    intmax_t i = 0;
    static_assert((std::is_same<decltype(imaxabs(i)), intmax_t>::value), "");
    static_assert((std::is_same<decltype(imaxdiv(i, i)), imaxdiv_t>::value), "");
    static_assert((std::is_same<decltype(strtoimax("", (char**)0, 0)), intmax_t>::value), "");
    static_assert((std::is_same<decltype(strtoumax("", (char**)0, 0)), uintmax_t>::value), "");
    static_assert((std::is_same<decltype(wcstoimax(L"", (wchar_t**)0, 0)), intmax_t>::value), "");
    static_assert((std::is_same<decltype(wcstoumax(L"", (wchar_t**)0, 0)), uintmax_t>::value), "");
}
Example #28
0
status_t
IntegerValueFormatter::_ValidateSigned(const BString& input, type_code type,
                                       ::Value*& _output, bool wantsValue) const
{
    const char* text = input.String();
    char *parseEnd = NULL;
    intmax_t parsedValue = strtoimax(text, &parseEnd, 10);
    if (parseEnd - text < input.Length() && !isspace(*parseEnd))
        return B_NO_MEMORY;

    BVariant newValue;
    switch (type) {
    case B_INT8_TYPE:
    {
        if (parsedValue < INT8_MIN || parsedValue > INT8_MAX)
            return B_BAD_VALUE;

        newValue.SetTo((int8)parsedValue);
        break;
    }
    case B_INT16_TYPE:
    {
        if (parsedValue < INT16_MIN || parsedValue > INT16_MAX)
            return B_BAD_VALUE;

        newValue.SetTo((int16)parsedValue);
        break;
    }
    case B_INT32_TYPE:
    {
        if (parsedValue < INT32_MIN || parsedValue > INT32_MAX)
            return B_BAD_VALUE;

        newValue.SetTo((int32)parsedValue);
        break;
    }
    case B_INT64_TYPE:
    {
        newValue.SetTo((int64)parsedValue);
        break;
    }
    default:
        return B_BAD_VALUE;
    }

    if (wantsValue) {
        _output = new(std::nothrow) IntegerValue(newValue);
        if (_output == NULL)
            return B_NO_MEMORY;
    }

    return B_OK;
}
Example #29
0
static bool _strtoimax(const char *str, int base, intmax_t *out) {
	char *endptr = NULL;
	errno = 0;
	intmax_t ret = strtoimax(str, &endptr, base);
	if (errno != 0) return false;
	if (*endptr != '\0') {
		errno = EINVAL;
		return false;
	}
	*out = ret;
	return true;
}
Example #30
0
/*
 * Convert an expression of the following forms to a int64_t.
 * 	1) A positive decimal number.
 *	2) A positive decimal number followed by a 'b' or 'B' (mult by 1).
 *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
 *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
 *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
 *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
 *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
 *	8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60).
 */
int
expand_number(const char *buf, int64_t *num)
{
	static const char unit[] = "bkmgtpe";
	char *endptr, s;
	int64_t number;
	int i;

	number = strtoimax(buf, &endptr, 0);

	if (endptr == buf) {
		/* No valid digits. */
		errno = EINVAL;
		return (-1);
	}

	if (*endptr == '\0') {
		/* No unit. */
		*num = number;
		return (0);
	}

	s = tolower(*endptr);
	switch (s) {
	case 'b':
	case 'k':
	case 'm':
	case 'g':
	case 't':
	case 'p':
	case 'e':
		break;
	default:
		/* Unrecognized unit. */
		errno = EINVAL;
		return (-1);
	}

	for (i = 0; unit[i] != '\0'; i++) {
		if (s == unit[i])
			break;
		if ((number < 0 && (number << 10) > number) ||
		    (number >= 0 && (number << 10) < number)) {
			errno = ERANGE;
			return (-1);
		}
		number <<= 10;
	}

	*num = number;
	return (0);
}