Пример #1
0
/**
 * \brief Convert string to JSON format
 */
const char *Translator::escapeString(uint16_t length, const uint8_t *field,
	const json_conf *config)
{
	uint32_t idx_output = 0;

	#define ESCAPE_CHAR(ch) { \
		buffer[idx_output++] = '\\'; \
		buffer[idx_output++] = ch; \
	}

	// Beginning of the string
	buffer[idx_output++] = '"';

	for (uint32_t i = 0; i < length; ++i) {
		// All characters from the extended part of ASCII must be escaped
		if (field[i] > 0x7F) {
			snprintf(&buffer[idx_output], 7, "\\u00%02x", field[i]);
			idx_output += 6;
			continue;
		}

		/*
		 * Based on RFC 4627 (Section: 2.5. Strings):
		 * Control characters (i.e. 0x00 - 0x1F), '"' and  '\' must be escaped
		 * using "\"", "\\" or "\uXXXX" where "XXXX" is a hexa value.
		 */
		if (field[i] > 0x1F && field[i] != '"' && field[i] != '\\') {
			// Copy to the output buffer
			buffer[idx_output++] = field[i];
			continue;
		}

		// Copy as escaped character
		switch(field[i]) {
		case '\\': // Reverse solidus
			ESCAPE_CHAR('\\');
			continue;
		case '\"': // Quotation
			ESCAPE_CHAR('\"');
			continue;
		default:
			break;
		}

		if (config->whiteSpaces == false) {
			// Skip white space characters
			continue;
		}

		switch(field[i]) {
		case '\t': // Tabulator
			ESCAPE_CHAR('t');
			break;
		case '\n': // New line
			ESCAPE_CHAR('n');
			break;
		case '\b': // Backspace
			ESCAPE_CHAR('b');
			break;
		case '\f': // Form feed
			ESCAPE_CHAR('f');
			break;
		case '\r': // Return
			ESCAPE_CHAR('r');
			break;
		default: // "\uXXXX"
			snprintf(&buffer[idx_output], 7, "\\u00%02x", field[i]);
			idx_output += 6;
			break;
		}
	}
	#undef ESCAPE_CHAR

	// End of the string
	buffer[idx_output++] = '"';
	buffer[idx_output] = '\0';
	return buffer;
}
Пример #2
0
int
buffer_in ()
{
  unsigned int c;
  int ret;
  unsigned int romkan;
  int ignored = 0;
  int in;
  unsigned int *output;

  while (1)
    {
      in = keyin ();
      output = romkan_henkan (in);
      conv_ltr_to_ieuc (output);
      if (input_func)
        (*input_func) (output, output);
      for (; *output != EOLTTR; output++)
        {
          c = ((romkan = *output) & 0x0000ffff);
          if (isSPCL (romkan))
            {
              if (romkan == REDRAW)
                {
                  (*c_b->redraw_fun) ();
                  continue;
                }
              else if (romkan == CHMSIG)
                {               /* mode changed */
                  if (redraw_when_chmsig_func)
                    (*redraw_when_chmsig_func) ();
                  disp_mode ();
                  continue;
                }
              else if (romkan == NISEBP)
                {               /* error */
                  ring_bell ();
                  continue;
                }
              else
                {
                  continue;     /* if (romkan == EOLTTR) */
                }
            }
          if (ignored)
            {
              if (isNISE (romkan))
                {
                  if (c == rubout_code)
                    {
                      --ignored;
                    }
                  else
                    {
                      ++ignored;
                    }
                }
              else
                {
                  ignored = 0;
                }
              continue;
            }
          if (isNISE (romkan) && (ESCAPE_CHAR (c) || NOT_NORMAL_CHAR (c)) && c != rubout_code)
            {
              ++ignored;
              continue;
            }

          if (c < TBL_SIZE && c_b->key_table[c])
            {
              if (c_b->rk_clear_tbl[c])
                romkan_clear ();
              ret = (*c_b->key_table[c]) (c, romkan);
            }
          else if (c >= 0x80 && c <= 0x9f)
            {
              ret = 0;
              ring_bell ();
            }
          else if (!(ESCAPE_CHAR (c)) || c == 0x09)
            {                   /* TAB is not ESCAPE char */
              if (c_b->maxlen < c_b->buflen)
                {
                  if (c_b->key_in_fun)
                    {
                      ret = (*c_b->key_in_fun) (c, romkan);
                    }
                  else
                    {
                      ret = insert_char (c);
                      call_t_redraw_move (c_b->t_c_p + 1, c_b->t_c_p, c_b->maxlen, 0, 1);
                      check_scroll ();
                      kk_cursor_normal ();
                    }
                }
              else
                {
                  romkan_clear ();
                  ret = 0;
                }
            }
          else
            {
              if (c_b->ctrl_code_fun)
                {
                  ret = (*c_b->ctrl_code_fun) (c, romkan);
                }
              else
                {
                  ret = 0;
                  ring_bell ();
                }
            }
          if (ret == 1)
            {
              return (0);
            }
          else if (ret == -1)
            {
              return (-1);
            }
        }
    }
}