shithub: zuke

Download patch

ref: 238db3819fb5fecd884a487cb62abbf4fd22ba32
parent: bdd69bf8337def2c8b4074cec03640cd70c2d336
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Dec 22 10:49:40 EST 2020

add a new format of playlist; zuke will load the old one too

--- a/mkplist.c
+++ b/mkplist.c
@@ -13,7 +13,7 @@
 
 #define MAX(a, b) (a > b ? a : b)
 
-static Biobuf *bf;
+static Biobuf *bf, out;
 static Meta *curr;
 static Meta *all;
 static int numall;
@@ -223,35 +223,26 @@
 }
 
 static void
-printmeta(Meta *m, int index)
+printmeta(Meta *m)
 {
-	static char buf[4096];
-	char *s;
 	int i;
 
-	buf[sizeof(buf)-2] = 0;
-	s = buf;
-
+	Bprint(&out, "%c %s\n%c %s\n", Ppath, m->path, Pfilefmt, m->filefmt);
 	for(i = 0; i < m->numartist; i++)
-		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Partist, strlen(m->artist[i]), m->artist[i]);
+		Bprint(&out, "%c %s\n", Partist, m->artist[i]);
 	if(m->album != nil)
-		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Palbum, strlen(m->album), m->album);
+		Bprint(&out, "%c %s\n", Palbum, m->album);
 	if(m->title != nil)
-		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Ptitle, strlen(m->title), m->title);
+		Bprint(&out, "%c %s\n", Ptitle, m->title);
 	if(m->date != nil)
-		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Pdate, strlen(m->date), m->date);
+		Bprint(&out, "%c %s\n", Pdate, m->date);
 	if(m->track != nil)
-		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Ptrack, strlen(m->track), m->track);
+		Bprint(&out, "%c %s\n", Ptrack, m->track);
 	if(m->duration > 0)
-		s = seprint(s, buf+sizeof(buf), "%c %llud\n", Pduration, m->duration);
+		Bprint(&out, "%c %llud\n", Pduration, m->duration);
 	if(m->imagesize > 0)
-		s = seprint(s, buf+sizeof(buf), "%c %d %d %d %s\n", Pimage, m->imageoffset, m->imagesize, m->imagereader, m->imagefmt);
-	s = seprint(s, buf+sizeof(buf), "%c %s\n%c %s\n", Pfilefmt, m->filefmt, Ppath, m->path);
-
-	if(buf[sizeof(buf)-2] != 0)
-		sysfatal("buffer size of %d bytes was a bad choice", sizeof(buf));
-
-	print("%c %d %d\n%s", Precord, index, (int)(s-buf), buf);
+		Bprint(&out, "%c %d %d %d %s\n", Pimage, m->imageoffset, m->imagesize, m->imagereader, m->imagefmt);
+	Bprint(&out, "\n");
 }
 
 static int
@@ -307,6 +298,8 @@
 		exits("usage");
 	}
 
+	Binit(&out, 1, OWRITE);
+
 	for(i = 1; i < argc; i++){
 		if(strncmp(argv[i], "http://", 7) == 0 || strncmp(argv[i], "https://", 8) == 0){
 			if((curr = newmeta()) == nil)
@@ -322,14 +315,14 @@
 		}
 	}
 	qsort(all, numall, sizeof(Meta), cmpmeta);
-	print("# %d\n", numall);
 	for(i = 0; i < numall; i++){
 		if(all[i].numartist < 1)
 			fprint(2, "no artists: %s\n", all[i].path);
 		if(all[i].title == nil)
 			fprint(2, "no title: %s\n", all[i].path);
-		printmeta(&all[i], i);
+		printmeta(all+i);
 	}
+	Bterm(&out);
 	fprint(2, "found %d tagged tracks\n", numall);
 	exits(nil);
 }
--- a/zuke.c
+++ b/zuke.c
@@ -674,6 +674,62 @@
 }
 
 static void
+readplistnew(void)
+{
+	char *s, *e, *a[5];
+	Meta *m;
+	int plsz;
+
+	plnum = 0;
+	plsz = 0;
+	pl = nil;
+	m = nil;
+	for(s = plraw;; s = e){
+		if((e = strchr(s, '\n')) == nil)
+			break;
+		if(plsz - plnum < 32){
+			plsz = (plsz + 32)*2;
+			if((pl = realloc(pl, sizeof(Meta)*plsz)) == nil)
+				sysfatal("memory");
+			memset(pl+plnum, 0, sizeof(Meta)*(plsz-plnum));
+		}
+		m = pl + plnum;
+		s += 2;
+		*e++ = 0;
+		switch(s[-2]){
+		case 0:
+			if(m->path != nil)
+				plnum++;
+			m = nil;
+			break;
+		case Pimage:
+			if(tokenize(s, a, nelem(a)) >= 4){
+				m->imageoffset = atoi(a[0]);
+				m->imagesize = atoi(a[1]);
+				m->imagereader = atoi(a[2]);
+				m->imagefmt = a[3];
+			}
+			break;
+		case Pduration:
+			m->duration = strtoull(s, nil, 0);
+			break;
+		case Partist:
+			if(m->numartist < Maxartist)
+				m->artist[m->numartist++] = s;
+			break;
+		case Pfilefmt: m->filefmt = s; break;
+		case Palbum:   m->album = s; break;
+		case Pdate:    m->date = s; break;
+		case Ppath:    m->path = s; break;
+		case Ptitle:   m->title = s; break;
+		case Ptrack:   m->track = s; break;
+		}
+	}
+	if(m != nil && m->path != nil)
+		plnum++;
+}
+
+static void
 readplist(void)
 {
 	Meta *m;
@@ -699,8 +755,11 @@
 	plraw = s;
 	plrawsize = sz;
 	plraw[plrawsize-1] = 0;
-	if(sz < 4 || s[0] != '#' || s[1] != ' ' || !isdigit(s[2]) || (s = memchr(plraw, '\n', sz)) == nil)
-		sysfatal("invalid playlist");
+	if(sz < 4 || s[0] != '#' || s[1] != ' ' || !isdigit(s[2]) || (s = memchr(plraw, '\n', sz)) == nil){
+		readplistnew();
+		return;
+	}
+
 	s++; /* at the start of the first record */
 
 	plnum = atoi(plraw+2);
@@ -749,7 +808,6 @@
 				e[tagsz] = 0; /* '\n'→'\0' to mark the end of the tag value */
 				if(s[0] == Palbum) m->album = e;
 				else if(s[0] == Partist && m->numartist < Maxartist) m->artist[m->numartist++] = e;
-				else if(s[0] == Pdate) m->date = e;
 				else if(s[0] == Ptitle) m->title = e;
 				else if(s[0] == Pdate) m->date = e;
 				else if(s[0] == Ptrack) m->track = e;