summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@gmail.com>2008-08-11 12:14:40 +0200
committerLars-Dominik Braun <PromyLOPh@gmail.com>2008-08-11 12:14:40 +0200
commit4878f5911d05af7bfa1917daf984dab0fd07eb42 (patch)
treeda3fef6e115fc9da3c6e1b1ef10c9b6f8ca5aff5
parent9f4cb080f6d1845b7879d5ace57fbdedc0e006cf (diff)
downloadpianobar-windows-4878f5911d05af7bfa1917daf984dab0fd07eb42.tar.gz
pianobar-windows-4878f5911d05af7bfa1917daf984dab0fd07eb42.tar.bz2
pianobar-windows-4878f5911d05af7bfa1917daf984dab0fd07eb42.zip
Temporary sort stations by name in BarUiSelectStation
-rw-r--r--src/main.c82
1 files changed, 70 insertions, 12 deletions
diff --git a/src/main.c b/src/main.c
index 9cea800..67e9319 100644
--- a/src/main.c
+++ b/src/main.c
@@ -91,34 +91,92 @@ char BarReadlineInt (const char *prompt, int *retVal) {
return ret;
}
+/* sort linked list (station); attention: this is a
+ * "i-had-no-clue-what-to-do-algo", but it works.
+ * @param stations
+ * @return NULL-terminated array with sorted stations
+ */
+PianoStation_t **BarSortedStations (PianoStation_t *unsortedStations) {
+ PianoStation_t *currStation, **sortedStations, **currSortedStation;
+ PianoStation_t *oldStation, *veryOldStation;
+ size_t unsortedStationsN = 0;
+ char inserted;
+
+ /* get size */
+ currStation = unsortedStations;
+ while (currStation != NULL) {
+ unsortedStationsN++;
+ currStation = currStation->next;
+ }
+ sortedStations = calloc (unsortedStationsN+1, sizeof (*sortedStations));
+
+ currStation = unsortedStations;
+ while (currStation != NULL) {
+ currSortedStation = sortedStations;
+ inserted = 0;
+ while (*currSortedStation != NULL && !inserted) {
+ /* item has to be inserted _before_ current item? */
+ /* FIXME: this doesn't handle multibyte chars correctly */
+ if (strcasecmp (currStation->name,
+ (*currSortedStation)->name) < 0) {
+ oldStation = *currSortedStation;
+ *currSortedStation = currStation;
+ currSortedStation++;
+ /* move items */
+ while (*currSortedStation != NULL) {
+ veryOldStation = *currSortedStation;
+ *currSortedStation = oldStation;
+ oldStation = veryOldStation;
+ currSortedStation++;
+ }
+ /* append last item */
+ if (oldStation != NULL) {
+ *currSortedStation = oldStation;
+ }
+ inserted = 1;
+ }
+ currSortedStation++;
+ }
+ /* item could not be inserted: append */
+ if (!inserted) {
+ *currSortedStation = currStation;
+ }
+ currStation = currStation->next;
+ }
+ return sortedStations;
+}
+
/* let user pick one station
* @param piano handle
* @return pointer to selected station or NULL
*/
PianoStation_t *BarUiSelectStation (PianoHandle_t *ph,
const char *prompt) {
- PianoStation_t *curStation = NULL;
+ PianoStation_t **ss = NULL, **ssCurr = NULL;
int i = 0;
- curStation = ph->stations;
- while (curStation != NULL) {
+ ss = BarSortedStations (ph->stations);
+ ssCurr = ss;
+ while (*ssCurr != NULL) {
printf ("%2i) %c%c%c %s\n", i,
- curStation->useQuickMix ? 'q' : ' ',
- curStation->isQuickMix ? 'Q' : ' ',
- !curStation->isCreator ? 'S' : ' ',
- curStation->name);
- curStation = curStation->next;
+ (*ssCurr)->useQuickMix ? 'q' : ' ',
+ (*ssCurr)->isQuickMix ? 'Q' : ' ',
+ !(*ssCurr)->isCreator ? 'S' : ' ',
+ (*ssCurr)->name);
+ ssCurr++;
i++;
}
+
if (!BarReadlineInt (prompt, &i)) {
return NULL;
}
- curStation = ph->stations;
- while (curStation != NULL && i > 0) {
- curStation = curStation->next;
+ ssCurr = ss;
+ while (*ssCurr != NULL && i > 0) {
+ ssCurr++;
i--;
}
- return curStation;
+ free (ss);
+ return *ssCurr;
}
/* let user pick one song