changeset 527:fe8b41abd923

Now everything necessary should be using the VFS functions.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 20 Feb 2007 18:09:04 +0000
parents fa95d942f226
children 0170ab0c98e6
files src/xs_sidplay.h src/xs_sidplay1.cc src/xs_sidplay2.cc src/xs_support.c src/xs_support.h
diffstat 5 files changed, 120 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/xs_sidplay.h	Tue Feb 20 16:53:05 2007 +0000
+++ b/src/xs_sidplay.h	Tue Feb 20 18:09:04 2007 +0000
@@ -25,19 +25,27 @@
 /* This function gets most of the information, though we do miss some
  * (those variables that are only set by libSIDPlay when tune is initialized).
  * Rest of the information is acquired in TFUNCTION2()
- *
- * NOTICE! FIXME?! It might be faster to use a SID-parser of our own here,
- * thought it might make us bit incompatible ...
  */
 t_xs_tuneinfo *TFUNCTION(gchar *sidFilename)
 {
 	t_xs_tuneinfo *pResult;
 	TTUNEINFO myInfo;
 	TTUNE *myTune;
+	guint8 *buf = NULL;
+	size_t bufSize = 0;
 
+	/* Load file */
+	if (!sidFilename) return NULL;
+
+	if (xs_fload_buffer(sidFilename, &buf, &bufSize) != 0)
+		return NULL;
+	
 	/* Check if the tune exists and is readable */
-	if ((myTune = new TTUNE(sidFilename)) == NULL)
+	if ((myTune = new TTUNE(buf, bufSize)) == NULL) {
+		g_free(buf);
 		return NULL;
+	}
+	g_free(buf);
 
 	if (!myTune->getStatus()) {
 		delete myTune;
--- a/src/xs_sidplay1.cc	Tue Feb 20 16:53:05 2007 +0000
+++ b/src/xs_sidplay1.cc	Tue Feb 20 18:09:04 2007 +0000
@@ -41,6 +41,8 @@
 	emuEngine *currEng;
 	emuConfig currConfig;
 	sidTune *currTune;
+	guint8 *buf;
+	size_t bufSize;
 } t_xs_sidplay1;
 
 
@@ -91,14 +93,14 @@
 	/* Initialize engine */
 	myEngine->currEng = new emuEngine();
 	if (!myEngine->currEng) {
-		xs_error(_("Could not initialize libSIDPlay1 emulation engine.\n"));
+		xs_error(_("[SIDPlay1] Could not initialize emulation engine.\n"));
 		g_free(myEngine);
 		return FALSE;
 	}
 
 	/* Verify endianess */
 	if (!myEngine->currEng->verifyEndianess()) {
-		xs_error(_("Endianess verification failed.\n"));
+		xs_error(_("[SIDPlay1] Endianess verification failed.\n"));
 		delete myEngine->currEng;
 		g_free(myEngine);
 		return FALSE;
@@ -233,10 +235,17 @@
 
 	/* Now set the emulator configuration */
 	if (!myEngine->currEng->setConfig(myEngine->currConfig)) {
-		xs_error(_("Emulator engine configuration failed!\n"));
+		xs_error(_("[SIDPlay1] Emulator engine configuration failed!\n"));
 		return FALSE;
 	}
-
+	
+	/* Create sidtune object */
+	myEngine->currTune = new sidTune(0);
+	if (!myEngine->currTune) {
+		xs_error(_("[SIDPlay1] Could not initialize SIDTune object.\n"));
+		return FALSE;
+	}
+	
 	return TRUE;
 }
 
@@ -256,6 +265,13 @@
 		myEngine->currEng = NULL;
 	}
 
+	if (myEngine->currTune) {
+		delete myEngine->currTune;
+		myEngine->currTune = NULL;
+	}
+
+	xs_sidplay1_delete(myStatus);
+	
 	g_free(myEngine);
 	myStatus->sidEngine = NULL;
 }
@@ -272,15 +288,17 @@
 	if (!myEngine) return FALSE;
 
 	if (!myEngine->currTune) {
-		xs_error(_("SID-tune struct pointer was NULL. This should not happen, report to XMMS-SID author.\n"));
+		xs_error(_("[SIDPlay1] SID-tune struct pointer was NULL. This should not happen, report to XMMS-SID author.\n"));
 		return FALSE;
 	}
 
 	if (!myEngine->currTune->getStatus()) {
-		xs_error(_("SID-tune status check failed. This should not happen, report to XMMS-SID author.\n"));
+		xs_error(_("[SIDPlay1] SID-tune status check failed. This should not happen, report to XMMS-SID author.\n"));
 		return FALSE;
 	}
 
+	myStatus->isInitialized = TRUE;
+
 	return sidEmuInitializeSong(*myEngine->currEng, *myEngine->currTune, myStatus->currSong);
 }
 
