Beispiel #1
0
int
parse_interface(struct tapif* tapif,char* param)
{
  enum {
    KEY_WAIT, KEY, VALUE_WAIT, VALUE, END
  };
  int state = KEY_WAIT;
  char* p = param;
  char* key = 0;
  char* value = 0;

  while (state != END) {
    switch (*p) {
    case '\0':
      if (parse_pair(tapif,key,value) != 0)
        return -1;
      state = END;
      break;
    case ',':
      if (state == KEY_WAIT) {
        p++;
        break;
      }
      state = KEY_WAIT;
      *p++ = 0;
      if (parse_pair(tapif,key,value) != 0)
        return -1;
      key = value = 0;
      break;
    case '=':
      if (state == KEY)
        state = VALUE_WAIT;
      else
        return -1;
      *p++ = 0;
      break;
    case ' ':
      if (state != KEY_WAIT && state != VALUE_WAIT)
        return -1;
      p++;
      break;
    default:
      if (state == KEY_WAIT) {
        state = KEY;
        key = p;
      } else if (state == VALUE_WAIT) {
        state = VALUE;
        value = p;
      }
      p++;
    }
  }
  return 0;
}
Beispiel #2
0
void CameraParameters::getPreferredPreviewSizeForVideo(int *width, int *height) const
{
    *width = *height = -1;
    const char *p = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
    if (p == 0)  return;
    parse_pair(p, width, height, 'x');
}
Beispiel #3
0
/*
 * Get configuration from configuration file.
 */
