libnjb: tracks.c

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

# tracks.c

    00001 #include "common.h"
    00002 #include <stdio.h>
    00003 #include <string.h>
    00004
    00005 static void songid_frame_dump (njb_songid_frame_t *frame, FILE *fp)
    00006 {
    00007   fprintf(fp, "%s: ", frame->label);
    00008
    00009   if ( frame->type == NJB_TYPE_STRING ) {
    00010     fprintf(fp, "%s\n", frame->data.strval);
    00011   } else if (frame->type == NJB_TYPE_UINT16) {
    00012     fprintf(fp, "%d\n", frame->data.u_int16_val);
    00013   } else if (frame->type == NJB_TYPE_UINT32) {
    00014     fprintf(fp, "%u\n", frame->data.u_int32_val);
    00015   } else {
    00016     fprintf(fp, "(weird data word size, cannot display!)\n");
    00017   }
    00018 }
    00019
    00020 static void songid_dump (njb_songid_t *song, FILE *fp)
    00021 {
    00022   njb_songid_frame_t *frame;
    00023
    00024   NJB_Songid_Reset_Getframe(song);
    00025   fprintf(fp, "ID: %u\n", song->trid);
    00026   while( (frame = NJB_Songid_Getframe(song)) ) {
    00027     songid_frame_dump(frame, fp);
    00028   }
    00029 }
    00030
    00031 int main (int argc, char **argv)
    00032 {
    00033   njb_t njbs[NJB_MAX_DEVICES], *njb;
    00034   extern char *optarg;
    00035   int opt;
    00036   int n, debug, rc = 1;
    00037   int extended = 0; /* Whether to get extended track info */
    00038   njb_songid_t *songtag;
    00039   char *lang;
    00040
    00041   debug= 0;
    00042   while ( (opt= getopt(argc, argv, "D:E")) != -1 ) {
    00043     switch (opt) {
    00044     case 'D':
    00045       debug = atoi(optarg);
    00046       break;
    00047     case 'E':
    00048       extended = 1;
    00049       break;
    00050     default:
    00051       fprintf(stderr, "usage: tracks [ -D debuglvl ] -E\n");
    00052       return 1;
    00053     }
    00054   }
    00055
    00056   if ( debug ) NJB_Set_Debug(debug);
    00057
    00058   /*
    00059    * Check environment variables $LANG and $LC_CTYPE
    00060    * to see if we want to support UTF-8 unicode
    00061    * $LANG = "xx_XX.UTF-8" or $LC_CTYPE = "?"
    00062    * trigger unicode support.
    00063    */
    00064   lang = getenv("LANG");
    00065   if (lang != NULL) {
    00066     if (strlen(lang) > 5) {
    00067       if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
    00068         NJB_Set_Unicode(NJB_UC_UTF8);
    00069       }
    00070     }
    00071   }
    00072
    00073   if (NJB_Discover(njbs, 0, &n) == -1) {
    00074     fprintf(stderr, "could not locate any jukeboxes\n");
    00075     return 1;
    00076   }
    00077
    00078   if ( n == 0 ) {
    00079     fprintf(stderr, "no NJB devices found\n");
    00080     return 0;
    00081   }
    00082
    00083   njb = njbs;
    00084
    00085   if ( NJB_Open(njb) == -1 ) {
    00086     NJB_Error_Dump(njb,stderr);
    00087     return 1;
    00088   }
    00089
    00090   if ( NJB_Capture(njb) == -1 ) {
    00091     NJB_Error_Dump(njb,stderr);
    00092     goto err1;
    00093   }
    00094
    00095   if (extended != 0) {
    00096     NJB_Get_Extended_Tags(njb, 1);
    00097   }
    00098
    00099   n = 0;
    00100   NJB_Reset_Get_Track_Tag(njb);
    00101   while ( (songtag = NJB_Get_Track_Tag(njb)) ) {
    00102     songid_dump(songtag, stdout);
    00103     NJB_Songid_Destroy(songtag);
    00104     printf("----------------------------------\n");
    00105     n ++;
    00106   }
    00107
    00108   /* Dump any pending errors */
    00109   if (NJB_Error_Pending(njb)) {
    00110     NJB_Error_Dump(njb,stderr);
    00111   }
    00112
    00113   printf("In total: %u tracks.\n", n);
    00114
    00115   NJB_Release(njb);
    00116   rc = 0;
    00117
    00118 err1:
    00119   NJB_Close(njb);
    00120   return rc;
    00121 }
    00122