@@ -306,19 +324,20 @@
 gboolean xs_sidplay1_load(t_xs_status * myStatus, gchar * pcFilename)
 {
 	t_xs_sidplay1 *myEngine;
-	sidTune *newTune;
 	assert(myStatus);
+	myStatus->isInitialized = FALSE;
 
 	myEngine = (t_xs_sidplay1 *) myStatus->sidEngine;
 	if (!myEngine) return FALSE;
 
-	/* Try to load the file/tune */
+	/* Try to get the tune */
 	if (!pcFilename) return FALSE;
-
-	newTune = new sidTune(pcFilename);
-	if (!newTune) return FALSE;
-
-	myEngine->currTune = newTune;
+	
+	if (xs_fload_buffer(pcFilename, &(myEngine->buf), &(myEngine->bufSize)) != 0)
+		return FALSE;
+	
+	if (!myEngine->currTune->load(myEngine->buf, myEngine->bufSize))
+		return FALSE;
 
 	return TRUE;
 }
@@ -333,11 +352,10 @@
 
 	myEngine = (t_xs_sidplay1 *) myStatus->sidEngine;
 	if (!myEngine) return;
-
-	if (myEngine->currTune) {
-		delete myEngine->currTune;
-		myEngine->currTune = NULL;
-	}
+	
+	g_free(myEngine->buf);
+	myEngine->buf = NULL;
+	myEngine->bufSize = 0;
 }
 
 
--- a/src/xs_sidplay2.cc	Tue Feb 20 16:53:05 2007 +0000
+++ b/src/xs_sidplay2.cc	Tue Feb 20 18:09:04 2007 +0000
@@ -43,6 +43,8 @@
 	sidbuilder *currBuilder;
 	sid2_config_t currConfig;
 	SidTune *currTune;
+	guint8 *buf;
+	size_t bufSize;
 } t_xs_sidplay2;
 
 
@@ -411,16 +413,18 @@
 {
 	t_xs_sidplay2 *myEngine;
 	assert(myStatus);
+	myStatus->isInitialized = FALSE;
 
 	myEngine = (t_xs_sidplay2 *) myStatus->sidEngine;
 	if (!myEngine) return FALSE;
-	
-	myStatus->isInitialized = FALSE;
 
 	/* Try to get the tune */
 	if (!pcFilename) return FALSE;
 
-	if (!myEngine->currTune->load(pcFilename))
+	if (xs_fload_buffer(pcFilename, &(myEngine->buf), &(myEngine->bufSize)) != 0)
+		return FALSE;
+	
+	if (!myEngine->currTune->read(myEngine->buf, myEngine->bufSize))
 		return FALSE;
 
 	return TRUE;
@@ -431,11 +435,15 @@
  */
 void xs_sidplay2_delete(t_xs_status * myStatus)
 {
+	t_xs_sidplay2 *myEngine;
 	assert(myStatus);
 
-	/* With the current scheme of handling sidtune-loading,
-	 * we don't do anything here.
-	 */
+	myEngine = (t_xs_sidplay2 *) myStatus->sidEngine;
+	if (!myEngine) return;
+	
+	g_free(myEngine->buf);
+	myEngine->buf = NULL;
+	myEngine->bufSize = 0;
 }
 
 
--- a/src/xs_support.c	Tue Feb 20 16:53:05 2007 +0000
+++ b/src/xs_support.c	Tue Feb 20 18:09:04 2007 +0000
@@ -67,6 +67,12 @@
 {
 	return ftell(f);
 }
+
+
+gint xs_fseek(t_xs_file *f, glong o, gint w)
+{
+	return fseek(f, o, w);
+}
 #endif
 
 
@@ -85,6 +91,57 @@
 }
 
 
+/* Load a file to a buffer, return 0 on success, negative value on error
+ */
+gint xs_fload_buffer(gchar *pcFilename, guint8 **buf, size_t *bufSize)
+{
+	t_xs_file *f;
+	glong seekPos;
+	
+	/* Open file, get file size */
+	if ((f = xs_fopen(pcFilename, "rb")) == NULL)
+		return -1;
+
+	xs_fseek(f, 0, SEEK_END);
+	seekPos = xs_ftell(f);
+	
+	if (seekPos > 0) {
+		size_t readSize = seekPos;
+		if (readSize >= *bufSize || *buf == NULL) {
+			/* Only re-allocate if the required size > current */
+			if (*buf != NULL) {
+				g_free(*buf);
+				*buf = NULL;
+			}
+	
+			*bufSize = seekPos;
+			
+			*buf = (guint8 *) g_malloc(*bufSize * sizeof(guint8));
+			if (*buf == NULL) {
+				xs_fclose(f);
+				return -2;
+			}
+		}
+		
+		/* Read data */	
+		xs_fseek(f, 0, SEEK_SET);
+		readSize = xs_fread(*buf, sizeof(guint8), *bufSize, f);
+		xs_fclose(f);
+		
+		if (readSize != *bufSize)
+			return -3;
+		else
+			return 0;
+	} else {
+		xs_fclose(f);
+		return -4;
+	}
+}
+
+
+
+
+
 /* Copy a string
  */
 gchar *xs_strncpy(gchar *pDest, gchar *pSource, size_t n)
--- a/src/xs_support.h	Tue Feb 20 16:53:05 2007 +0000
+++ b/src/xs_support.h	Tue Feb 20 18:09:04 2007 +0000
@@ -59,6 +59,7 @@
 #endif
 guint16 xs_fread_be16(t_xs_file *);
 guint32 xs_fread_be32(t_xs_file *);
+gint	xs_fload_buffer(gchar *, guint8 **, size_t *);
 
 
 /* Misc functions