X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=score.c;h=10cd254a8dba2926614d0a76ffa29cad90e74792;hb=9055c71be97f1095dcdbd83da305b600f204f763;hp=54af56f7c60100ad65ac09cafb52e7769514c958;hpb=610cb53b94af8e95326cd44b7845f35cffdb6bf3;p=paraslash.git diff --git a/score.c b/score.c index 54af56f7..10cd254a 100644 --- a/score.c +++ b/score.c @@ -76,18 +76,6 @@ static struct osl_table_description score_table_desc = { .column_descriptions = score_cols }; -/** - * Compute the number of files in score table. - * - * \param num Result is returned here. - * - * \return Positive on success, negative on errors. - */ -int get_num_admissible_files(unsigned *num) -{ - return osl(osl_get_num_rows(score_table, num)); -} - /* On errors (negative return value) the content of score is undefined. */ static int get_score_of_row(void *score_row, long *score) { @@ -132,16 +120,6 @@ int score_add(const struct osl_row *aft_row, long score) return ret; } -static int get_nth_score(unsigned n, long *score) -{ - struct osl_row *row; - int ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row)); - - if (ret < 0) - return ret; - return get_score_of_row(row, score); -} - /** * Replace a row of the score table. * @@ -156,7 +134,7 @@ static int get_nth_score(unsigned n, long *score) */ int score_update(const struct osl_row *aft_row, long percent) { - struct osl_row *row; + struct osl_row *row, *rrow; /* score row, reference row */ long new_score; unsigned n, new_pos; struct osl_object obj = {.data = (struct osl_row *)aft_row, @@ -167,11 +145,14 @@ int score_update(const struct osl_row *aft_row, long percent) return 1; if (ret < 0) return ret; - ret = get_num_admissible_files(&n); + ret = osl(osl_get_num_rows(score_table, &n)); if (ret < 0) return ret; new_pos = 1 + (n - 1) * percent / 100; - ret = get_nth_score(new_pos, &new_score); + ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, new_pos, &rrow)); + if (ret < 0) + return ret; + ret = get_score_of_row(rrow, &new_score); if (ret < 0) return ret; new_score--; @@ -215,17 +196,15 @@ static int get_score_row_from_aft_row(const struct osl_row *aft_row, } /** - * Loop over all files in the score table. - * - * \param data A pointer to arbitrary data. - * \param func Function to be called for each admissible file. + * Call the given function for each row of the score table. * - * \return The return value of the underlying call to osl_rbtree_loop(). + * \param func Callback, called once per row. + * \param data Passed verbatim to the callback. * - * This is used for the ls command. The \a data parameter is passed as the - * second argument to \a func. + * \return The return value of the underlying call to osl_rbtree_loop(). The + * loop terminates early if the callback returns negative. */ -int admissible_file_loop(void *data, osl_rbtree_loop_func *func) +int score_loop(osl_rbtree_loop_func *func, void *data) { return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func)); } @@ -276,27 +255,20 @@ 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. - * \param rank Result pointer * - * \return Positive, if \a aft_row belongs to the audio file table, - * 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. + * \return If the lookup operation fails for any other reason than "not found", + * the function aborts the current process (afs), since this is considered a + * fatal error that should never happen. */ -int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank) +bool row_belongs_to_score_table(const struct osl_row *aft_row) { struct osl_row *score_row; int ret = get_score_row_from_aft_row(aft_row, &score_row); if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) - return 0; - if (ret < 0) - return ret; - if (!rank) - return 1; - ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank)); - if (ret < 0) - return ret; - return 1; + return false; + assert(ret >= 0); + return true; } static void score_close(void) @@ -307,36 +279,21 @@ static void score_close(void) static int score_open(__a_unused const char *dir) { - score_table_desc.dir = NULL; /* this table has only volatile columns */ - return osl(osl_open_table(&score_table_desc, &score_table)); + assert(osl_open_table(&score_table_desc, &score_table) >= 0); + return 1; } /** * Remove all entries from the score table, but keep the table open. - * - * \return Standard. */ -int clear_score_table(void) +void score_clear(void) { score_close(); - return score_open(NULL); + 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. - * - * \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; - t->event_handler = score_event_handler; -} +/** The score table stores (aft row, score) pairs in memory. */ +const struct afs_table_operations score_ops = { + .open = score_open, + .close = score_close, +};