Example #1
0
static void
skip_comments (unsigned char **inbuf, unsigned char *inbufend)
{
  while (*inbuf < inbufend && **inbuf == '%') {
    skip_line(inbuf, inbufend);
    skip_white_spaces(inbuf, inbufend);
  }
}
Example #2
0
int
parser_read_uint64_hex(uint64_t *value, const char *p)
{
	char *next;
	uint64_t val;

	p = skip_white_spaces(p);

	val = strtoul(p, &next, 16);
	if (p == next)
		return -EINVAL;

	p = skip_white_spaces(next);
	if (*p != '\0')
		return -EINVAL;

	*value = val;
	return 0;
}
Example #3
0
int
parser_read_uint64(uint64_t *value, const char *p)
{
	char *next;
	uint64_t val;

	p = skip_white_spaces(p);
	if (!isdigit(*p))
		return -EINVAL;

	val = strtoul(p, &next, 10);
	if (p == next)
		return -EINVAL;

	p = next;
	switch (*p) {
	case 'T':
		val *= 1024ULL;
		/* fall through */
	case 'G':
		val *= 1024ULL;
		/* fall through */
	case 'M':
		val *= 1024ULL;
		/* fall through */
	case 'k':
	case 'K':
		val *= 1024ULL;
		p++;
		break;
	}

	p = skip_white_spaces(p);
	if (*p != '\0')
		return -EINVAL;

	*value = val;
	return 0;
}
Example #4
0
int
parser_read_arg_bool(const char *p)
{
	p = skip_white_spaces(p);
	int result = -EINVAL;

	if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) ||
		((p[0] == 'Y') && (p[1] == 'E') && (p[2] == 'S'))) {
		p += 3;
		result = 1;
	}

	if (((p[0] == 'o') && (p[1] == 'n')) ||
		((p[0] == 'O') && (p[1] == 'N'))) {
		p += 2;
		result = 1;
	}

	if (((p[0] == 'n') && (p[1] == 'o')) ||
		((p[0] == 'N') && (p[1] == 'O'))) {
		p += 2;
		result = 0;
	}

	if (((p[0] == 'o') && (p[1] == 'f') && (p[2] == 'f')) ||
		((p[0] == 'O') && (p[1] == 'F') && (p[2] == 'F'))) {
		p += 3;
		result = 0;
	}

	p = skip_white_spaces(p);

	if (p[0] != '\0')
		return -EINVAL;

	return result;
}
Example #5
0
int
parser_read_int32(int32_t *value, const char *p)
{
	char *next;
	int32_t val;

	p = skip_white_spaces(p);
	if (!isdigit(*p))
		return -EINVAL;

	val = strtol(p, &next, 10);
	if (p == next)
		return -EINVAL;

	*value = val;
	return 0;
}
Example #6
0
/* NOTE: the input buffer must be null-terminated, i.e., *inbufend == 0 */
pst_obj *
pst_get_token (unsigned char **inbuf, unsigned char *inbufend)
{
  pst_obj *obj = NULL;
  unsigned char c;

  ASSERT(*inbuf <= inbufend && !*inbufend);

  skip_white_spaces(inbuf, inbufend);
  skip_comments(inbuf, inbufend);
  if (*inbuf >= inbufend)
    return NULL;
  c = **inbuf;
  switch (c) {
#if 0
  case '%':
    obj = pst_parse_comment(inbuf, inbufend);
    break;
#endif
  case '/':
    obj = pst_parse_name(inbuf, inbufend);
    break;
  case '[': case '{': /* This is wrong */
    obj = pst_new_mark();
    (*inbuf)++;
    break;
  case '<':
    if (*inbuf + 1 >= inbufend)
      return NULL;
    c = *(*inbuf+1);
    if (c == '<') {
      obj = pst_new_mark();
      *inbuf += 2;
    } else if (isxdigit(c))
      obj = pst_parse_string(inbuf, inbufend);
    else if (c == '~') /* ASCII85 */
      obj = pst_parse_string(inbuf, inbufend);
    break;
  case '(':
    obj = pst_parse_string(inbuf, inbufend);
    break;
  case '>':
    if (*inbuf + 1 >= inbufend || *(*inbuf+1) != '>') {
      ERROR("Unexpected end of ASCII hex string marker.");
    } else  {
      char *mark;

      mark = NEW(3, char);
      mark[0] = '>'; mark[1] = '>'; mark[2] = '\0';
      obj = pst_new_obj(PST_TYPE_UNKNOWN, mark);
      (*inbuf) += 2;
    }
    break;
  case ']': case '}': 
    {
      char *mark;

      mark = NEW(2, char);
      mark[0] = c; mark[1] = '\0';
      obj = pst_new_obj(PST_TYPE_UNKNOWN, mark);
      (*inbuf)++;
    }
    break;
  default:
    if (c == 't' || c == 'f')
      obj = pst_parse_boolean(inbuf, inbufend);
    else if (c == 'n')
      obj = pst_parse_null(inbuf, inbufend);
    else if (c == '+' || c == '-' || isdigit(c) || c == '.')
      obj = pst_parse_number(inbuf, inbufend);
    break;
  }

  if (!obj) {
    obj = pst_parse_any(inbuf, inbufend);
  }

  return obj;
}
Example #7
0
	Token InputReader::get_next_token() {
		skip_white_spaces();
		
		Token token, raw_token;
		std::string qstr;
		std::string value;

		get_raw_token(raw_token);
		switch(raw_token.type_) {

			case null_token:
				token.type_ = null_token;		// this means no more tokens left
				break;

			case digit_token:
			case negative_token:
				float_t n_value;
				input_stream_.unget();
				if(!read_number(n_value)) {
					std::cerr << "fatal error: failed while reading a number" << std::endl;
					token.type_ = error_token;
				} else {
					token.type_ = number_token;
					token.dvalue_ = n_value;
				} // if-else
				break;

			case object_begin_token:
				token.type_ = object_begin_token;
				structure_stack_.push(object_begin_token);
				break;

			case object_end_token:
				token.type_ = object_end_token;
				if(structure_stack_.top() != object_begin_token) {
					std::cerr << "fatal error: mismatched object encapsulators" << std::endl;
					token.type_ = error_token;
				} else {
					structure_stack_.pop();
				} // if-else
				break;

			case array_begin_token:
				token.type_ = array_begin_token;
				structure_stack_.push(array_begin_token);
				break;

			case array_end_token:
				token.type_ = array_end_token;
				if(structure_stack_.top() != array_begin_token) {
					std::cerr << "fatal error: mismatched array encapsulators" << std::endl;
					token.type_ = error_token;
				} else {
					structure_stack_.pop();
				} // if-else
				break;

			case string_begin_end_token:	// will always be begin since
											// end will be removed while reading
											//the whole string earlier
				if(!read_quoted_string(qstr)) {
					std::cerr << "fatal error: premature EOF reached while reading string" << std::endl;
					token.type_ = error_token;
				} else {
					token.type_ = string_token;
					token.svalue_ = qstr;
				} // if-else
				break;

			case character_token:
				input_stream_.unget();
				read_keyword(value);
				token.type_ = process_keyword_token(value);
				if(token.type_ == error_token) {
					std::cerr << "fatal error: unknown keyword '" << value << "'" << std::endl;
				} // if
				token.svalue_ = value;
				break;

			case assignment_token:
				token.type_ = assignment_token;
				break;

			case separator_token:
				token.type_ = separator_token;
				break;

			case comment_token:
				skip_comments();
				token.type_ = comment_token;
				break;

			default:
				std::cerr << "fatal error: unknown token" << std::endl;
				token.type_ = error_token;
		} // switch

		return token;
	} // InputReader::get_next_token()