libnjb: tagtr.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# tagtr.c

    00001 #include "common.h"
    00002 #include <string.h>
    00003
    00004 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
    00005 {
    00006   char *cp, *bp;
    00007
    00008   while (1) {
    00009     fprintf(stdout, "%s> ", prompt);
    00010     if ( fgets(buffer, bufsz, stdin) == NULL ) {
    00011       if (ferror(stdin)) {
    00012         perror("fgets");
    00013       } else {
    00014         fprintf(stderr, "EOF on stdin\n");
    00015       }
    00016       return NULL;
    00017     }
    00018
    00019     cp= strrchr(buffer, '\n');
    00020     if ( cp != NULL ) *cp= '\0';
    00021
    00022     bp= buffer;
    00023     while ( bp != cp ) {
    00024       if ( *bp != ' ' && *bp != '\t' ) return bp;
    00025       bp++;
    00026     }
    00027
    00028     if (! required) return bp;
    00029   }
    00030 }
    00031
    00032 static void usage (void)
    00033 {
    00034   fprintf(stderr, "usage: tagtr [ -D debuglvl ] <trackid>\n");
    00035   exit(1);
    00036 }
    00037
    00038 int main(int argc, char **argv)
    00039 {
    00040   njb_t njbs[NJB_MAX_DEVICES], *njb;
    00041   int n, opt, debug, lookup;
    00042   extern char *optarg;
    00043   extern int optind;
    00044   char filename[80], artist[80], title[80], genre[80], codec[80], album[80];
    00045   char num[80];
    00046   char *pfilename, *partist, *ptitle, *pgenre, *pcodec, *palbum, *pnum;
    00047   char *endptr;
    00048   u_int16_t tracknum, length, year;
    00049   u_int32_t filesize;
    00050   int trackid;
    00051   char *lang;
    00052   njb_songid_t *songid;
    00053   njb_songid_frame_t *frame;
    00054
    00055   debug = 0;
    00056   lookup = 0;
    00057
    00058   while ( (opt= getopt(argc, argv, "D:l")) != -1 ) {
    00059     switch (opt) {
    00060     case 'l':
    00061       lookup= 1;
    00062     case 'D':
    00063       debug= atoi(optarg);
    00064       break;
    00065     default:
    00066       usage();
    00067     }
    00068   }
    00069   argc-= optind;
    00070   argv+= optind;
    00071
    00072   if ( argc != 1 ) usage();
    00073
    00074   if ( debug ) NJB_Set_Debug(debug);
    00075
    00076   /*
    00077    * Check environment variables $LANG and $LC_CTYPE
    00078    * to see if we want to support UTF-8 unicode
    00079    * $LANG = "xx_XX.UTF-8" or $LC_CTYPE = "?"
    00080    * trigger unicode support.
    00081    */
    00082   lang = getenv("LANG");
    00083   if (lang != NULL) {
    00084     if (strlen(lang) > 5) {
    00085       if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
    00086         NJB_Set_Unicode(NJB_UC_UTF8);
    00087       }
    00088     }
    00089   }
    00090
    00091   trackid= strtoul(argv[0], &endptr, 10);
    00092   if ( endptr[0] != '\0' ) {
    00093     fprintf(stderr, "invalid id %s\n", argv[1]);
    00094     return 1;
    00095   }
    00096
    00097   if ( (pcodec= prompt("CODEC", codec, 80, 0)) == NULL ) return 1;
    00098   if ( ! strlen(pcodec) ) pcodec= NULL;
    00099
    00100   if ( (ptitle= prompt("Title", title, 80, 0)) == NULL ) return 1;
    00101   if ( ! strlen(ptitle) ) ptitle= NULL;
    00102
    00103   if ( (palbum= prompt("Album", album, 80, 0)) == NULL ) return 1;
    00104   if ( ! strlen(palbum) ) palbum= NULL;
    00105
    00106   if ( (partist= prompt("Artist", artist, 80, 0)) == NULL ) return 1;
    00107   if ( ! strlen(partist) ) partist= NULL;
    00108
    00109   if ( (pgenre= prompt("Genre", genre, 80, 0)) == NULL ) return 1;
    00110   if ( ! strlen(pgenre) ) pgenre= NULL;
    00111
    00112   if ( (pfilename= prompt("File path", filename, 80, 0)) == NULL ) return 1;
    00113   if ( ! strlen(pfilename) ) pfilename= NULL;
    00114
    00115   if ( (pnum= prompt("Track number", num, 80, 0)) == NULL ) return 1;
    00116   if ( strlen(pnum) ) {
    00117     tracknum = strtoul(pnum, 0, 10);
    00118   } else {
    00119     tracknum = 0;
    00120   }
    00121
    00122   if ( (pnum= prompt("Length", num, 80, 0)) == NULL ) return 1;
    00123   if ( strlen(pnum) ) {
    00124     length= strtoul(pnum, 0, 10);
    00125   } else {
    00126     length= 0;
    00127   }
    00128
    00129   if ( (pnum= prompt("Year", num, 80, 0)) == NULL ) return 1;
    00130   if ( strlen(pnum) ) {
    00131     year = strtoul(pnum, 0, 10);
    00132   } else {
    00133     year = 0;
    00134   }
    00135
    00136   filesize= 0;
    00137   if ( ! lookup ) {
    00138     if ( (pnum= prompt("File size", num, 80, 0)) == NULL )
    00139       return 1;
    00140     if ( strlen(pnum) ) {
    00141       filesize = strtoul(pnum, 0, 10);
    00142     } else {
    00143       filesize = 0;
    00144     }
    00145   }
    00146
    00147   if (NJB_Discover(njbs, 0, &n) == -1) {
    00148     fprintf(stderr, "could not locate any jukeboxes\n");
    00149     return 1;
    00150   }
    00151
    00152   if ( n == 0 ) {
    00153     fprintf(stderr, "no NJB devices found\n");
    00154     return 0;
    00155   }
    00156
    00157   njb= njbs;
    00158
    00159   if ( NJB_Open(njb) == -1 ) {
    00160     NJB_Error_Dump(njb,stderr);
    00161     return 1;
    00162   }
    00163
    00164   NJB_Capture(njb);
    00165
    00166   songid = NJB_Songid_New();
    00167   frame = NJB_Songid_Frame_New_Codec(pcodec);
    00168   NJB_Songid_Addframe(songid, frame);
    00169   frame = NJB_Songid_Frame_New_Filesize(filesize);
    00170   NJB_Songid_Addframe(songid, frame);
    00171   frame = NJB_Songid_Frame_New_Title(ptitle);
    00172   NJB_Songid_Addframe(songid, frame);
    00173   frame = NJB_Songid_Frame_New_Album(palbum);
    00174   NJB_Songid_Addframe(songid, frame);
    00175   frame = NJB_Songid_Frame_New_Artist(partist);
    00176   NJB_Songid_Addframe(songid, frame);
    00177   frame = NJB_Songid_Frame_New_Genre(pgenre);
    00178   NJB_Songid_Addframe(songid, frame);
    00179   frame = NJB_Songid_Frame_New_Year(year);
    00180   NJB_Songid_Addframe(songid, frame);
    00181   frame = NJB_Songid_Frame_New_Tracknum(tracknum);
    00182   NJB_Songid_Addframe(songid, frame);
    00183   frame = NJB_Songid_Frame_New_Length(length);
    00184   NJB_Songid_Addframe(songid, frame);
    00185   frame = NJB_Songid_Frame_New_Filename(pfilename);
    00186   NJB_Songid_Addframe(songid, frame);
    00187
    00188   if ( NJB_Replace_Track_Tag(njb, trackid, songid) == -1 ) {
    00189     NJB_Error_Dump(njb,stderr);
    00190   } else {
    00191     printf("NJB track ID:    %u\n", trackid);
    00192   }
    00193   printf("\n");
    00194
    00195   NJB_Songid_Destroy(songid);
    00196
    00197   NJB_Release(njb);
    00198
    00199   return 0;
    00200 }