void
pgut_readopt(const char *path, pgut_option options[], int elevel)
{
	FILE   *fp;
	char	buf[1024];
	char	key[1024];
	char	value[1024];

	if (!options)
		return;

	if ((fp = pgut_fopen(path, "rt", true)) == NULL)
		return;

	while (fgets(buf, lengthof(buf), fp))
	{
		size_t		i;

		for (i = strlen(buf); i > 0 && IsSpace(buf[i - 1]); i--)
			buf[i - 1] = '\0';

		if (parse_pair(buf, key, value))
		{
			for (i = 0; options[i].type; i++)
			{
				pgut_option *opt = &options[i];

				if (key_equals(key, opt->lname))
				{
					if (opt->allowed == SOURCE_DEFAULT ||
						opt->allowed > SOURCE_FILE)
						if (elevel >= ERROR)
						{
							ereport(ERROR,
								(errcode(elevel),
								 errmsg("option %s cannot specified in file", opt->lname)));
						} else {
							elog(elevel, "option %s cannot specified in file", opt->lname);
						}
					else if (opt->source <= SOURCE_FILE)
						assign_option(opt, value, SOURCE_FILE);
					break;
				}
			}
			if (!options[i].type)
			{
				if (elevel >= ERROR)
				{
					ereport(ERROR,
						(errcode(elevel),
						 errmsg("invalid option \"%s\"", key)));
				} else {
					elog(elevel, "invalid option \"%s\"", key);
				}
			}
		}
	}

	fclose(fp);
}
Beispiel #4
0
void CameraParameters::getVideoSize(int *width, int *height) const
{
    *width = *height = -1;
    const char *p = get(KEY_VIDEO_SIZE);
    if (p == 0) return;
    parse_pair(p, width, height, 'x');
}
Beispiel #5
0
void CameraParameters::getPreviewFpsRange(int *min_fps, int *max_fps) const
{
    *min_fps = *max_fps = -1;
    const char *p = get(KEY_PREVIEW_FPS_RANGE);
    if (p == 0) return;
    parse_pair(p, min_fps, max_fps, ',');
}
Beispiel #6
0
static int
parse_pairs(void *opts, char *param, int (*parse_pair)(void *, char*, char*))
{
	enum {
		WAIT_KEY, KEY, WAIT_VALUE, VALUE, END
	};
	int state = WAIT_KEY;
	char* p = param;
	char* key = 0;
	char* value = 0;

	while (state != END) {
		switch (*p) {
		case '\0':
			if (parse_pair(opts, key, value) != 0)
				return -1;
			state = END;
			break;
		case ',':
			if (state != VALUE)
				return -1;
			state = WAIT_KEY;
			*p++ = 0;
			if (parse_pair(opts, key, value) != 0)
				return -1;
			key = value = 0;
			break;
		case '=':
			if (state != KEY)
				return -1;
			state = WAIT_VALUE;
			*p++ = 0;
			break;
		default:
			if (state == WAIT_KEY) {
				state = KEY;
				key = p;
			} else if (state == WAIT_VALUE) {
				state = VALUE;
				value = p;
			}
			p++;
		}
	}
	return 0;
}
Beispiel #7
0
void CameraParameters::getPictureSize(int *width, int *height) const
{
    *width = *height = -1;
    // Get the current string, if it doesn't exist, leave the -1x-1
    const char *p = get(KEY_PICTURE_SIZE);
    if (p == 0) return;
    parse_pair(p, width, height, 'x');
}
Beispiel #8
0
static heim_dict_t
parse_dict(struct parse_ctx *ctx)
{
    heim_dict_t dict;
    size_t count = 0;
    int ret;

    heim_assert(*ctx->p == '{', "string doesn't start with {");

    dict = heim_dict_create(11);
    if (dict == NULL) {
	ctx->error = heim_error_create_enomem();
	return NULL;
    }

    ctx->p += 1; /* safe because parse_pair() calls white_spaces() first */

    while ((ret = parse_pair(dict, ctx)) > 0)
	count++;
    if (ret < 0) {
	heim_release(dict);
	return NULL;
    }
    if (count == 1 && !(ctx->flags & HEIM_JSON_F_NO_DATA_DICT)) {
	heim_object_t v = heim_dict_copy_value(dict, heim_tid_data_uuid_key);

	/*
	 * Binary data encoded as a dict with a single magic key with
	 * base64-encoded value?  Decode as heim_data_t.
	 */
	if (v != NULL && heim_get_tid(v) == HEIM_TID_STRING) {
	    void *buf;
	    size_t len;

	    buf = malloc(strlen(heim_string_get_utf8(v)));
	    if (buf == NULL) {
		heim_release(dict);
		heim_release(v);
		ctx->error = heim_error_create_enomem();
		return NULL;
	    }
	    len = base64_decode(heim_string_get_utf8(v), buf);
	    heim_release(v);
	    if (len == -1) {
		free(buf);
		return dict; /* assume aliasing accident */
	    }
	    heim_release(dict);
	    return (heim_dict_t)heim_data_ref_create(buf, len, free);
	}
    }
    return dict;
}
Beispiel #9
0
// object = '{' pair { ',' pair } '}'
static int parse_object(struct frozen *f) {
  int ind;
  TRY(test_and_skip(f, '{'));
  TRY(capture_ptr(f, f->cur - 1, JSON_TYPE_OBJECT));
  ind = f->num_tokens - 1;
  while (cur(f) != '}') {
    TRY(parse_pair(f));
    if (cur(f) == ',') f->cur++;
  }
  TRY(test_and_skip(f, '}'));
  capture_len(f, ind, f->cur);
  return 0;
}
Beispiel #10
0
/* object = '{' pair { ',' pair } '}' */
static int parse_object(struct frozen *f) {
  TRY(test_and_skip(f, '{'));
  {
    SET_STATE(f, f->cur - 1, JSON_TYPE_OBJECT, ".", 1);
    while (cur(f) != '}') {
      TRY(parse_pair(f));
      if (cur(f) == ',') f->cur++;
    }
    TRY(test_and_skip(f, '}'));
    CALL_BACK(f);
  }
  return 0;
}
Beispiel #11
0
static void parse_ranges ( char *input, struct range_pair **list, unsigned int *length )
{
    char *endp;
    for ( char *token = strtok_r ( input, ",", &endp );
          token != NULL;
          token = strtok_r ( NULL, ",", &endp ) ) {
        // Make space.
        *list = g_realloc ( ( *list ), ( ( *length ) + 1 ) * sizeof ( struct range_pair ) );
        // Parse a single pair.
        parse_pair ( token, &( ( *list )[*length] ) );

        ( *length )++;
    }
}
Beispiel #12
0
Datei: parse.c Projekt: mk12/eva
// Parses a pair, assuming the opening left parenthesis has already been read.
static struct ParseResult parse_pair(const char *text) {
	struct ParseResult result;
	result.err_type = -1;
	const char *s = text;
	s += skip_whitespace(s);

