FreeBSD/usr.sbin/nandsim/nandsim_cfgparse.c
/* Parse given ',' separated list of bytes into buffer. */
Parse given ',' separated list of bytes into buffer.
Parse given ' ' separated list of bytes into buffer .
int
parse_intarray(char *array, int **buffer)
{
	char *tmp, *tmpstr, *origstr;
	unsigned int currbufp = 0, i;
	unsigned int count = 0, from  = 0, to = 0;
	if (array[0] == '[')
		array ++;
	if (array[strlen(array)-1] == ']')
		array[strlen(array)-1] = ',';
	from = strlen(array);
	origstr = (char *)malloc(sizeof(char) * from);
	strcpy(origstr, array);
	tmpstr = (char *)strtok(array, ",");
	while (tmpstr != NULL) {
		errno = 0;
		if ((tmp = strchr(tmpstr, '-')) != NULL) {
			*tmp = ' ';
			if (convert_arguint(tmpstr, &from) ||
			    convert_arguint(tmp, &to)) {
				free(origstr);
				return (EINVAL);
			}
			count += to - from + 1;
		} else {
			if (convert_arguint(tmpstr, &from)) {
				free(origstr);
				return (EINVAL);
			}
			count++;
		}
		tmpstr = (char *)strtok(NULL, ",");
	}
	if (count == 0)
		goto out;
	tmpstr = (char *)strtok(origstr, ",");
	*buffer = malloc(count * sizeof(int));
	while (tmpstr != NULL) {
		errno = 0;
		if ((tmp = strchr(tmpstr, '-')) != NULL) {
			*tmp = ' ';
			from = strtol(tmpstr, NULL, 0);
			to = strtol(tmp, NULL, 0);
			tmpstr = strtok(NULL, ",");
			for (i = from; i <= to; i ++)
				(*buffer)[currbufp++] = i;
			continue;
		}
		errno = 0;
		from = (int)strtol(tmpstr, NULL, 0);
		(*buffer)[currbufp++] = from;
		tmpstr = (char *)strtok(NULL, ",");
	}
out:
	free(origstr);
	return (count);
}
int parse_intarray ( char * array , int * * buffer ) { char * tmp , * tmpstr , * origstr ; unsigned int currbufp = 0 , i ; unsigned int count = 0 , from = 0 , to = 0 ; if ( array [ 0 ] == ' ' ) array ++ ; if ( array [ strlen ( array ) - 1 ] == ' ' ) array [ strlen ( array ) - 1 ] = ' ' ; from = strlen ( array ) ; origstr = ( char * ) malloc ( sizeof ( char ) * from ) ; strcpy ( origstr , array ) ; tmpstr = ( char * ) strtok ( array , " " ) ; while ( tmpstr != NULL ) { errno = 0 ; if ( ( tmp = strchr ( tmpstr , ' ' ) ) != NULL ) { * tmp = ' ' ; if ( convert_arguint ( tmpstr , & from ) || convert_arguint ( tmp , & to ) ) { free ( origstr ) ; return ( EINVAL ) ; } count += to - from + 1 ; } else { if ( convert_arguint ( tmpstr , & from ) ) { free ( origstr ) ; return ( EINVAL ) ; } count ++ ; } tmpstr = ( char * ) strtok ( NULL , " " ) ; } if ( count == 0 ) goto out ; tmpstr = ( char * ) strtok ( origstr , " " ) ; * buffer = malloc ( count * sizeof ( int ) ) ; while ( tmpstr != NULL ) { errno = 0 ; if ( ( tmp = strchr ( tmpstr , ' ' ) ) != NULL ) { * tmp = ' ' ; from = strtol ( tmpstr , NULL , 0 ) ; to = strtol ( tmp , NULL , 0 ) ; tmpstr = strtok ( NULL , " " ) ; for ( i = from ; i <= to ; i ++ ) ( * buffer ) [ currbufp ++ ] = i ; continue ; } errno = 0 ; from = ( int ) strtol ( tmpstr , NULL , 0 ) ; ( * buffer ) [ currbufp ++ ] = from ; tmpstr = ( char * ) strtok ( NULL , " " ) ; } out : free ( origstr ) ; return ( count ) ; }
Parse given ',' separated list of bytes into buffer.
Parse given ' ' separated list of bytes into buffer .
/* Remove square braces */ /* First loop checks for how big int array we need to allocate */ /* Allocate buffer of ints */ /* Second loop is just inserting converted values into int array */