summaryrefslogtreecommitdiff
path: root/src/ui.c
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@lavabit.com>2010-03-06 10:54:59 +0100
committerLars-Dominik Braun <PromyLOPh@lavabit.com>2010-03-06 10:54:59 +0100
commit3838e77238e383d6bea6496a7bc035702f320210 (patch)
treed9eeb29e47d6395ae1947d68d93ff2282afeaf65 /src/ui.c
parentb4e293558266d9ae2f7971c682ac0846972005f8 (diff)
downloadpianobar-windows-3838e77238e383d6bea6496a7bc035702f320210.tar.gz
pianobar-windows-3838e77238e383d6bea6496a7bc035702f320210.tar.bz2
pianobar-windows-3838e77238e383d6bea6496a7bc035702f320210.zip
Use qsort instead of my own ugly insertion sort
Diffstat (limited to 'src/ui.c')
-rw-r--r--src/ui.c98
1 files changed, 42 insertions, 56 deletions
diff --git a/src/ui.c b/src/ui.c
index dfd37eb..7fd4bba 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -94,58 +94,47 @@ inline PianoReturn_t BarUiPrintPianoStatus (PianoReturn_t ret) {
return ret;
}
+/* compare stations by name (ignore case)
+ * @param station a
+ * @param station b
+ * @return -1, 0, 1
+ */
+static int BarStationCmp (const void *a, const void *b) {
+ const PianoStation_t *stationA = *((PianoStation_t **) a),
+ *stationB = *((PianoStation_t **) b);
+ return strcasecmp (stationA->name, stationB->name);
+}
+
/* sort linked list (station)
* @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;
+PianoStation_t **BarSortedStations (PianoStation_t *unsortedStations,
+ size_t *retStationCount) {
+ PianoStation_t **stationArray = NULL, *currStation = NULL;
+ size_t stationCount = 0, i;
/* get size */
currStation = unsortedStations;
while (currStation != NULL) {
- unsortedStationsN++;
+ ++stationCount;
currStation = currStation->next;
}
- sortedStations = calloc (unsortedStationsN+1, sizeof (*sortedStations));
+ stationArray = calloc (stationCount, sizeof (*stationArray));
+ /* copy station pointers */
currStation = unsortedStations;
+ i = 0;
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;
- }
+ stationArray[i] = currStation;
currStation = currStation->next;
+ ++i;
}
- return sortedStations;
+
+ qsort (stationArray, stationCount, sizeof (*stationArray), BarStationCmp);
+
+ *retStationCount = stationCount;
+ return stationArray;
}
/* let user pick one station
@@ -154,34 +143,31 @@ PianoStation_t **BarSortedStations (PianoStation_t *unsortedStations) {
*/
PianoStation_t *BarUiSelectStation (PianoHandle_t *ph, const char *prompt,
FILE *curFd) {
- PianoStation_t **ss = NULL, **ssCurr = NULL, *retStation;
- int i = 0;
+ PianoStation_t **sortedStations = NULL, *retStation = NULL;
+ size_t stationCount, i;
+ int input;
/* sort and print stations */
- ss = BarSortedStations (ph->stations);
- ssCurr = ss;
- while (*ssCurr != NULL) {
+ sortedStations = BarSortedStations (ph->stations, &stationCount);
+ for (i = 0; i < stationCount; i++) {
+ const PianoStation_t *currStation = sortedStations[i];
BarUiMsg (MSG_LIST, "%2i) %c%c%c %s\n", i,
- (*ssCurr)->useQuickMix ? 'q' : ' ',
- (*ssCurr)->isQuickMix ? 'Q' : ' ',
- !(*ssCurr)->isCreator ? 'S' : ' ',
- (*ssCurr)->name);
- ssCurr++;
- i++;
+ currStation->useQuickMix ? 'q' : ' ',
+ currStation->isQuickMix ? 'Q' : ' ',
+ !currStation->isCreator ? 'S' : ' ',
+ currStation->name);
}
BarUiMsg (MSG_QUESTION, prompt);
- if (BarReadlineInt (&i, curFd) == 0) {
- free (ss);
+ /* FIXME: using a _signed_ int is ugly */
+ if (BarReadlineInt (&input, curFd) == 0) {
+ free (sortedStations);
return NULL;
}
- ssCurr = ss;
- while (*ssCurr != NULL && i > 0) {
- ssCurr++;
- i--;
+ if (input < stationCount) {
+ retStation = sortedStations[input];
}
- retStation = *ssCurr;
- free (ss);
+ free (sortedStations);
return retStation;
}