Ejemplo n.º 1
0
ssize_t expand_label_list(const char *labels,
                          const char *line,
                          const char *delim,
                          int **array, size_t *array_sz) {
  int i = 0, j = 0;
  size_t labels_len = strlen(labels);
  char *labels_copy = xmalloc(labels_len + 1);
  size_t tokens = 0;
  char *pos, *labels_end;

  /* Tokenize the list of labels.  Possible escape sequences are
   * \\ - translates to a literal backslash
   * \, - translates to a literal comma
   *
   * Back-slash literals aren't required to be escaped unless they are
   * followed by a label-separating comma.
   */
  while (i < labels_len + 1) {
    if (labels[i] == '\0' || labels[i] == '\n' || labels[i] == '\r') {
      labels_copy[j] = '\0';
      tokens++;
      break;
    } else if (labels[i] == '\\') {
      if (labels[i+1] == '\\') {
        labels_copy[j] = '\\';
      } else if (labels[i+1] == ',') {
        labels_copy[j] = ',';
      } else {
        /* untranslatable escape sequence - copy it verbatim. */
        labels_copy[j++] = labels[i];
        labels_copy[j] = labels[i+1];
      }
      i += 2;
    } else if (labels[i] == ',') {
      /* label-separator comma */
      labels_copy[j] = '\0';
      tokens++;
      i++;
    } else {
      labels_copy[j] = labels[i];
      i++;
    }
    j++;
  }
  labels_end = labels_copy + j;

  /* make sure the array can hold all of the indexes */
  if (*array == NULL) {
    *array = xmalloc(sizeof(int) * tokens);
    *array_sz = tokens;
  } else {
    if (*array_sz < tokens) {
      *array_sz = arr_resize((void **) array, sizeof(int),
                             *array_sz, tokens - *array_sz);
      if (*array_sz == 0)
        return -2;
    }
  }

  j = 0;
  /* search for each label in the header line */
  for (pos = labels_copy; pos != labels_end + 1; pos += strlen(pos) + 1) {
    i = field_str(pos, line, delim);
    if (i < 0)
      return -1;
    (*array)[j++] = i + 1;
  }

  free(labels_copy);
  return j;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	struct array *arr = NULL;
	unsigned int i = 0;
	unsigned int x = 0;
	unsigned int errors = 0;
	srand(time(NULL));
	printf("*********************************************\n");
	printf(" TEST 1\n");
	printf(" Normal array work. Size = %d\n",SIZE1);
	printf("*********************************************\n");
	errors = 0;
	printf("\tCreating ...\n");
	arr = arr_create(SIZE1);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tSetting %d elements ...\n",SIZE1+4);
    for (i = 0; i < SIZE1; i++)
    {
    	x = rand()%MAXELEM;
    	if (arr_setitem(arr, 0, x) < 0) errors++;
    }
    if (arr_setitem(arr, SIZE1-1, 0) < 0) errors++;
    if (arr_setitem(arr, 1, 1) < 0) errors++;
    if (arr_setitem(arr, 1, 1) < 0) errors++;
    if (arr_setitem(arr, 1, MAXELEM+1) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tGetting %d elements ...\n",SIZE1);
    for (i = 0; i < SIZE1; i++)
    {
    	if (arr_getitem(arr,i,&x) < 0) errors++;
    }
	printf("\tDone with %d errors.\n\n",errors);
	errors=0;
	printf("\tIterator ...\n");
	if (arr_for_each(arr,(*sqr), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	arr_delete(arr);


	printf("*********************************************\n");
	printf(" TEST 2\n");
	printf(" Zero array size. Working with empty array. \n");
	printf("*********************************************\n");
	errors = 0;
	printf("\tCreating ...\n");
	arr = arr_create(0);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tSetting 1 element ...\n");
	if (arr_setitem(arr,0,x) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tGetting 1 element ...\n");
	if (arr_getitem(arr,0,&x) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors=0;
	printf("\tIterator ...\n");
	if (arr_for_each(arr,(*sqr), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	arr_delete(arr);


	printf("*********************************************\n");
	printf(" TEST 3\n");
	printf(" Invalid indexes. Size = %d\n",SIZE3);
	printf("*********************************************\n");
	errors = 0;
	printf("\tCreating ...\n");
	arr = arr_create(SIZE3);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tSetting 3 elements ...\n");
	if (arr_setitem(arr,-1,x) < 0) errors++;
	if (arr_setitem(arr,SIZE3,x) < 0) errors++;
	if (arr_setitem(arr,SIZE3+1,x) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tGetting 3 elements ...\n");
	if (arr_getitem(arr,-1,&x) < 0) errors++;
	if (arr_getitem(arr,SIZE3,&x) < 0) errors++;
	if (arr_getitem(arr,SIZE3+1,&x) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	arr_delete(arr);


	printf("*********************************************\n");
	printf(" TEST 4\n");
	printf(" Normal resizing. Size1 = %d, Size2 = %d, Size3 = %d\n", SIZE41, SIZE42, SIZE43);
	printf("*********************************************\n");
	errors = 0;
	printf("\tCreating ...\n");
	arr = arr_create(SIZE41);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tSetting %d elements ...\n",SIZE41);
    for (i = 0; i < SIZE41; i++)
    {
    	x = rand()%MAXELEM;
    	if (arr_setitem(arr, 0, x) < 0) errors++;
    }
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tResizing Array from %d to %d ...\n", SIZE41, SIZE42);
	if (arr_resize(arr, SIZE42) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tResizing Array from %d to %d ...\n", SIZE42, SIZE43);
	if (arr_resize(arr, SIZE43) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	arr_delete(arr);


	printf("*********************************************\n");
	printf(" TEST 5\n");
	printf(" Memory limit. \n");
	printf("*********************************************\n");
	struct rlimit r;
	r.rlim_cur=SIZE5;
	r.rlim_max=2*SIZE5;
	setrlimit(RLIMIT_AS, &r);
	errors = 0;
	printf("\tCreating with size = %d ...\n",SIZE5*5);
	arr = arr_create(SIZE5*5);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	arr_delete(arr);
	printf("\tCreating with size = %d ...\n",SIZE1);
	arr = arr_create(SIZE1);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tSetting %d elements ...\n",SIZE1);
	for (i = 0; i < SIZE1; i++)
	{
	   	x = rand()%MAXELEM;
	   	if (arr_setitem(arr, 0, x) < 0) errors++;
	}
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tResizing Array from %d to %d ...\n", SIZE1, SIZE5*5);
	if (arr_resize(arr, SIZE5*5) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tResizing Array from %d to %d ...\n", SIZE1, SIZE5/2);
	if (arr_resize(arr, SIZE5) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	arr_delete(arr);


	printf("*********************************************\n");
	printf(" TEST 6\n");
	printf(" Resizing to zero size.\n");
	printf("*********************************************\n");
	errors = 0;
	printf("\tCreating with size = %d ...\n",SIZE1);
	arr = arr_create(SIZE1);
	if (arr == NULL) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tSetting %d elements ...\n",SIZE1);
    for (i = 0; i < SIZE1; i++)
    {
    	x = rand()%MAXELEM;
    	if (arr_setitem(arr, 0, x) < 0) errors++;
    }
    printf("\tDone with %d errors.\n\n",errors);
    errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tResizing Array from %d to %d ...\n", SIZE1, 0);
	if (arr_resize(arr, 0) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	errors = 0;
	printf("\tPrinting Array ...\n");
	if (arr_for_each(arr,(*print), NULL) < 0) errors++;
	printf("\tDone with %d errors.\n\n",errors);
	arr_delete(arr);
    return 0;
}
Ejemplo n.º 3
0
ssize_t expand_nums(char *arg, int **array, size_t * array_size) {
  int i;
  char *token;

  if (arg == NULL || strlen(arg) == 0) {
    return 0;
  }

  /* check the string syntax */
  for (i = 0; arg[i] != '\0'; i++) {
    if ((!isdigit(arg[i]))
        && arg[i] != ',' && arg[i] != '-') {
      return -2;
    }
  }
  i = 0;

  if (*array == NULL && *array_size == 0) {
    *array = xmalloc(sizeof(int) * FFUTILS_RESIZE_AMT);
    *array_size = FFUTILS_RESIZE_AMT;
  }

  if (strchr(arg, ',') == NULL && strchr(arg, '-') == NULL) {
    sscanf(arg, "%u", &((*array)[0]));
    return 1;
  }

  token = strtok(arg, ",");

  while (token != NULL) {
    if (i >= *array_size) {
      if ((*array_size = arr_resize((void **) array, sizeof(int),
                                    *array_size, FFUTILS_RESIZE_AMT)) == 0) {
        return -1;
      }
    }

    if (strchr(token, '-') == NULL) {
      sscanf(token, "%u", &((*array)[i]));
      i++;
    } else {
      unsigned int i0, i1, ii;
      sscanf(token, "%u-%u", &i0, &i1);

      /* make sure the array is big enough to hold the range */
      if (*array_size < (i + i1 - i0)) {
        *array_size = arr_resize((void **) array,
                                 sizeof(int), *array_size, i1 - i0);
        if (*array_size == 0) {
          return -1;
        }
      }

      /* add all numbers in the range to the array */
      for (ii = i0; ii <= i1; ii++) {
        (*array)[i++] = ii;
      }
    }

    /* get the next token */
    token = strtok(NULL, ",");
  }
  return i;
}