Exemple #1
0
static int 
regexp_set(struct objlist *obj, N_VALUE *inst, N_VALUE *rval, int argc, char **argv)
{
  struct oregexp_local *local;
  GRegex *regexp;
  char *str;

  _getobj(obj, "_local", inst, &local);

  str = (char *) argv[2];
  if (str == NULL || str[0] == '\0') {
    if (local->regexp) {
      g_regex_unref(local->regexp);
    }
    local->regexp = NULL;

    del_array_element(local->array);

    return 0;
  }

  if (! g_utf8_validate(str, -1, NULL)) {
    error(obj, ERR_INVALID_UTF8);
    return 1;
  }

  regexp = g_regex_new(str, 0, 0, NULL);
  if (regexp == NULL) {
    error(obj, ERR_REGEXP);
    return 1;
  }

  if (local->regexp) {
    g_regex_unref(local->regexp);
  }

  local->regexp = regexp;

  del_array_element(local->array);

  return 0;
}
Exemple #2
0
static int 
regexpdone(struct objlist *obj,N_VALUE *inst,N_VALUE *rval,int argc,char **argv)
{
  struct oregexp_local *local;

  if (_exeparent(obj,(char *)argv[1],inst,rval,argc,argv)) return 1;

  _getobj(obj, "_local", inst, &local);

  if (local->regexp) {
    g_regex_unref(local->regexp);
  }

  if (local->array) {
    del_array_element(local->array);
    arrayfree(local->array);
  }

  return 0;
}
Exemple #3
0
static int 
regexp_match(struct objlist *obj,N_VALUE *inst,N_VALUE *rval,int argc,char **argv)
{
  struct oregexp_local *local;
  GMatchInfo *info;
  int r;
  char *str, **match;

  str = (char *) argv[2];

  rval->i = 0;

  _getobj(obj, "_local", inst, &local);
  if (local->regexp == NULL) {
    return 1;
  }

  del_array_element(local->array);

  if (str == NULL || str[0] == '\0') {
    return 1;
  }

  info = NULL;
  r = g_regex_match(local->regexp, str, 0, &info);
  if (! r) {
    g_match_info_free(info);
    return 1;
  }

  while (g_match_info_matches(info)) {
    match = g_match_info_fetch_all(info);
    arrayadd(local->array, &match);
    g_match_info_next(info, NULL);
  }
  g_match_info_free(info);

  rval->i = arraynum(local->array);

  return 0;
}
Exemple #4
0
/*
BEGIN {
	n = split("one two three four five six", test_array2)
	ret = test_array_elem(test_array2, "3")
	printf("test_array_elem() returned %d, test_array2[3] = %g\n", ret, test_array2[3])
	if ("5" in test_array2)
		printf("error: test_array_elem() did not remove element \"5\"\n")
	else
		printf("test_array_elem() did remove element \"5\"\n")
	if ("7" in test_array2)
		printf("test_array_elem() added element \"7\" --> %s\n", test_array2[7])
	else
		printf("test_array_elem() did not add element \"7\"\n")
	if ("subarray" in test_array2) {
		if (isarray(test_array2["subarray"])) {
			for (i in test_array2["subarray"])
				printf("test_array2[\"subarray\"][\"%s\"] = %s\n",
					i, test_array2["subarray"][i])
		} else
			printf("test_array_elem() added element \"subarray\" as scalar\n")
	} else
		printf("test_array_elem() did not add element \"subarray\"\n")
	print ""
}
*/
static awk_value_t *
test_array_elem(int nargs, awk_value_t *result)
{
	awk_value_t array, index, index2, value;

	make_number(0.0, result);	/* default return until full success */

	assert(result != NULL);

	if (nargs != 2) {
		printf("test_array_elem: nargs not right (%d should be 2)\n", nargs);
		goto out;
	}

	/* look up an array element and print the value */
	if (! get_argument(0, AWK_ARRAY, & array)) {
		printf("test_array_elem: get_argument 0 (array) failed\n");
		goto out;
	}
	if (! get_argument(1, AWK_STRING, & index)) {
		printf("test_array_elem: get_argument 1 (index) failed\n");
		goto out;
	}
	(void) make_const_string(index.str_value.str, index.str_value.len, & index2);
	if (! get_array_element(array.array_cookie, & index2, AWK_UNDEFINED, & value)) {
		printf("test_array_elem: get_array_element failed\n");
		goto out;
	}
	printf("test_array_elem: a[\"%.*s\"] = %s\n",
			(int) index.str_value.len,
			index.str_value.str,
			valrep2str(& value));

	/* change the element - "3" */
	(void) make_number(42.0, & value);
	(void) make_const_string(index.str_value.str, index.str_value.len, & index2);
	if (! set_array_element(array.array_cookie, & index2, & value)) {
		printf("test_array_elem: set_array_element failed\n");
		goto out;
	}

	/* delete another element - "5" */
	(void) make_const_string("5", 1, & index);
	if (! del_array_element(array.array_cookie, & index)) {
		printf("test_array_elem: del_array_element failed\n");
		goto out;
	}

	/* add a new element - "7" */
	(void) make_const_string("7", 1, & index);
	(void) make_const_string("seven", 5, & value);
	if (! set_array_element(array.array_cookie, & index, & value)) {
		printf("test_array_elem: set_array_element failed\n");
		goto out;
	}

	/* add a subarray */
	(void) make_const_string("subarray", 8, & index);
	fill_in_array(& value);
	if (! set_array_element(array.array_cookie, & index, & value)) {
		printf("test_array_elem: set_array_element (subarray) failed\n");
		goto out;
	}

	/* change and deletion should be reflected in awk script */
	make_number(1.0, result);
out:
	return result;
}