1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include <string.h>
#include <libnjb.h>
#include <getopt.h>
char *prompt (const char *prompt, char *buffer, size_t bufsz, int required);
void usage (void);
int main(int argc, char **argv)
{
njb_t njbs[NJB_MAX_DEVICES], *njb;
int n, opt, debug, lookup;
extern char *optarg;
extern int optind;
char fname[80], artist[80], title[80], genre[80], codec[80], album[80];
char num[80];
char *pfname, *partist, *ptitle, *pgenre, *pcodec, *palbum, *pnum;
char *endptr;
u_int32_t tracknum, length, size;
int trackid;
char *lang;
debug= lookup= 0;
while ( (opt= getopt(argc, argv, "D:l")) != -1 ) {
switch (opt) {
case 'l':
lookup= 1;
case 'D':
debug= atoi(optarg);
break;
default:
usage();
}
}
argc-= optind;
argv+= optind;
if ( argc != 1 ) usage();
if ( debug ) NJB_Set_Debug(debug);
/*
* Check environment variables $LANG and $LC_CTYPE
* to see if we want to support UTF-8 unicode
* $LANG = "xx_XX.UTF-8" or $LC_CTYPE = "?"
* trigger unicode support.
*/
lang = getenv("LANG");
if (lang != NULL) {
if (strlen(lang) > 5) {
if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
NJB_Set_Unicode(NJB_UC_UTF8);
}
}
}
trackid= strtoul(argv[0], &endptr, 10);
if ( endptr[0] != '\0' ) {
fprintf(stderr, "invalid id %s\n", argv[1]);
return 1;
}
if ( (pcodec= prompt("CODEC", codec, 80, 0)) == NULL ) return 1;
if ( ! strlen(pcodec) ) pcodec= NULL;
if ( (ptitle= prompt("Title", title, 80, 0)) == NULL ) return 1;
if ( ! strlen(ptitle) ) ptitle= NULL;
if ( (palbum= prompt("Album", album, 80, 0)) == NULL ) return 1;
if ( ! strlen(palbum) ) palbum= NULL;
if ( (partist= prompt("Artist", artist, 80, 0)) == NULL ) return 1;
if ( ! strlen(partist) ) partist= NULL;
if ( (pgenre= prompt("Genre", genre, 80, 0)) == NULL ) return 1;
if ( ! strlen(pgenre) ) pgenre= NULL;
if ( (pfname= prompt("File path", fname, 80, 0)) == NULL ) return 1;
if ( ! strlen(pfname) ) pfname= NULL;
if ( (pnum= prompt("Track number", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
tracknum= strtoul(pnum, 0, 10);
} else {
tracknum= 0;
}
if ( (pnum= prompt("Length", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
length= strtoul(pnum, 0, 10);
} else {
length= 0;
}
size= 0;
if ( ! lookup ) {
if ( (pnum= prompt("File size", num, 80, 0)) == NULL )
return 1;
if ( strlen(pnum) ) {
size= strtoul(pnum, 0, 10);
} else {
size= 0;
}
}
if ( NJB_Discover(njbs, 0, &n) == -1 ) {
njb_error_dump(stderr);
return 1;
}
if ( n == 0 ) {
fprintf(stderr, "no NJB devices found\n");
return 0;
}
njb= njbs;
if ( NJB_Open(njb) == -1 ) {
njb_error_dump(stderr);
return 1;
}
NJB_Capture(njb);
if ( NJB_Replace_Track_Tag(njb, trackid, pcodec, ptitle, palbum,
pgenre, partist, length, tracknum, size, pfname,
NULL, 0 ) == -1 ) {
njb_error_dump(stderr);
} else {
printf("NJB ID: %u\n", trackid);
}
printf("\n");
NJB_Release(njb);
return 0;
}
char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
{
char *cp, *bp;
while (1) {
fprintf(stdout, "%s> ", prompt);
if ( fgets(buffer, bufsz, stdin) == NULL ) {
if (ferror(stdin)) {
perror("fgets");
} else {
fprintf(stderr, "EOF on stdin\n");
}
return NULL;
}
cp= strrchr(buffer, '\n');
if ( cp != NULL ) *cp= '\0';
bp= buffer;
while ( bp != cp ) {
if ( *bp != ' ' && *bp != '\t' ) return bp;
bp++;
}
if (! required) return bp;
}
}
void usage (void)
{
fprintf(stderr, "usage: tagtr [ -D debuglvl ] <trackid>\n");
exit(1);
}
|