	if (*s == ')') {
		s++;
		result.expr = new_null();
		goto chars_read;
	}

	struct ParseResult first = parse(s);
	s += first.chars_read;
	if (first.err_type != -1) {
		result.err_type = first.err_type;
		goto chars_read;
	}

	if (*s == '.') {
		s++;
		struct ParseResult second = parse(s);
		s += second.chars_read;
		if (second.err_type != -1) {
			result.err_type = second.err_type;
			release_expression(first.expr);
			goto chars_read;
		}
		if (*s != ')') {
			result.err_type = *s ? ERR_EXPECTED_RPAREN : ERR_UNEXPECTED_EOI;
			release_expression(first.expr);
			goto chars_read;
		}
		s++;
		result.expr = new_pair(first.expr, second.expr);
	} else {
		struct ParseResult rest = parse_pair(s);
		s += rest.chars_read;
		if (rest.err_type != -1) {
			result.err_type = rest.err_type;
			release_expression(first.expr);
			goto chars_read;
		}
		result.expr = new_pair(first.expr, rest.expr);
	}

chars_read:
	result.chars_read = (size_t)(s - text);
	return result;
}
Beispiel #13
0
/* object = '{' pair { ',' pair } '}' */
static int parse_object(struct frozen *f) {
  CALL_BACK(f, JSON_TYPE_OBJECT_START, NULL, 0);
  TRY(test_and_skip(f, '{'));
  {
    SET_STATE(f, f->cur - 1, ".", 1);
    while (cur(f) != '}') {
      TRY(parse_pair(f));
      if (cur(f) == ',') f->cur++;
    }
    TRY(test_and_skip(f, '}'));
    truncate_path(f, fstate.path_len);
    CALL_BACK(f, JSON_TYPE_OBJECT_END, fstate.ptr, f->cur - fstate.ptr);
  }
  return 0;
}
void CameraParameters::getTouchIndexAf(int *x, int *y) const
{
    *x = -1;
    *y = -1;

    // Get the current string, if it doesn't exist, leave the -1x-1
    const char *p = get(KEY_TOUCH_INDEX_AF);
    if (p == 0)
        return;

    int tempX, tempY;
    if (parse_pair(p, &tempX, &tempY, 'x') == 0) {
        *x = tempX;
        *y = tempY;
    }
}
Beispiel #15
0
static heim_dict_t
parse_dict(struct parse_ctx *ctx)
{
    heim_dict_t dict = heim_dict_create(11);
    int ret;

    heim_assert(*ctx->p == '{', "string doesn't start with {");
    ctx->p += 1;

    while ((ret = parse_pair(dict, ctx)) > 0)
	;
    if (ret < 0) {
	heim_release(dict);
	return NULL;
    }
    return dict;
}
Beispiel #16
0
/*
 * Get configuration from configuration file.
 */
