pl.c

00001 #include "common.h"
00002 #include <string.h>
00003 
00004 #define PL_CREATE       1
00005 #define PL_DELETE       2
00006 #define PL_RENAME       3
00007 
00008 static void usage(void)
00009 {
00010   fprintf(stderr, "usage: pl [ -D debuglvl ] -d name\n" \
00011           "pl [ -D debuglvl ] -r oldname newname\n" \
00012           "pl [ -D debuglvl ] -c name trackid < trackid2 trackid3 ... >\n");
00013 }
00014 
00015 static void playlist_dump (njb_playlist_t *pl, FILE *fp)
00016 {
00017   njb_playlist_track_t *track;
00018   unsigned int i = 1;
00019   
00020   fprintf(fp, "Playlist: %s\n", pl->name);
00021   fprintf(fp, "ID:       %u\n", pl->plid);
00022   fprintf(fp, "Tracks:   %u\n", pl->ntracks);
00023   fprintf(fp, "State:    %d\n", pl->_state);
00024   
00025   NJB_Playlist_Reset_Gettrack(pl);
00026   while ( (track = NJB_Playlist_Gettrack(pl)) ) {
00027     fprintf(fp, "%5u - track ID %u\n", i, track->trackid);
00028     i++;
00029   }
00030 }
00031 
00032 int main (int argc, char **argv)
00033 {
00034   njb_t njbs[NJB_MAX_DEVICES];
00035   njb_t *njb;
00036   njb_playlist_t *playlist;
00037   njb_playlist_track_t *pl_track;
00038   int i, n, opt, mode, debug;
00039   char *name, *newname;
00040   extern int optind;
00041   extern char *optarg;
00042   char *endptr;
00043   char *lang;
00044   
00045   debug= 0;
00046   mode= 0;
00047   name= NULL;
00048   newname = NULL;
00049   while ( (opt= getopt(argc, argv, "d:r:c:")) != -1 ) {
00050     switch (opt) {
00051     case 'c':
00052       mode= PL_CREATE;
00053       break;
00054     case 'd':
00055       mode= PL_DELETE;
00056       break;
00057     case 'r':
00058       mode= PL_RENAME;
00059       break;
00060     case 'D':
00061       debug= atoi(optarg);
00062       break;
00063     case '?':
00064     default:
00065       usage();
00066       return 1;
00067     }
00068     name= optarg;
00069   }
00070   argc-= optind;
00071   argv+= optind;
00072   
00073   if ( debug ) NJB_Set_Debug(debug);
00074   
00075   /*
00076    * Check environment variables $LANG and $LC_CTYPE
00077    * to see if we want to support UTF-8 unicode
00078    * $LANG = "xx_XX.UTF-8" or $LC_CTYPE = "?"
00079    * trigger unicode support.
00080    */
00081   lang = getenv("LANG");
00082   if (lang != NULL) {
00083     if (strlen(lang) > 5) {
00084       if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
00085         NJB_Set_Unicode(NJB_UC_UTF8);
00086       }
00087     }
00088   }
00089   
00090   if ( ! mode ) {
00091     usage();
00092     return 1;
00093   }
00094   
00095   playlist = NULL;
00096   if ( mode == PL_CREATE ) {
00097     playlist = NJB_Playlist_New();
00098     if ( playlist == NULL ) {
00099       perror("NJB_Playlist_New");
00100       return 1;
00101     }
00102     
00103     if ( NJB_Playlist_Set_Name(playlist, name) == -1 ) {
00104       perror("NJB_Playlist_Set_Name");
00105     } else {
00106       for (i= 0; i< argc; i++) {
00107         u_int32_t trid= strtoul(argv[i], &endptr, 10);
00108         if ( *endptr != '\0' ) {
00109           fprintf(stderr, "invalid track id %s",
00110                   argv[i]);
00111           return 1;
00112         }
00113         
00114         pl_track= NJB_Playlist_Track_New(trid);
00115         if ( pl_track == NULL ) {
00116           perror("NJB_Playlist_Track_New");
00117           return 1;
00118         }
00119         
00120         NJB_Playlist_Addtrack(playlist, pl_track,
00121                               NJB_PL_END);
00122       }
00123       playlist_dump(playlist, stdout);
00124     }
00125   } else if ( mode == PL_RENAME ) {
00126     newname= argv[0];
00127   }
00128   
00129   if (NJB_Discover(njbs, 0, &n) == -1) {
00130     fprintf(stderr, "could not locate any jukeboxes\n");
00131     return 1;
00132   }
00133   
00134   if ( n == 0 ) {
00135     fprintf(stderr, "no NJB devices found\n");
00136     return 0;
00137   }
00138   
00139   njb = njbs;
00140   
00141   if ( NJB_Open(njb) == -1 ) {
00142     NJB_Error_Dump(njb,stderr);
00143     return 1;
00144   }
00145   
00146   if ( NJB_Capture(njb) == -1 ) {
00147     NJB_Error_Dump(njb,stderr);
00148     return 1;
00149   }
00150   
00151   if ( mode == PL_CREATE ) {
00152     if ( NJB_Update_Playlist(njb, playlist) == -1 )
00153       NJB_Error_Dump(njb,stderr);
00154   } else {
00155     int status = 0;
00156     int repeat= 1;
00157     
00158     NJB_Reset_Get_Playlist(njb);
00159     while ( repeat ) {
00160       playlist = NJB_Get_Playlist(njb);
00161       if ( playlist == NULL ) { 
00162         repeat= 0;
00163       } else {
00164         repeat = strcmp(name, playlist->name);
00165         if ( repeat )
00166           NJB_Playlist_Destroy(playlist);
00167       }
00168     }
00169     
00170     if ( playlist == NULL ) {
00171       NJB_Error_Dump(njb,stderr);
00172     } else {
00173       if ( mode == PL_DELETE ) {
00174         status= NJB_Delete_Playlist(njb,
00175                                     playlist->plid);
00176       } else {
00177         if ( NJB_Playlist_Set_Name(playlist, newname) == -1 ) {
00178           status = -1;
00179         } else {
00180           status= NJB_Update_Playlist(njb,
00181                                       playlist);
00182         }
00183       }
00184     }
00185     
00186     if ( status == -1 ) NJB_Error_Dump(njb,stderr);
00187   }
00188   
00189   NJB_Release(njb);
00190   
00191   NJB_Close(njb);
00192   
00193   return 0;
00194 }
00195 

Generated on Mon Sep 11 00:52:12 2006 for libnjb by  doxygen 1.4.7