gettr.c

00001 #include "common.h"
00002 
00003 static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
00004 {
00005   int percent = (sent*100)/total;
00006 #ifdef __WIN32__
00007   printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
00008 #else
00009   printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
00010 #endif
00011   fflush(stdout);
00012   return 0;
00013 }
00014 
00015 static void usage (void)
00016 {
00017   fprintf(stderr, "get [ -s size ] <trackid> <filename>\n");
00018 }
00019 
00020 int main (int argc, char **argv)
00021 {
00022   njb_t njbs[NJB_MAX_DEVICES], *njb;
00023   int n, opt, debug;
00024   u_int32_t id, size;
00025   extern int optind;
00026   extern char *optarg;
00027   char *endptr;
00028   char *file;
00029   
00030   debug= 0;
00031   size= 0;
00032   while ( (opt= getopt(argc, argv, "D:s:")) != -1 ) {
00033     switch (opt) {
00034     case 'D':
00035       debug= atoi(optarg);
00036       break;
00037     case 's':
00038       size= strtoul(optarg, &endptr, 10);
00039       if ( *endptr != '\0' ) {
00040         fprintf(stderr, "illegal size value %s\n",
00041                 optarg);
00042         return 1;
00043       }
00044       break;
00045     default:
00046       usage();
00047       return 1;
00048     }
00049   }
00050   argc -= optind;
00051   argv += optind;
00052   
00053   if ( argc != 2 ) {
00054     usage();
00055     return 1;
00056   }
00057   
00058   id= strtoul(argv[0], &endptr, 10);
00059   if ( *endptr != 0 ) {
00060     fprintf(stderr, "illegal value %s\n", optarg);
00061     return 1;
00062   } else if ( ! id ) {
00063     fprintf(stderr, "bad song id %u\n", id);
00064     return 1;
00065   }
00066   
00067   file= argv[1];
00068   
00069   if ( debug ) NJB_Set_Debug(debug);
00070 
00071   if (NJB_Discover(njbs, 0, &n) == -1) {
00072     fprintf(stderr, "could not locate any jukeboxes\n");
00073     return 1;
00074   }
00075 
00076   if ( n == 0 ) {
00077     fprintf(stderr, "no NJB devices found\n");
00078     return 1;
00079   } 
00080   
00081   njb = njbs;
00082   
00083   if ( NJB_Open(njb) == -1 ) {
00084     NJB_Error_Dump(njb,stderr);
00085     return 1;
00086   }
00087   
00088   NJB_Capture(njb);
00089   
00090   if ( ! size ) {
00091     njb_songid_t *tag;
00092     
00093     printf("Locating track %u\n", id);
00094     NJB_Reset_Get_Track_Tag(njb);
00095     while ( (tag = NJB_Get_Track_Tag(njb)) ) {
00096       if ( tag->trid == id ) {
00097         njb_songid_frame_t *frame;
00098         
00099         frame = NJB_Songid_Findframe(tag, FR_SIZE);
00100         size = frame->data.u_int32_val;
00101       }
00102       NJB_Songid_Destroy(tag);
00103     }
00104 
00105     /* Dump any pending errors */
00106     if (NJB_Error_Pending(njb)) {
00107       NJB_Error_Dump(njb, stderr);
00108     }
00109     
00110     if ( size ) {
00111       printf("%u bytes\n", size);
00112     } else {
00113       fprintf(stderr, "Track %u not found\n", id);
00114     }
00115   }
00116   
00117   if ( size ) {
00118     if ( NJB_Get_Track(njb, id, size, file, progress, NULL) == -1 ) {
00119       NJB_Error_Dump(njb, stderr);
00120     }
00121     printf("\n");
00122   }
00123   
00124   NJB_Release(njb);
00125   
00126   NJB_Close(njb);
00127   
00128   return 0;
00129 }

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