Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

inf.c

Go to the documentation of this file.
00001 /*
00002  *  inf.c
00003  *  mod_musicindex
00004  *
00005  *  $Id: inf.c,v 1.18 2003/10/28 20:50:11 boudinr Exp $
00006  *
00007  *  Created by Thibaut VARENE on Thu Mar 20 2003.
00008  *  Copyright (c) 2003 Regis BOUDIN
00009  *  
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU Lesser General Public License as published by
00012  *  the Free Software Foundation; either version 2.1, or (at your option)
00013  *  any later version.
00014  *
00015  */
00016 
00028 #include "inf.h"
00029 
00042 mu_ent *quicksort(mu_ent *base, mu_ent *end, mu_config * conf) 
00043 {
00044         mu_ent *prem;
00045         mu_ent *deux = base;
00046         /* The first entry will be used as reference */
00047         mu_ent *refe = base;
00048 
00049         /* If we have 0 or one entry, sorting is very easy */
00050         if ((base == end) || (base->next == end))
00051                 return base;
00052 
00053         /* We will begin comparisons with the second entry of the list */
00054         prem = base->next;
00055 
00056         do {
00057                 /* If the current entry is not correctly placed,
00058                 move it before the reference */
00059                 if (inf_global(prem, refe, conf) < 0) {
00060                         deux->next = prem->next;
00061                         prem->next = base;
00062                         base = prem;
00063                         prem = deux->next;
00064                 /* Otherwise, just continue working with the next one */
00065                 } else {
00066                         deux = prem;
00067                         prem = prem->next;
00068                 }
00069         } while (prem != end);
00070 
00071         /* We now have two lists, A & B, separated by a value V,
00072         considering A < V < B . Just run the same quicksort on A and B */
00073         base = quicksort(base, refe, conf);
00074         refe->next = quicksort(refe->next, end, conf);
00075 
00076         return base;
00077 }
00078 
00084 void set_fctptrs(mu_config * conf)
00085 {
00086         unsigned short i;
00087 
00088         for (i = 0; i < ARG_NUMBER; i++) {
00089                 switch (conf->order[i]) {
00090                         case SB_ARTIST:
00091                                 conf->order_functions[i] = inf_by_artist;
00092                                 break;
00093                         case SB_ALBUM:
00094                                 conf->order_functions[i] = inf_by_album;
00095                                 break;
00096                         case SB_TITLE:
00097                                 conf->order_functions[i] = inf_by_title;
00098                                 break;
00099                         case SB_TRACK:
00100                                 conf->order_functions[i] = inf_by_track;
00101                                 break;
00102                         case SB_POSN:
00103                                 conf->order_functions[i] = inf_by_posn;
00104                                 break;
00105                         case SB_LENGTH:
00106                                 conf->order_functions[i] = inf_by_length;
00107                                 break;
00108                         case SB_BITRATE:
00109                                 conf->order_functions[i] = inf_by_bitrate;
00110                                 break;
00111                         case SB_FILETYPE:
00112                                 conf->order_functions[i] = inf_by_filetype;
00113                                 break;
00114                         case SB_FILENAME:
00115                                 conf->order_functions[i] = inf_by_file;
00116                                 break;
00117                         case SB_URI:
00118                                 conf->order_functions[i] = inf_by_uri;
00119                                 break;
00120                         case SB_DATE:
00121                                 conf->order_functions[i] = inf_by_date;
00122                                 break;
00123                         case SB_SIZE:
00124                                 conf->order_functions[i] = inf_by_size;
00125                                 break;
00126                         case SB_GENRE:
00127                                 conf->order_functions[i] = inf_by_genre;
00128                                 break;
00129                         case SB_RANDOM:
00130                                 conf->order_functions[i] = inf_by_rand;
00131                                 break;
00132                         default:
00133                                 conf->order_functions[i] = inf_by_uri;
00134                 }
00135         }
00136 }
00137 
00138 int inf_by_rand(mu_ent *first, mu_ent *second)
00139 {
00140         return (rand()-(RAND_MAX/2));
00141 }
00142 
00143 int inf_by_track(mu_ent *first, mu_ent *second)
00144 {
00145         return (first->track - second->track);
00146 }
00147 
00148 int inf_by_posn(mu_ent *first, mu_ent *second)
00149 {
00150         return (first->posn - second->posn);
00151 }
00152 
00153 int inf_by_date(mu_ent *first, mu_ent *second)
00154 {
00155         return (first->date - second->date);
00156 }
00157 
00158 int inf_by_length(mu_ent *first, mu_ent *second)
00159 {
00160         return (first->length - second->length);
00161 }
00162 
00163 int inf_by_bitrate(mu_ent *first, mu_ent *second)
00164 {
00165         return (first->bitrate - second->bitrate);
00166 }
00167 
00168 int inf_by_size(mu_ent *first, mu_ent *second)
00169 {
00170         return (first->size - second->size);
00171 }
00172 
00173 int inf_by_artist(mu_ent *first, mu_ent *second)
00174 {
00175         if ((!first->artist) || (!second->artist)) {
00176                 if (first->artist)
00177                         return -1;
00178                 else if (second->artist)
00179                         return 1;
00180                 else
00181                         return 0;
00182         }
00183         return strcasecmp(first->artist, second->artist);
00184 }
00185 
00186 int inf_by_album(mu_ent *first, mu_ent *second)
00187 {
00188         if ((!first->album) || (!second->album)) {
00189                 if (first->album)
00190                         return -1;
00191                 else if (second->album)
00192                         return 1;
00193                 else
00194                         return 0;
00195         }
00196         return strcasecmp(first->album, second->album);
00197 }
00198 
00199 int inf_by_title(mu_ent *first, mu_ent *second)
00200 {
00201         if ((!first->title) || (!second->title)) {
00202                 if (first->title)
00203                         return -1;
00204                 else if (second->title)
00205                         return 1;
00206                 else
00207                         return 0;
00208         }
00209         return strcasecmp(first->title, second->title);
00210 }
00211 
00212 int inf_by_filetype(mu_ent *first, mu_ent *second)
00213 {
00214         return (first->filetype - second->filetype);
00215 }
00216 
00217 int inf_by_file(mu_ent *first, mu_ent *second)
00218 {
00219         return strcasecmp(first->file, second->file);
00220 }
00221 
00222 int inf_by_uri(mu_ent *first, mu_ent *second)
00223 {
00224         return strcmp(first->uri, second->uri);
00225 }
00226 
00227 int inf_by_genre(mu_ent *first, mu_ent *second)
00228 {
00229         if ((!first->genre) || (!second->genre)) {
00230                 if (first->genre)
00231                         return -1;
00232                 else if (second->genre)
00233                         return 1;
00234                 else
00235                         return 0;
00236         }
00237         return strcasecmp(first->genre, second->genre);
00238 }
00239 
00240 int inf_global(mu_ent *first, mu_ent *second, mu_config * conf)
00241 {
00242         short result, i;
00243 
00244         if ((result = second->filetype - first->filetype))
00245                 return (result);
00246 
00247         if (first->filetype == FT_DIR)
00248                 return (inf_by_file(first, second));
00249 
00250         for (i = 0; i < ARG_NUMBER; i++) {
00251                 if (conf->order_functions[i]) {
00252                         if ((result = (conf->order_functions[i](first, second))))
00253                                 return result;
00254                 }
00255         }
00256         return 1;
00257 }

Generated on Thu Oct 30 13:50:29 2003 for mod_musicindex by doxygen 1.3.4