From 3314377b910b89da979392f3e90342f2097ee157 Mon Sep 17 00:00:00 2001 From: Nicolas Pennequin Date: Tue, 7 Aug 2007 23:27:59 +0200 Subject: [PATCH] Make the (very primitive) metadata handling use bufalloc(). Added read_metadata() which makes a few changes to the metadata that we get from the files. The resulting buffer is then passed to bufalloc, which stores it in the main buffer. print_metadata() becomes much simpler thanks to this. This is a good example of how bufalloc is meant to be used. Tested successfully on target and sim. --- testplugin.c | 60 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/testplugin.c b/testplugin.c index 7168f38..e18523f 100644 --- a/testplugin.c +++ b/testplugin.c @@ -899,27 +899,38 @@ void print_progress(size_t amount, int file, int numfiles) void print_metadata(int handle_id) { ssize_t ret; - char *artist, *title, *newline; - char art[50], ttl[50]; - int artist_len, title_len; + char *artist, *title; unsigned char *buf; ret = bufgetdata(handle_id, 0, &buf); + if (ret < 0) + return; + artist = buf; - newline = rb->strchr(artist, '\n'); - artist_len = newline - artist; - rb->strncpy(art, artist, artist_len); - art[artist_len] = 0; - - title = newline + 1; - newline = rb->strchr(title, '\n'); - title_len = newline - title; - rb->strncpy(ttl, title, title_len); - ttl[title_len] = 0; - - rb->lcd_puts(0, 3, art); - rb->lcd_puts(0, 4, ttl); + title = artist + rb->strlen(artist) + 1; + + rb->lcd_puts(0, 3, artist); + rb->lcd_puts(0, 4, title); +} + +int read_metadata(char *filename, char *buf, int bufsize) +{ + int ret; + char *newline; + int fd = rb->open(filename, O_RDONLY); + if (fd < 0) + return -1; + + ret = rb->read(fd, buf, bufsize); + newline = rb->strchr(buf, '\n'); + *newline = '\0'; + newline = rb->strchr(newline + 1, '\n'); + *newline = '\0'; + + rb->close(fd); + + return ret; } bool buffer_init(void) @@ -1047,12 +1058,23 @@ void codec_thread(void) void bufopen_thread(void) { int idx = 0, ret; + char filename[MAX_PATH]; char buf[MAX_PATH]; + int meta_len; + while (idx < num_files) { - /* open the metadata file */ - rb->snprintf(buf, MAX_PATH, "/meta%s.txt", files[idx]); - meta_handles[idx] = bufopen(buf, 0, TYPE_ID3); + if (meta_handles[idx] <= 0) + { + /* read the metadata file */ + rb->snprintf(filename, MAX_PATH, "/meta%s.txt", files[idx]); + meta_len = read_metadata(filename, buf, MAX_PATH); + + /* copy the metadata to a new handle */ + if (meta_len > 0) + meta_handles[idx] = bufalloc(buf, meta_len, TYPE_ID3); + } + if (meta_handles[idx] > 0) { /* open the audio file */ -- 2.11.4.GIT