void
pgut_readopt(const char *path, pgut_option options[], int elevel)
{
	FILE   *fp;
	char	buf[1024];
	char	key[1024];
	char	value[1024];

	if (!options)
		return;

	if ((fp = pgut_fopen(path, "Rt")) == NULL)
		return;

	while (fgets(buf, lengthof(buf), fp))
	{
		size_t		i;

		for (i = strlen(buf); i > 0 && IsSpace(buf[i - 1]); i--)
			buf[i - 1] = '\0';

		if (parse_pair(buf, key, value))
		{
			for (i = 0; options[i].type; i++)
			{
				pgut_option *opt = &options[i];

				if (pgut_keyeq(key, opt->lname))
				{
					if (opt->allowed == SOURCE_DEFAULT ||
						opt->allowed > SOURCE_FILE)
						elog(elevel, "option %s cannot specified in file", opt->lname);
					else if (opt->source <= SOURCE_FILE)
						pgut_setopt(opt, value, SOURCE_FILE);
					break;
				}
			}
			if (!options[i].type)
				elog(elevel, "invalid option \"%s\"", key);
		}
	}

	fclose(fp);
}
Beispiel #17
0
static void parseSizesList(const char *sizesStr, Vector<Size> &sizes)
{
    if (sizesStr == 0) {
        return;
    }

    char *sizeStartPtr = (char *)sizesStr;

    while (true) {
        int width, height;
        int success = parse_pair(sizeStartPtr, &width, &height, 'x',
                                 &sizeStartPtr);
        if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) {
            ALOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr);
            return;
        }
        sizes.push(Size(width, height));

        if (*sizeStartPtr == '\0') {
            return;
        }
        sizeStartPtr++;
    }
}
Beispiel #18
0
Datei: parse.c Projekt: mk12/eva
// Parses any expression.
struct ParseResult parse(const char *text) {
	struct ParseResult result;
	result.err_type = PARSE_SUCCESS;
	const char *s = text;
	s += skip_whitespace(s);

	size_t len;
	switch (*s) {
	case '\0':
		result.err_type = ERR_UNEXPECTED_EOI;
		break;
	case '(':
		s++;
		result = parse_pair(s);
		s += result.chars_read;
		break;
	case ')':
		result.err_type = ERR_UNEXPECTED_RPAREN;
		break;
	case '.':
		result.err_type = ERR_INVALID_DOT;
		break;
	case '#':
		len = skip_symbol(s + 1);
		if (len == 1 && s[1] == 't') {
			s += 2;
			result.expr = new_boolean(true);
		} else if (len == 1 && s[1] == 'f') {
			s += 2;
			result.expr = new_boolean(false);
		} else if (len > 2 && s[1] == '\\') {
			char c = parse_char_name(s + 2, len - 1);
			if (c == '\0') {
				s += 2;
				result.err_type = ERR_UNKNOWN_CHARACTER;
			} else {
				s += 1 + len;
				result.expr = new_character(c);
			}
		} else if (s[1] == '\\' && s[2] && !isspace(s[2])) {
			result.expr = new_character(s[2]);
			s += 3;
		} else {
			result.err_type = ERR_INVALID_LITERAL;
		}
		break;
	case '\'':
		s++;
		result = parse(s);
		s += result.chars_read;
		if (result.err_type == PARSE_SUCCESS) {
			result.expr = new_pair(
					new_stdmacro(F_QUOTE),
					new_pair(result.expr, new_null()));
		}
		break;
	case '`':
		s++;
		result = parse(s);
		s += result.chars_read;
		if (result.err_type == PARSE_SUCCESS) {
			result.expr = new_pair(
					new_stdmacro(F_QUASIQUOTE),
					new_pair(result.expr, new_null()));
		}
		break;
	case ',':
		s++;
		enum StandardMacro stdmacro = F_UNQUOTE;
		if (*s == '@') {
			s++;
			stdmacro = F_UNQUOTE_SPLICING;
		}
		result = parse(s);
		s += result.chars_read;
		if (result.err_type == PARSE_SUCCESS) {
			result.expr = new_pair(
					new_stdmacro(stdmacro),
					new_pair(result.expr, new_null()));
		}
		break;
	case '"':
		s++;
		len = skip_string(s);
		if (!(s[len-1] == '\\' && (len < 2 || s[len-2] == '\\'))
				&& s[len] == '"') {
			result.expr = parse_string(s, len);
		} else {
			result.err_type = ERR_UNEXPECTED_EOI;
		}
		s += len;
		s++;
		break;
	default:;
		len = skip_symbol(s);
		assert(len > 0);
		Number number;
		if (parse_number(s, len, &number)) {
			result.expr = new_number(number);
		} else {
			InternId symbol_id = intern_string_n(s, len);
			result.expr = new_symbol(symbol_id);
		}
		s += len;
		break;
	}

	if (result.err_type == PARSE_SUCCESS) {
		s += skip_whitespace(s);
		assert(s > text);
	}
	result.chars_read = (size_t)(s - text);
	return result;
}