From a68445f2b035b8f979bd6e885effc8c7106fb964 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 5 May 2002 11:59:14 +0000 Subject: Changed how indices are read from a playlist. We just can't read the full file into memory first, and then scan for newlines. The file might be very big. This version instead scans through the file. This could probably be further improved too, to not use this read-single-bytes approach. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@442 a1c6a512-1295-4272-9138-f99709370657 --- firmware/playlist.c | 56 +++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 32 deletions(-) (limited to 'firmware') diff --git a/firmware/playlist.c b/firmware/playlist.c index acea68cad6..16eab01cfe 100644 --- a/firmware/playlist.c +++ b/firmware/playlist.c @@ -24,7 +24,7 @@ #include #include "playlist.h" #include "debug.h" -#include "disk.h" +#include /* * load playlist info from disk @@ -59,18 +59,13 @@ int reload_playlist_info( playlist_info_t *playlist ) */ void load_playlist( playlist_info_t *playlist, const char *filename ) { - char *m3u_buf = NULL; - DEBUGF( "load_playlist( %s )\n", filename ); - /* read file */ - read_file_into_buffer( &m3u_buf, filename ); - /* store playlist filename */ strncpy( playlist->filename, filename, sizeof(playlist->filename) ); /* add track indices to playlist data structure */ - add_indices_to_playlist( m3u_buf, playlist ); + add_indices_to_playlist( playlist ); } /* @@ -88,46 +83,43 @@ void empty_playlist( playlist_info_t *playlist ) { } /* - * calculate track offsets within a playlist buffer + * calculate track offsets within a playlist file */ -void add_indices_to_playlist( char *buf, playlist_info_t *playlist ) +void add_indices_to_playlist( playlist_info_t *playlist ) { char *p; int i = 0; + unsigned char byte; + unsigned char lastbyte='\n'; + int nread; + + int fd; + + fd = open(playlist->filename, O_RDONLY); + if(-1 == fd) + return; /* failure */ + /*DEBUGF( "add_indices_to_playlist()\n" ); */ - p = buf; + p = &byte; /* loop thru buffer, store index whenever we get a new line */ - while( p[i] != '\0' ) + while((nread = read(fd, &byte, 1)) == 1) { /* move thru (any) newlines */ - while( ( p[i] != '\0' ) && ( ( p[i] == '\n' ) || ( p[i] == '\r' ) ) ) - { - i++; - } + if(( byte != '\n' ) && ( byte != '\r' ) && + ((lastbyte == '\n') || (lastbyte == '\r'))) + /* we're now at the start of a new track filename. store index */ + extend_indices( playlist, i ); - /* did we get to the end of the buffer? */ - - if( p[i] == '\0' ) - { - break; - } - - /* we're now at the start of a new track filename. store index */ - - extend_indices( playlist, i ); - - /* we're now inside a track name. move to next newline */ - - while( ( p[i] != '\0' ) && ( ( p[i] != '\n' ) && ( p[i] != '\r' ) ) ) - { - i++; - } + lastbyte = byte; + i++; } + + close(fd); } /* -- cgit v1.2.3