test-lib: Fix a bash-4.4 issue.
[paraslash.git] / score.c
1 /*
2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file score.c Scoring functions to determine the audio file streaming order. */
8 #include <regex.h>
9 #include <osl.h>
10
11 #include "para.h"
12 #include "error.h"
13 #include "string.h"
14 #include "afh.h"
15 #include "afs.h"
16 #include "list.h"
17
18 static struct osl_table *score_table;
19
20 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
21 {
22 void *d1 = *(void **)obj1->data;
23 void *d2 = *(void **)obj2->data;
24 return NUM_COMPARE(d1, d2);
25 }
26
27 /**
28 * Compare the score of two audio files
29 *
30 * \param obj1 Pointer to the first score object.
31 * \param obj2 Pointer to the second score object.
32 *
33 * This function first compares the score values as usual integers. If they compare as
34 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
35 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
36 *
37 * \sa osl_compare_function.
38 */
39 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
40 {
41 long d1 = *(long *)obj1->data;
42 long d2 = *(long *)obj2->data;
43 int ret = NUM_COMPARE(d2, d1);
44
45 if (ret)
46 return ret;
47 return NUM_COMPARE(obj2->data, obj1->data);
48 }
49
50 /**
51 * The score table consists of two columns: The \a aft_row column contains
52 * pointers to the rows of the audio file table, and the score column contains
53 * the current score of the audio file associated with that row.
54 */
55 enum score_table_columns {
56 /** The row of the audio file. */
57 SCORECOL_AFT_ROW,
58 /** The score */
59 SCORECOL_SCORE,
60 /** This table has two columns */
61 NUM_SCORE_COLUMNS
62 };
63
64 static struct osl_column_description score_cols[] = {
65 [SCORECOL_AFT_ROW] = {
66 .storage_type = OSL_NO_STORAGE,
67 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE | OSL_DONT_FREE,
68 .name = "aft_row",
69 .compare_function = ptr_compare,
70 .data_size = sizeof(void *)
71 },
72 [SCORECOL_SCORE] = {
73 .storage_type = OSL_NO_STORAGE,
74 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
75 .name = "score",
76 .compare_function = score_compare,
77 .data_size = sizeof(long)
78 }
79 };
80
81 static struct osl_table_description score_table_desc = {
82 .name = "score",
83 .num_columns = NUM_SCORE_COLUMNS,
84 .flags = 0,
85 .column_descriptions = score_cols
86 };
87
88 /**
89 * Compute the number of files in score table.
90 *
91 * \param num Result is returned here.
92 *
93 * \return Positive on success, negative on errors.
94 *
95 * \sa osl_get_num_rows().
96 */
97 int get_num_admissible_files(unsigned *num)
98 {
99 return osl(osl_get_num_rows(score_table, num));
100 }
101
102 /**
103 * Get the score of the audio file associated with given row of the score table.
104 *
105 * \param score_row Pointer to the row in the score table.
106 * \param score Result is returned here on success.
107 *
108 * On errors (negative return value) the content of \a score is undefined.
109 *
110 * \return The return value of the underlying call to osl_get_object().
111 */
112 static int get_score_of_row(void *score_row, long *score)
113 {
114 struct osl_object obj;
115 int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
116
117 if (ret >= 0)
118 *score = *(long *)obj.data;
119 return ret;
120 }
121
122 /**
123 * Add an entry to the table of admissible files.
124 *
125 * \param aft_row The audio file to be added.
126 * \param score The score for this file.
127 *
128 * \return The return value of the underlying call to osl_add_row().
129 */
130 int score_add(const struct osl_row *aft_row, long score)
131 {
132 int ret;
133 struct osl_object score_objs[NUM_SCORE_COLUMNS];
134 size_t size;
135
136 assert(aft_row);
137 size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
138 score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
139 score_objs[SCORECOL_AFT_ROW].size = size;
140
141 size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
142 score_objs[SCORECOL_SCORE].data = para_malloc(size);
143 score_objs[SCORECOL_SCORE].size = size;
144 *(long *)(score_objs[SCORECOL_SCORE].data) = score;
145
146 // PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
147 ret = osl(osl_add_row(score_table, score_objs));
148 if (ret < 0) {
149 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
150 free(score_objs[SCORECOL_SCORE].data);
151 }
152 return ret;
153 }
154
155 static int get_nth_score(unsigned n, long *score)
156 {
157 struct osl_row *row;
158 int ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row));
159
160 if (ret < 0)
161 return ret;
162 return get_score_of_row(row, score);
163 }
164
165 /**
166 * Replace a row of the score table.
167 *
168 * \param aft_row Determines the audio file to change.
169 * \param percent The position to re-insert the audio file.
170 *
171 * The percent parameter must be between 0 and 100. A value of zero means to
172 * re-insert the audio file into the score table with a score lower than any
173 * other admissible file.
174 *
175 * \return Positive on success, negative on errors.
176 */
177 int score_update(const struct osl_row *aft_row, long percent)
178 {
179 struct osl_row *row;
180 long new_score;
181 unsigned n, new_pos;
182 struct osl_object obj = {.data = (struct osl_row *)aft_row,
183 .size = sizeof(aft_row)};
184 int ret = osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row));
185
186 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* not an error */
187 return 1;
188 if (ret < 0)
189 return ret;
190 ret = get_num_admissible_files(&n);
191 if (ret < 0)
192 return ret;
193 new_pos = 1 + (n - 1) * percent / 100;
194 ret = get_nth_score(new_pos, &new_score);
195 if (ret < 0)
196 return ret;
197 new_score--;
198 obj.size = sizeof(long);
199 obj.data = para_malloc(obj.size);
200 *(long *)obj.data = new_score;
201 PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
202 return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
203 }
204
205 /**
206 * Given an admissible file, get the corresponding row in the aft and the score.
207 *
208 * \param score_row Determines the admissible file.
209 * \param score Result pointer.
210 * \param aft_row Result pointer.
211 *
212 * \return Negative on errors, positive on success. Possible errors: Errors
213 * returned by osl_get_object().
214 */
215 int get_score_and_aft_row(struct osl_row *score_row, long *score,
216 struct osl_row **aft_row)
217 {
218 struct osl_object obj;
219 int ret = get_score_of_row(score_row, score);
220
221 if (ret < 0)
222 return ret;
223 ret = osl(osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj));
224 if (ret < 0)
225 return ret;
226 *aft_row = obj.data;
227 return 1;
228 }
229
230 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
231 struct osl_row **score_row)
232 {
233 struct osl_object obj = {.data = (struct osl_row *)aft_row,
234 .size = sizeof(aft_row)};
235 return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
236 }
237
238 /**
239 * Loop over all files in the score table.
240 *
241 * \param data A pointer to arbitrary data.
242 * \param func Function to be called for each admissible file.
243 *
244 * \return The return value of the underlying call to osl_rbtree_loop().
245 *
246 * This is used for the ls command. The \a data parameter is passed as the
247 * second argument to \a func.
248 */
249 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
250 {
251 return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
252 }
253
254 /**
255 * Get the admissible audio file with highest score.
256 *
257 * \param aft_row Points to the row in the aft of the "best" audio file.
258 * \param score Highest score value in the score table.
259 *
260 * \return Positive on success, negative on errors. Possible errors: Errors
261 * returned by osl_rbtree_last_row(), osl_get_object().
262 */
263 int score_get_best(struct osl_row **aft_row, long *score)
264 {
265 struct osl_row *row;
266 struct osl_object obj;
267 int ret = osl(osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row));
268
269 if (ret < 0)
270 return ret;
271 ret = osl(osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj));
272 if (ret < 0)
273 return ret;
274 *aft_row = obj.data;
275 return get_score_of_row(row, score);
276 }
277
278 /**
279 * Remove an entry from the rbtree of admissible files.
280 *
281 * \param aft_row The file which is no longer admissible.
282 *
283 * \return Positive on success, negative on errors. Possible errors:
284 * Errors returned by osl_get_row() and osl_del_row().
285 *
286 * \sa score_add(), score_shutdown().
287 */
288 int score_delete(const struct osl_row *aft_row)
289 {
290 struct osl_row *score_row;
291 int ret = get_score_row_from_aft_row(aft_row, &score_row);
292
293 if (ret < 0)
294 return ret;
295 return osl(osl_del_row(score_table, score_row));
296 }
297
298 /**
299 * Find out whether an audio file is contained in the score table.
300 *
301 * \param aft_row The row of the audio file table.
302 * \param rank Result pointer
303 *
304 * \return Positive, if \a aft_row belongs to the audio file table,
305 * zero if not, negative on errors. If \a aft_row was found, and \a rank
306 * is not \p NULL, the rank of \a aft_row is returned in \a rank.
307 */
308 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
309 {
310 struct osl_row *score_row;
311 int ret = get_score_row_from_aft_row(aft_row, &score_row);
312
313 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
314 return 0;
315 if (ret < 0)
316 return ret;
317 if (!rank)
318 return 1;
319 ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank));
320 if (ret < 0)
321 return ret;
322 return 1;
323 }
324
325 /* Close the score table. */
326 static void score_close(void)
327 {
328 osl_close_table(score_table, OSL_FREE_VOLATILE);
329 score_table = NULL;
330 }
331
332 /**
333 * Open the score table.
334 *
335 * \param dir Unused.
336 *
337 * \return The return value of the underlying call to osl_open_table().
338 */
339 static int score_open(__a_unused const char *dir)
340 {
341 score_table_desc.dir = NULL; /* this table has only volatile columns */
342 return osl(osl_open_table(&score_table_desc, &score_table));
343 }
344
345 /**
346 * Remove all entries from the score table, but keep the table open.
347 *
348 * \return Standard.
349 */
350 int clear_score_table(void)
351 {
352 score_close();
353 return score_open(NULL);
354 }
355
356 static int score_event_handler(__a_unused enum afs_events event,
357 __a_unused struct para_buffer *pb, __a_unused void *data)
358 {
359 return 1;
360 }
361
362 /**
363 * Initialize the scoring subsystem.
364 *
365 * \param t The members of \a t are filled in by the function.
366 */
367 void score_init(struct afs_table *t)
368 {
369 t->name = score_table_desc.name;
370 t->open = score_open;
371 t->close = score_close;
372 t->event_handler = score_event_handler;
373 }