home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / mikmod / mlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-07  |  5.1 KB  |  250 lines

  1. /*  MikMod example player
  2.     (c) 1998 Miodrag Vallat and others - see file AUTHORS for complete list
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*==============================================================================
  20.  
  21.   $Id: mlist.c,v 1.14 1998/12/07 06:01:27 miod Exp $
  22.  
  23.   Functions to handle the playlist.
  24.  
  25. ==============================================================================*/
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30.  
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #ifdef HAVE_LIMITS_H
  35. #include <limits.h>
  36. #endif
  37. #ifdef __EMX__
  38. #define PATH_MAX _POSIX_PATH_MAX
  39. #endif
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <time.h>
  44.  
  45. #include <mikmod.h>
  46. #include "player.h"
  47.  
  48. void PL_InitList(PLAYLIST* pl)
  49. {
  50.     pl->numused=0;
  51.     pl->current=-1;
  52.     pl->file=pl->archive=NULL;
  53. }
  54.  
  55. void PL_ClearList(PLAYLIST* pl)
  56. {
  57.     int i;
  58.  
  59.     for(i=0;i<pl->numused;i++) {
  60.         free(pl->file[i]);
  61.         pl->file[i]=NULL;
  62.         free(pl->archive[i]);
  63.         pl->archive[i]=NULL;
  64.     }
  65.     if(pl->file) {
  66.         free(pl->file);
  67.         pl->file=NULL;
  68.     }
  69.     if(pl->archive) {
  70.         free(pl->archive);
  71.         pl->archive=NULL;
  72.     }
  73.     pl->current=pl->numused=0;
  74. }
  75.  
  76. int PL_GetCurrent(PLAYLIST* pl,CHAR* retfile,CHAR* retarc)
  77. {
  78.     int index=(pl->current>0)?pl->current:0;
  79.  
  80.     if(!pl->numused) return 1;
  81.  
  82.     strcpy(retfile,pl->file[index]);
  83.     if(pl->archive[index]) 
  84.         strcpy(retarc,pl->archive[index]);
  85.     else
  86.         strcpy(retarc,"");
  87.  
  88.     return (pl->current==(pl->numused-1))?1:0;
  89. }
  90.  
  91. int PL_GetNext(PLAYLIST* pl,CHAR* retfile,CHAR* retarc)
  92. {
  93.     int ret=0;
  94.     int index;
  95.  
  96.     if(!pl->numused) return 1;
  97.     pl->current++;
  98.     if(pl->current==pl->numused) {
  99.         pl->current=pl->numused-1; ret=1;
  100.     }
  101.  
  102.     index=(pl->current>0)?pl->current:0;
  103.     if(retfile)
  104.         strcpy(retfile,pl->file[index]);
  105.     if (retarc) {
  106.         if(pl->archive[index])
  107.             strcpy(retarc,pl->archive[index]);
  108.         else
  109.             strcpy(retarc,"");
  110.     }
  111.  
  112.     return ret;
  113. }
  114.  
  115. int PL_GetPrev(PLAYLIST* pl,CHAR* retfile,CHAR* retarc)
  116. {
  117.     int index;
  118.  
  119.     if(!pl->numused) return 1;
  120.     pl->current--;
  121.     if(pl->current<-1) pl->current=-1;
  122.  
  123.     index=(pl->current>0)?pl->current:0;
  124.     if(retfile)
  125.         strcpy(retfile,pl->file[index]);
  126.     if(retarc) {
  127.         if(pl->archive[index]) 
  128.             strcpy(retarc,pl->archive[index]);
  129.         else
  130.             strcpy(retarc,"");
  131.     }
  132.  
  133.     return 0;
  134. }
  135.  
  136. void PL_Randomize(PLAYLIST* pl)
  137. {
  138.     if(pl->numused>1) {
  139.         int current;
  140.  
  141. #if defined(__OS2__)||defined(__EMX__)
  142.         srand(time(NULL));
  143. #else
  144.         srandom(time(NULL));
  145. #endif
  146.         for(current=0;current<pl->numused-1;current++) {
  147. #if defined(__OS2__)||defined(__EMX__)
  148.             int target=((rand()*(pl->numused-current))/(RAND_MAX+1.0))+current;
  149. #else
  150.             int target=(random()%(pl->numused-current))+current;
  151. #endif
  152.  
  153.             if (target!=current) {
  154.                 CHAR* temp;
  155.  
  156.                 temp=pl->file[current];
  157.                 pl->file[current]=pl->file[target];
  158.                 pl->file[target]=temp;
  159.                 temp=pl->archive[current];
  160.                 pl->archive[current]=pl->archive[target];
  161.                 pl->archive[target]=temp;
  162.             }
  163.         }
  164.     }
  165. }
  166.  
  167. int PL_DelCurrent(PLAYLIST* pl)
  168. {
  169.     int i,index;
  170.  
  171.     if(!pl->numused) return 1;
  172.  
  173.     index=(pl->current>0)?pl->current:0;
  174.     free(pl->file[index]);
  175.     free(pl->archive[index]);
  176.  
  177.     pl->numused--;
  178.     pl->current--;
  179.     for(i=index;i<pl->numused;i++) {
  180.         pl->file[i]=pl->file[i+1];
  181.         pl->archive[i]=pl->archive[i+1];
  182.     }
  183.     pl->file=realloc(pl->file,pl->numused*sizeof(CHAR*));
  184.     pl->archive=realloc(pl->archive,pl->numused*sizeof(CHAR*));
  185.  
  186.     return 0;
  187. }
  188.  
  189. void PL_Add(PLAYLIST* pl,CHAR* file,CHAR* arc)
  190. {
  191.     pl->numused++;
  192.     pl->file=realloc(pl->file,pl->numused*sizeof(CHAR*));
  193.     pl->archive=realloc(pl->archive,pl->numused*sizeof(CHAR*));
  194.  
  195.     pl->file[pl->numused-1]=malloc((strlen(file)+1)*sizeof(CHAR));
  196.     strcpy(pl->file[pl->numused-1],file);
  197.     if(arc) {
  198.         pl->archive[pl->numused-1]=malloc((strlen(arc)+1)*sizeof(CHAR));
  199.         strcpy(pl->archive[pl->numused-1],arc);
  200.     } else
  201.         pl->archive[pl->numused-1]=NULL;
  202. }
  203.  
  204. BOOL PL_Load(PLAYLIST* pl,CHAR* filename)
  205. {
  206.     FILE *f;
  207.     CHAR file[PATH_MAX*2+2];    /* two complete paths + separator + null */
  208.     CHAR *arc;
  209.  
  210.     if(!(f=fopen(filename,"r"))) {
  211.         MikMod_errno=MMERR_OPENING_FILE;
  212.         return 0;
  213.     }
  214.  
  215.     while(!feof(f)) {
  216.         fgets(file,PATH_MAX*2+1,f);
  217.         if(file[strlen(file)-1]=='\n') file[strlen(file)-1]=0;
  218.         arc=strchr(file,'|');
  219.         if(!arc)
  220.             MA_FindFiles(pl,file);
  221.         else {
  222.             *arc=0;
  223.             arc++;
  224.             PL_Add(pl,arc,file);
  225.         }
  226.     }
  227.     fclose(f);
  228.     return 1;
  229. }
  230.  
  231. BOOL PL_Save(PLAYLIST* pl,CHAR* savefile)
  232. {
  233.     FILE *f;
  234.     int i;
  235.  
  236.     if(!(f=fopen(savefile,"w"))) {
  237.         MikMod_errno=MMERR_OPENING_FILE;
  238.         return 0;
  239.     }
  240.  
  241.     for(i=0;i<pl->numused;i++) {
  242.         if(pl->archive[i])
  243.             fprintf(f,"%s|%s\n",pl->archive[i],pl->file[i]);
  244.         else
  245.             fprintf(f,"%s\n",pl->file[i]);
  246.     }
  247.     fclose(f);
  248.     return 1;
  249. }
  250.