diff src/xs_support.c @ 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 c192468eb8ce
children 3e4901a89f3d
line wrap: on
line diff
--- 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)