File: pl.c

package info (click to toggle)
libnjb 1.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,952 kB
  • ctags: 643
  • sloc: ansic: 10,718; sh: 7,926; makefile: 220
file content (182 lines) | stat: -rw-r--r-- 3,398 bytes parent folder | download
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
#include <string.h>
#include <libnjb.h>
#include <getopt.h>

#define PL_CREATE	1
#define PL_DELETE	2
#define PL_RENAME	3

void usage(void);

int main (int argc, char **argv)
{
	njb_t njbs[NJB_MAX_DEVICES], *njb;
	playlist_t *playlist;
	playlist_track_t *pl_track;
	int i, n, opt, mode, debug;
	char *name, *newname;
	extern int optind, njb_error;
	extern char *optarg;
	char *endptr;
	char *lang;

	debug= 0;
	mode= 0;
	name= NULL;
	newname = NULL;
	while ( (opt= getopt(argc, argv, "d:r:c:")) != -1 ) {
		switch (opt) {
		case 'c':
			mode= PL_CREATE;
			break;
		case 'd':
			mode= PL_DELETE;
			break;
		case 'r':
			mode= PL_RENAME;
			break;
		case 'D':
			debug= atoi(optarg);
			break;
		case '?':
		default:
			usage();
			return 1;
		}
		name= optarg;
	}
	argc-= optind;
	argv+= optind;

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

	if ( ! mode ) {
		usage();
		return 1;
	}

	playlist = NULL;
	if ( mode == PL_CREATE ) {
		playlist= playlist_new();
		if ( playlist == NULL ) {
			perror("playlist_new");
			return 1;
		}

		if ( playlist_set_name(playlist, name) == -1 ) {
			njb_error_dump(stderr);
		} else {
			for (i= 0; i< argc; i++) {
				u_int32_t trid= strtoul(argv[i], &endptr, 10);
				if ( *endptr != '\0' ) {
					fprintf(stderr, "invalid track id %s",
						argv[i]);
					return 1;
				}

				pl_track= playlist_track_new(trid);
				if ( pl_track == NULL ) {
					perror("playlist_track_new");
					return 1;
				}

				playlist_addtrack(playlist, pl_track,
					NJB_PL_END);
			}

			playlist_dump(playlist, stdout);
		}
	} else if ( mode == PL_RENAME ) {
		newname= argv[0];
	}

	if ( NJB_Discover(njbs, 0, &n) == -1 ) njb_error_dump(stderr);
	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;
	}

	if ( NJB_Capture(njb) == -1 ) {
		njb_error_dump(stderr);
		return 1;
	}

	if ( mode == PL_CREATE ) {
		if ( NJB_Update_Playlist(njb, playlist) == -1 )
			njb_error_dump(stderr);
	} else {
		int status = 0;
		int repeat= 1;

		NJB_Reset_Get_Playlist(njb);
		while ( repeat ) {
			playlist= NJB_Get_Playlist(njb);
			if ( playlist == NULL ) { 
				repeat= 0;
			} else {
				repeat = strcmp(name, playlist->name);
				if ( repeat )
					playlist_destroy(playlist);
			}
		}

		if ( playlist == NULL ) {
			if ( njb_error == EO_EOM ) {
				fprintf(stderr, "playlist \"%s\" not found\n",
					name);
			} else {
				njb_error_dump(stderr);
			}
		} else {
			if ( mode == PL_DELETE ) {
				status= NJB_Delete_Playlist(njb,
					playlist->plid);
			} else {
				if ( playlist_set_name(playlist, newname) == -1 ) {
					status= -1;
				} else {
					status= NJB_Update_Playlist(njb,
						playlist);
				}
			}
		}

		if ( status == -1 ) njb_error_dump(stderr);
	}

	NJB_Release(njb);

	NJB_Close(njb);

	return 0;
}

void usage (void)
{
	fprintf(stderr, "usage: pl [ -D debuglvl ] -d name\n\
       pl [ -D debuglvl ] -r oldname newname\n\
       pl [ -D debuglvl ] -c name trackid < trackid2 trackid3 ... >\n");
}