Ejemplo n.º 1
0
static VALUE
c_dict_aref (VALUE self, VALUE key)
{
	RbDict *dict = NULL;
	xmmsv_dict_iter_t *it;
	xmmsv_t *value;
	const char *ckey;
	int s;

	Check_Type (key, T_SYMBOL);

	Data_Get_Struct (self, RbDict, dict);

	ckey = rb_id2name (SYM2ID (key));

	xmmsv_get_dict_iter (dict->real, &it);

	s = xmmsv_dict_iter_find (it, ckey);
	if (!s)
		return Qnil;

	xmmsv_dict_iter_pair (it, NULL, &value);

	return extract_value (self, value);
}
Ejemplo n.º 2
0
void
Dict::const_iterator::copy( const const_iterator& rh )
{
    const char* key = 0;
    xmmsv_get_dict_iter( dict_, &it_ );
    xmmsv_dict_iter_pair( rh.it_, &key, NULL );
    xmmsv_dict_iter_find( it_, key );
}
Ejemplo n.º 3
0
Dict::const_iterator Dict::find( const std::string& key ) const
{
    const_iterator it( value_ );

    if( xmmsv_dict_iter_find( it.it_, key.c_str() ) ) {
        return it;
    }
    else {
        return end();
    }
}
Ejemplo n.º 4
0
static VALUE
c_dict_has_key (VALUE self, VALUE key)
{
	RbDict *dict = NULL;
	xmmsv_dict_iter_t *it;
	const char *ckey;

	Check_Type (key, T_SYMBOL);

	Data_Get_Struct (self, RbDict, dict);

	ckey = rb_id2name (SYM2ID (key));

	xmmsv_get_dict_iter (dict->real, &it);

	return xmmsv_dict_iter_find (it, ckey) ? Qtrue : Qfalse;
}
Ejemplo n.º 5
0
/**
 * This function will make a pretty string about the information in
 * xmmsv dict.
 *
 * @param target A allocated char *
 * @param len Length of target
 * @param fmt A format string to use. You can insert items from the dict by
 * using specialformat "${field}".
 * @param val The #xmmsv_t that contains the dict.
 *
 * @returns The number of chars written to target
 */
int
xmmsv_dict_format (char *target, int len, const char *fmt, xmmsv_t *val)
{
	const char *pos;

	if (!target) {
		return 0;
	}

	if (!fmt) {
		return 0;
	}

	memset (target, 0, len);

	pos = fmt;
	while (strlen (target) + 1 < len) {
		char *next_key, *key, *end;
		int keylen;
		xmmsv_dict_iter_t *it;
		xmmsv_t *v;

		next_key = strstr (pos, "${");
		if (!next_key) {
			strncat (target, pos, len - strlen (target) - 1);
			break;
		}

		strncat (target, pos, MIN (next_key - pos, len - strlen (target) - 1));
		keylen = strcspn (next_key + 2, "}");
		key = malloc (keylen + 1);

		if (!key) {
			fprintf (stderr, "Unable to allocate %u bytes of memory, OOM?", keylen);
			break;
		}

		memset (key, 0, keylen + 1);
		strncpy (key, next_key + 2, keylen);

		xmmsv_get_dict_iter (val, &it);

		if (strcmp (key, "seconds") == 0) {
			int64_t duration;

			if (xmmsv_dict_iter_find (it, "duration")) {
				xmmsv_dict_iter_pair (it, NULL, &v);
				xmmsv_get_int (v, &duration);
			} else {
				duration = 0;
			}

			if (!duration) {
				strncat (target, "00", len - strlen (target) - 1);
			} else {
				char seconds[21];
				/* rounding */
				duration += 500;
				snprintf (seconds, sizeof (seconds), "%02" PRId64, (duration/1000)%60);
				strncat (target, seconds, len - strlen (target) - 1);
			}
		} else if (strcmp (key, "minutes") == 0) {
			int64_t duration;

			if (xmmsv_dict_iter_find (it, "duration")) {
				xmmsv_dict_iter_pair (it, NULL, &v);
				xmmsv_get_int (v, &duration);
			} else {
				duration = 0;
			}

			if (!duration) {
				strncat (target, "00", len - strlen (target) - 1);
			} else {
				char minutes[21];
				/* rounding */
				duration += 500;
				snprintf (minutes, sizeof (minutes), "%02" PRId64, duration/60000);
				strncat (target, minutes, len - strlen (target) - 1);
			}
		} else {
			const char *result = NULL;
			char tmp[21];

			if (xmmsv_dict_iter_find (it, key)) {
				xmmsv_dict_iter_pair (it, NULL, &v);

				xmmsv_type_t type = xmmsv_get_type (v);
				if (type == XMMSV_TYPE_STRING) {
					xmmsv_get_string (v, &result);
				} else if (type == XMMSV_TYPE_INT64) {
					int64_t i;
					xmmsv_get_int (v, &i);
					snprintf (tmp, 21, "%" PRId64, i);
					result = tmp;
				} else if (type == XMMSV_TYPE_FLOAT) {
					float f;
					xmmsv_get_float (v, &f);
					snprintf (tmp, 12, "%.6f", f);
					result = tmp;
				}
			}

			if (result)
				strncat (target, result, len - strlen (target) - 1);
		}

		free (key);
		end = strchr (next_key, '}');

		if (!end) {
			break;
		}

		pos = end + 1;
	}

	return strlen (target);
}