// inner function for swfdec_as_array_sort_compare static int swfdec_as_array_sort_compare_values (SwfdecAsContext *cx, const SwfdecAsValue *a, const SwfdecAsValue *b, SortOption options, SwfdecAsFunction *custom_function) { int retval; g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (cx), 0); g_return_val_if_fail (SWFDEC_IS_AS_VALUE (a), 0); g_return_val_if_fail (SWFDEC_IS_AS_VALUE (b), 0); g_return_val_if_fail (custom_function == NULL || SWFDEC_IS_AS_FUNCTION (custom_function), 0); if (custom_function != NULL) { SwfdecAsValue argv[2] = { *a, *b }; SwfdecAsValue ret; SwfdecAsContext *context = swfdec_gc_object_get_context (custom_function); swfdec_as_function_call (custom_function, NULL, 2, argv, &ret); retval = swfdec_as_value_to_integer (context, &ret); } else if (options & SORT_OPTION_NUMERIC && (SWFDEC_AS_VALUE_IS_NUMBER (a) || SWFDEC_AS_VALUE_IS_NUMBER (b)) && !SWFDEC_AS_VALUE_IS_UNDEFINED (a) && !SWFDEC_AS_VALUE_IS_UNDEFINED (b)) { if (!SWFDEC_AS_VALUE_IS_NUMBER (a)) { retval = 1; } else if (!SWFDEC_AS_VALUE_IS_NUMBER (b)) { retval = -1; } else { double an = swfdec_as_value_to_number (cx, a); double bn = swfdec_as_value_to_number (cx, b); retval = (an < bn ? -1 : (an > bn ? 1 : 0)); } } else { // can't pass swfdec_as_value_to_string calls directly to compare // functions, since the order of these is important const char *a_str = swfdec_as_value_to_string (cx, a); const char *b_str = swfdec_as_value_to_string (cx, b); if (options & SORT_OPTION_CASEINSENSITIVE) { retval = g_strcasecmp (a_str, b_str); } else { retval = strcmp (a_str, b_str); } } if (options & SORT_OPTION_DESCENDING) retval = -retval; return retval; }
void swfdec_as_array_sort (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret) { guint pos; SortOption options; SwfdecAsFunction *custom_function; if (object == NULL || SWFDEC_IS_MOVIE (object)) return; pos = 0; if (argc > pos && !SWFDEC_AS_VALUE_IS_NUMBER (&argv[pos])) { SwfdecAsFunction *fun; if (!SWFDEC_AS_VALUE_IS_OBJECT (&argv[pos]) || !SWFDEC_IS_AS_FUNCTION ( fun = (SwfdecAsFunction *) SWFDEC_AS_VALUE_GET_OBJECT (&argv[pos]))) return; custom_function = fun; pos++; } else { custom_function = NULL; } if (argc > pos) { options = swfdec_as_value_to_integer (cx, &argv[pos]) & MASK_SORT_OPTION; } else { options = 0; } swfdec_as_array_do_sort (cx, object, &options, custom_function, NULL, ret); }
void swfdec_system_query (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc, SwfdecAsValue *argv, SwfdecAsValue *retval) { SwfdecPlayer *player = SWFDEC_PLAYER (cx); SwfdecAsValue val; guint i; GString *server; if (object == NULL) { SWFDEC_WARNING ("no this object in Query()"); return; } server = g_string_new (""); for (i = 0; i < G_N_ELEMENTS (queries); i++) { queries[i].get (player, &val); swfdec_as_object_set_variable (object, queries[i].name, &val); if (queries[i].name == SWFDEC_AS_STR_screenResolutionY) { g_string_append_printf (server, "x%d", (int) SWFDEC_AS_VALUE_GET_NUMBER (val)); } else if (queries[i].name == SWFDEC_AS_STR_pixelAspectRatio) { char buffer[10]; g_ascii_formatd (buffer, sizeof (buffer), "%.1f", SWFDEC_AS_VALUE_GET_NUMBER (val)); g_string_append (server, "&AR="); g_string_append (server, buffer); } else if (queries[i].name == SWFDEC_AS_STR_manufacturer) { char *s = swfdec_as_string_escape (cx, player->priv->system->server_manufacturer); g_string_append_printf (server, "&M=%s", s); g_free (s); } else { g_assert (queries[i].server_string); if (i > 0) g_string_append_c (server, '&'); g_string_append (server, queries[i].server_string); g_string_append_c (server, '='); if (SWFDEC_AS_VALUE_IS_BOOLEAN (val)) { g_string_append_c (server, SWFDEC_AS_VALUE_GET_BOOLEAN (val) ? 't' : 'f'); } else if (SWFDEC_AS_VALUE_IS_NUMBER (val)) { g_string_append_printf (server, "%d", (int) SWFDEC_AS_VALUE_GET_NUMBER (val)); } else if (SWFDEC_AS_VALUE_IS_STRING (val)) { char *s = swfdec_as_string_escape (cx, SWFDEC_AS_VALUE_GET_STRING (val)); g_string_append (server, s); g_free (s); } else { g_assert_not_reached (); } } } SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_give_string (cx, g_string_free (server, FALSE))); swfdec_as_object_set_variable (object, SWFDEC_AS_STR_serverString, &val); }