oggdec filter improvements.
[paraslash.git] / score.c
diff --git a/score.c b/score.c
index e233f7352c19af0c6f23a47030d024e334cb0239..51ec1e4a69a92e43d5af8274b1b769b3ce8f9181 100644 (file)
--- a/score.c
+++ b/score.c
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -28,7 +28,7 @@ static int ptr_compare(const struct osl_object *obj1, const struct osl_object *o
  * \param obj2 Pointer to the second score object.
  *
  * This function first compares the score values as usual integers. If they compare as
  * \param obj2 Pointer to the second score object.
  *
  * This function first compares the score values as usual integers. If they compare as
- * equal, the addresss of \a obj1 and \a obj2 are compared. So this compare function
+ * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
  *
  * \sa osl_compare_function.
  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
  *
  * \sa osl_compare_function.
@@ -45,7 +45,7 @@ static int score_compare(const struct osl_object *obj1, const struct osl_object
 }
 
 /**
 }
 
 /**
- * The score table consists of two colums: The \a aft_row column contains
+ * The score table consists of two columns: The \a aft_row column contains
  * pointers to the rows of the audio file table,  and the score column contains
  * the current score of the audio file associated with that row.
  */
  * pointers to the rows of the audio file table,  and the score column contains
  * the current score of the audio file associated with that row.
  */
@@ -61,7 +61,7 @@ enum score_table_columns {
 static struct osl_column_description score_cols[] = {
        [SCORECOL_AFT_ROW] = {
                .storage_type = OSL_NO_STORAGE,
 static struct osl_column_description score_cols[] = {
        [SCORECOL_AFT_ROW] = {
                .storage_type = OSL_NO_STORAGE,
-               .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
+               .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE | OSL_DONT_FREE,
                .name = "aft_row",
                .compare_function = ptr_compare,
                .data_size = sizeof(void *)
                .name = "aft_row",
                .compare_function = ptr_compare,
                .data_size = sizeof(void *)
@@ -132,9 +132,8 @@ int score_add(const struct osl_row *aft_row, long score)
 
        assert(aft_row);
        size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
 
        assert(aft_row);
        size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
-       score_objs[SCORECOL_AFT_ROW].data = para_malloc(size);
+       score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
        score_objs[SCORECOL_AFT_ROW].size = size;
        score_objs[SCORECOL_AFT_ROW].size = size;
-       *(const void **)(score_objs[SCORECOL_AFT_ROW].data) = aft_row;
 
        size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
        score_objs[SCORECOL_SCORE].data = para_malloc(size);
 
        size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
        score_objs[SCORECOL_SCORE].data = para_malloc(size);
@@ -144,8 +143,7 @@ int score_add(const struct osl_row *aft_row, long score)
 //     PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
        ret = osl_add_row(score_table, score_objs);
        if (ret < 0) {
 //     PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
        ret = osl_add_row(score_table, score_objs);
        if (ret < 0) {
-               PARA_ERROR_LOG("failed to add row: %d\n", ret);
-               free(score_objs[SCORECOL_AFT_ROW].data);
+               PARA_ERROR_LOG("%s\n", para_strerror(-ret));
                free(score_objs[SCORECOL_SCORE].data);
        }
        return ret;
                free(score_objs[SCORECOL_SCORE].data);
        }
        return ret;
@@ -167,7 +165,7 @@ static int get_nth_score(unsigned n, long *score)
  * \param aft_row Determines the audio file to change.
  * \param percent The position to re-insert the audio file.
  *
  * \param aft_row Determines the audio file to change.
  * \param percent The position to re-insert the audio file.
  *
- * The percent paramenter must be between \p 0 and 100 and. A value of zero
+ * The percent parameter must be between \p 0 and 100 and. A value of zero
  * means to re-insert the audio file into the score table with a score lower
  * than any other admissible file.
  *
  * means to re-insert the audio file into the score table with a score lower
  * than any other admissible file.
  *
@@ -180,9 +178,12 @@ int score_update(const struct osl_row *aft_row, long percent)
        struct osl_row *row;
        long new_score;
        unsigned n, new_pos;
        struct osl_row *row;
        long new_score;
        unsigned n, new_pos;
-       struct osl_object obj = {.data = &aft_row, .size = sizeof(void **)};
+       struct osl_object obj = {.data = (struct osl_row *)aft_row,
+               .size = sizeof(aft_row)};
        int ret = osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row);
 
        int ret = osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row);
 
+       if (ret == -E_RB_KEY_NOT_FOUND) /* not an error */
+               return 1;
        if (ret < 0)
                return ret;
        ret = get_num_admissible_files(&n);
        if (ret < 0)
                return ret;
        ret = get_num_admissible_files(&n);
@@ -196,8 +197,7 @@ int score_update(const struct osl_row *aft_row, long percent)
        obj.size = sizeof(long);
        obj.data = para_malloc(obj.size);
        *(long *)obj.data = new_score;
        obj.size = sizeof(long);
        obj.data = para_malloc(obj.size);
        *(long *)obj.data = new_score;
-       PARA_DEBUG_LOG("new score: %ld, position: %u/%u\n", new_score,
-               new_pos, n);
+       PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
        return osl_update_object(score_table, row, SCORECOL_SCORE, &obj);
 }
 
        return osl_update_object(score_table, row, SCORECOL_SCORE, &obj);
 }
 
@@ -222,14 +222,15 @@ int get_score_and_aft_row(struct osl_row *score_row, long *score,
        ret = osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj);
        if (ret < 0)
                return ret;
        ret = osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj);
        if (ret < 0)
                return ret;
-       *aft_row = *(void **)obj.data;
+       *aft_row = obj.data;
        return 1;
 }
 
 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
                struct osl_row **score_row)
 {
        return 1;
 }
 
 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
                struct osl_row **score_row)
 {
-       struct osl_object obj = {.data = &aft_row, .size = sizeof(void **)};
+       struct osl_object obj = {.data = (struct osl_row *)aft_row,
+               .size = sizeof(aft_row)};
        return osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row);
 
 }
        return osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row);
 
 }
