shithub: zuke

Download patch

ref: 81916a6bba65225bf4ed83133ebb2081eb996dc5
parent: aaaaadd1005ce272f9257c2e3987653afefbafbb
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Feb 20 12:58:25 EST 2016

lower playlist size, separate common stuff into plist.h

--- a/LICENSE
+++ b/LICENSE
@@ -1,1 +1,1 @@
-Everything here is in the public domain.
+Public domain.
--- a/mkplist.c
+++ b/mkplist.c
@@ -1,33 +1,14 @@
 #include <u.h>
 #include <libc.h>
 #include <tags.h>
+#include "plist.h"
 
 enum
 {
-	Maxartist = 16, /* max artists for a track */
 	Maxname = 256+2, /* seems enough? */
 	Maxdepth = 16, /* max recursion depth */
 };
 
-typedef struct Meta Meta;
-
-struct Meta
-{
-	char *artist[Maxartist];
-	char *album;
-	char *title;
-	char *date;
-	char *track;
-	char *path;
-	int numartist;
-	int duration;
-	int samplerate;
-	int channels;
-	int imageoffset;
-	int imagesize;
-	int imagereader; /* non-zero if a special reader required */
-};
-
 static int fd;
 static Meta *curr;
 static Meta *all;
@@ -216,29 +197,29 @@
 	s = buf;
 
 	for(i = 0; i < m->numartist; i++)
-		s = seprint(s, buf+sizeof(buf), "artist %ld %s\n", strlen(m->artist[i]), m->artist[i]);
+		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Partist, strlen(m->artist[i]), m->artist[i]);
 	if(m->album != nil)
-		s = seprint(s, buf+sizeof(buf), "album %ld %s\n", strlen(m->album), m->album);
+		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Palbum, strlen(m->album), m->album);
 	if(m->title != nil)
-		s = seprint(s, buf+sizeof(buf), "title %ld %s\n", strlen(m->title), m->title);
+		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Ptitle, strlen(m->title), m->title);
 	if(m->date != nil)
-		s = seprint(s, buf+sizeof(buf), "date %ld %s\n", strlen(m->date), m->date);
+		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Pdate, strlen(m->date), m->date);
 	if(m->track != nil)
-		s = seprint(s, buf+sizeof(buf), "track %ld %s\n", strlen(m->track), m->track);
+		s = seprint(s, buf+sizeof(buf), "%c%ld %s\n", Ptrack, strlen(m->track), m->track);
 	if(m->duration > 0)
-		s = seprint(s, buf+sizeof(buf), "duration %d\n", m->duration);
+		s = seprint(s, buf+sizeof(buf), "%c %d\n", Pduration, m->duration);
 	if(m->samplerate > 0)
-		s = seprint(s, buf+sizeof(buf), "samplerate %d\n", m->samplerate);
+		s = seprint(s, buf+sizeof(buf), "%c %d\n", Psamplerate, m->samplerate);
 	if(m->channels > 0)
-		s = seprint(s, buf+sizeof(buf), "channels %d\n", m->channels);
+		s = seprint(s, buf+sizeof(buf), "%c %d\n", Pchannels, m->channels);
 	if(m->imagesize > 0)
-		s = seprint(s, buf+sizeof(buf), "image %d %d %d\n", m->imageoffset, m->imagesize, m->imagereader);
-	s = seprint(s, buf+sizeof(buf), "path %s\n", m->path);
+		s = seprint(s, buf+sizeof(buf), "%c %d %d %d\n", Pimage, m->imageoffset, m->imagesize, m->imagereader);
+	s = seprint(s, buf+sizeof(buf), "%c %s\n", Ppath, m->path);
 
 	if(buf[sizeof(buf)-2] != 0)
 		sysfatal("buffer size of %d bytes was a bad choice", sizeof(buf));
 
-	print("# %d %d\n%s", index, (int)(s-buf), buf);
+	print("%c %d %d\n%s", Precord, index, (int)(s-buf), buf);
 }
 
 static int
@@ -289,10 +270,14 @@
 		dir = strdup(argv[i]);
 		scan(&dir, 0);
 	}
-	if(numall > 0){
-		qsort(all, numall, sizeof(Meta), cmpmeta);
-		for(i = 0; i < numall; i++)
-			printmeta(&all[i], i);
+	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);
 	}
 	fprint(2, "found %d tagged tracks\n", numall);
 }
--- /dev/null
+++ b/plist.h
@@ -1,0 +1,45 @@
+/* Playlist begins with "# x\n" where x is the total number of records.
+ * Each record begins with "# x y\n" where x is record index, y is its size in bytes.
+ * Records are sorted according to mkplist.c:/^cmpmeta function.
+ * This makes it somewhat easy to just load the whole playlist into memory once,
+ * map all (Meta*)->... fields to it, saving on memory allocations, and using the same
+ * data to provide poor's man full text searching.
+ * Encoding: mkplist.c:/^printmeta/.
+ * Decoding: zuke.c:/^readplist/.
+ */
+enum
+{
+	Precord='#',
+
+	Palbum=			'a',
+	Partist=		'A',
+	Pchannels=		'c',
+	Pdate=			'd',
+	Pduration=		'D',
+	Pimage=			'i',
+	Ptitle=			't',
+	Ptrack=			'T',
+	Ppath=			'p',
+	Psamplerate=	's',
+
+	Maxartist=16, /* max artists for a track */
+};
+
+typedef struct Meta Meta;
+
+struct Meta
+{
+	char *artist[Maxartist];
+	char *album;
+	char *title;
+	char *date;
+	char *track;
+	char *path;
+	int numartist;
+	int duration;
+	int samplerate;
+	int channels;
+	int imageoffset;
+	int imagesize;
+	int imagereader; /* non-zero if a special reader required */
+};