Exemplo n.º 1
0
void ERROR_define(const char *pattern, const char *arg[])
{
  int i, n;
  uchar c;
  bool subst;

  void _add_char(uchar c)
  {
    if (n >= MAX_ERROR_MSG)
      return;

    ERROR_info.msg[n++] = c;
  }

  void _add_string(const char *s)
  {
    while (*s)
    {
      _add_char(*s);
      s++;
    }
  }

  if ((intptr_t)pattern > 0 && (intptr_t)pattern < 256)
  {
    ERROR_info.code = (int)(intptr_t)pattern;
    pattern = _message[(int)(intptr_t)pattern];
  }
  else
    ERROR_info.code = -1;

  n = 0;

	subst = FALSE;

	if (ERROR_translate)
	{
		int nsubst = 0;
		
		_add_string(pattern);

		for (;;)
		{
			c = *pattern++;
			if (c == 0)
				break;

			if (subst)
			{
				if (c >= '1' && c <= '4')
				{
					c -= '0';
					if (c > nsubst) nsubst = c;
				}
				subst = FALSE;
			}
			else
			{
				if (c == '&')
					subst = TRUE;
			}
		}
		
		for (i = 0; i < nsubst; i++)
		{
			_add_char('\t');
			_add_string(arg[i]);
		}
	}
	else
	{
		for (;;)
		{
			c = *pattern++;
			if (c == 0)
				break;

			if (subst)
			{
				if (c >= '1' && c <= '4')
					_add_string(arg[c - '1']);
				else
				{
					_add_char('&');
					_add_char(c);
				}
				subst = FALSE;
			}
			else
			{
				if (c == '&')
					subst = TRUE;
				else
					_add_char(c);
			}
		}
	}

  _add_char(0);
}
Exemplo n.º 2
0
void _test_serialize()
{
	cmdObj_t *cmd = cmd_array;
//	printf("\n\nJSON serialization tests\n");

	// null list
	_reset_array();
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// parent with a null child
	cmd = _reset_array();
	cmd = _add_parent(cmd, "r");
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// single string element (message)
	cmd = _reset_array();
	cmd = _add_string(cmd, "msg", "test message");
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// string element and an integer element
	cmd = _reset_array();
	cmd = _add_string(cmd, "msg", "test message");
	cmd = _add_integer(cmd, "answer", 42);
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// parent with a string and an integer element
	cmd = _reset_array();
	cmd = _add_parent(cmd, "r");
	cmd = _add_string(cmd, "msg", "test message");
	cmd = _add_integer(cmd, "answer", 42);
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// parent with a null child followed by a final level 0 element (footer)
	cmd = _reset_array();
	cmd = _add_parent(cmd, "r");
	cmd = _add_empty(cmd);
	cmd = _add_string(cmd, "f", "[1,0,12,1234]");	// fake out a footer
	cmd->pv->depth = 0;
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// parent with a single element child followed by empties folowed by a final level 0 element
	cmd = _reset_array();
	cmd = _add_parent(cmd, "r");
	cmd = _add_integer(cmd, "answer", 42);
	cmd = _add_empty(cmd);
	cmd = _add_empty(cmd);
	cmd = _add_string(cmd, "f", "[1,0,12,1234]");	// fake out a footer
	cmd->pv->depth = 0;
	js_serialize_json(cmd_array, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// response object parent with no children w/footer
	cmd_reset_list();								// works with the header/body/footer list
	_add_array(cmd, "1,0,12,1234");					// fake out a footer
	js_serialize_json(cmd_header, tg.out_buf, sizeof(tg.out_buf));
	_printit();

	// response parent with one element w/footer
	cmd_reset_list();								// works with the header/body/footer list
	cmd_add_string("msg", "test message");
	_add_array(cmd, "1,0,12,1234");					// fake out a footer
	js_serialize_json(cmd_header, tg.out_buf, sizeof(tg.out_buf));
	_printit();
}