@@ -287,7 +288,7 @@ int score_get_best(struct osl_row **aft_row, long *score)
        ret = osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj);
        if (ret < 0)
                return ret;
        ret = osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj);
        if (ret < 0)
                return ret;
-       *aft_row = *(void **)obj.data;
+       *aft_row = obj.data;
        return get_score_of_row(row, score);
 }
 
        return get_score_of_row(row, score);
 }
 
@@ -315,19 +316,27 @@ int score_delete(const struct osl_row *aft_row)
  * Find out whether an audio file is contained in the score table.
  *
  * \param aft_row The row of the audio file table.
  * Find out whether an audio file is contained in the score table.
  *
  * \param aft_row The row of the audio file table.
+ * \param rank Result pointer
  *
  * \return Positive, if \a aft_row belongs to the audio file table,
  *
  * \return Positive, if \a aft_row belongs to the audio file table,
- * zero if not, negative on errors.
+ * zero if not, negative on errors. If \a aft_row was found, and \a rank
+ * is not \p NULL, the rank of \a aft_row is returned in \a rank.
  */
  */
-int row_belongs_to_score_table(const struct osl_row *aft_row)
+int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
 {
        struct osl_row *score_row;
        int ret = get_score_row_from_aft_row(aft_row, &score_row);
 {
        struct osl_row *score_row;
        int ret = get_score_row_from_aft_row(aft_row, &score_row);
-       if (ret >= 0)
-               return 1;
+
        if (ret == -E_RB_KEY_NOT_FOUND)
                return 0;
        if (ret == -E_RB_KEY_NOT_FOUND)
                return 0;
-       return ret;
+       if (ret < 0)
+               return ret;
+       if (!rank)
+               return 1;
+       ret = osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank);
+       if (ret < 0)
+               return ret;
+       return 1;
 }
 
 /* Close the score table. */
 }
 
 /* Close the score table. */
@@ -340,26 +349,42 @@ static void score_close(void)
 /**
  * Open the score table.
  *
 /**
  * Open the score table.
  *
- * \param dir The database directory.
+ * \param dir Unused.
  *
  * \return The return value of the underlying call to osl_open_table().
  *
  * \return The return value of the underlying call to osl_open_table().
- *
- * \sa score_shutdown().
  */
  */
-static int score_open(const char *dir)
+static int score_open(__a_unused const char *dir)
 {
 {
-       score_table_desc.dir = dir;
+       score_table_desc.dir = NULL; /* this table has only volatile columns */
        return osl_open_table(&score_table_desc, &score_table);
 }
 
        return osl_open_table(&score_table_desc, &score_table);
 }
 
+/**
+ * Remove all entries from the score table, but keep the table open.
+ *
+ * \return Standard.
+ */
+int clear_score_table(void)
+{
+       score_close();
+       return score_open(NULL);
+}
+
+static int score_event_handler(__a_unused enum afs_events event,
+               __a_unused struct para_buffer *pb, __a_unused void *data)
+{
+       return 1;
+}
+
 /**
  * Initialize the scoring subsystem.
  *
 /**
  * Initialize the scoring subsystem.
  *
- * \param t Members Gets filled in by the function.
+ * \param t The members of \a t are filled in by the function.
  */
 void score_init(struct afs_table *t)
 {
        t->name = score_table_desc.name;
        t->open = score_open;
        t->close = score_close;
  */
 void score_init(struct afs_table *t)
 {
        t->name = score_table_desc.name;
        t->open = score_open;
        t->close = score_close;
+       t->event_handler = score_event_handler;
 }
 }