File: sendtr.c

package info (click to toggle)
libnjb 2.2.5-4.3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,528 kB
  • ctags: 1,072
  • sloc: ansic: 11,860; sh: 8,611; makefile: 144
file content (228 lines) | stat: -rw-r--r-- 5,639 bytes parent folder | download | duplicates (3)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "common.h"
#include <string.h>
#include <sys/stat.h>
#ifdef HAVE_LIBGEN_H
#include <libgen.h> /* basename() */
#endif

/* Function that compensate for missing libgen.h on Windows */
#ifndef HAVE_LIBGEN_H
static char *basename(char *in) {
  char *p;
  if (in == NULL)
    return NULL;
  p = in + strlen(in) - 1;
  while (*p != '\\' && *p != '/' && *p != ':')
    { p--; }
  return ++p;
}
#endif

static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
{
  int percent = (sent*100)/total;
#ifdef __WIN32__
  printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
#else
  printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
#endif
  fflush(stdout);
  return 0;
}

static 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;
  }
}

static void usage (void)
{
  fprintf(stderr, "usage: sendtr [ -D debuglvl ] <path>\n");
  exit(1);
}


int main(int argc, char **argv)
{
  njb_t njbs[NJB_MAX_DEVICES], *njb;
  int n, opt, debug;
  extern int optind;
  extern char *optarg;
  char *path, *filename;
  char artist[80], title[80], genre[80], codec[80], album[80];
  char num[80];
  char *partist, *ptitle, *pgenre, *pcodec, *palbum, *pnum;
  u_int16_t tracknum, length, year;
  u_int32_t filesize;
  struct stat sb;
  u_int32_t trackid;
  char *lang;
  njb_songid_t *songid;
  njb_songid_frame_t *frame;
  
  debug= 0;
  while ( (opt= getopt(argc, argv, "D:")) != -1 ) {
    switch (opt) {
    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);
      }
    }
  }
  
  path = argv[0];

  filename = basename(path);
  if ( stat(path, &sb) == -1 ) {
    fprintf(stderr, "%s: ", path);
    perror("stat");
    return 1;
  }
  filesize = (u_int32_t) sb.st_size;
  
  if ( (pcodec = prompt("Codec", codec, 80, 1)) == NULL ) return 1;
  
  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 ( (pnum = prompt("Track number", num, 80, 0)) == NULL ) return 1;
  if ( strlen(pnum) ) {
    tracknum = strtoul(pnum, 0, 10);
  } else {
    tracknum = 0;
  }
  
  if ( (pnum = prompt("Year", num, 80, 0)) == NULL ) return 1;
  if ( strlen(pnum) ) {
    year = strtoul(pnum, 0, 10);
  } else {
    year = 0;
  }
  
  if ( (pnum = prompt("Length", num, 80, 0)) == NULL ) return 1;
  if ( strlen(pnum) ) {
    length = strtoul(pnum, 0, 10);
  } else {
    length = 0;
  }
  
  printf("Sending track:\n");
  printf("Codec:     %s\n", pcodec);
  if (ptitle) printf("Title:     %s\n", ptitle);
  if (palbum) printf("Album:     %s\n", palbum);
  if (partist) printf("Artist:    %s\n", partist);
  if (pgenre) printf("Genre:     %s\n", pgenre);
  if (year > 0) printf("Year:      %d\n", year);
  if (tracknum) printf("Track no:  %d\n", tracknum);
  if (length) printf("Length:    %d\n", length);
  
  if (NJB_Discover(njbs, 0, &n) == -1) {
    fprintf(stderr, "could not locate any jukeboxes\n");
    return 1;
  }
  
  if ( n == 0 ) {
    fprintf(stderr, "no NJB devices found\n");
    return 0;
  } 
  
  njb= njbs;
  
  if ( NJB_Open(njb) == -1 ) {
    NJB_Error_Dump(njb,stderr);
    return 1;
  }
  
  NJB_Capture(njb);
  
  songid = NJB_Songid_New();
  frame = NJB_Songid_Frame_New_Codec(pcodec);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Filesize(filesize);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Title(ptitle);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Album(palbum);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Artist(partist);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Genre(pgenre);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Year(year);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Tracknum(tracknum);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Length(length);
  NJB_Songid_Addframe(songid, frame);
  frame = NJB_Songid_Frame_New_Filename(filename);
  NJB_Songid_Addframe(songid, frame);
  
  if ( NJB_Send_Track(njb, path, songid, progress, NULL, &trackid) == -1 ) {
    printf("\n");
    NJB_Error_Dump(njb,stderr);
  } else {
    printf("\nNJB track ID:    %u\n", trackid);
  }
  printf("\n");
  
  NJB_Songid_Destroy(songid);
  
  NJB_Release(njb);
  NJB_Close(njb);
  
  return 0;
}