summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Romanick <ian.d.romanick@intel.com>2009-11-14 16:56:04 -0800
committerIan Romanick <ian.d.romanick@intel.com>2009-11-14 16:56:04 -0800
commit7ae40f08d382499878b9a5175b53a05086fa993b (patch)
tree1ad6afbfa4f85433bdd67aabf64f7c7b55b4309e
parenta44406cdfb1568b497ddf2176660f4fadf792085 (diff)
Add function to load text file (for shaders) from disk
-rw-r--r--configure.ac3
-rw-r--r--include/glu3.h25
-rw-r--r--src/Makefile.am2
-rw-r--r--src/load_text.c143
4 files changed, 171 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index 7059ce4..07b20e5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -19,7 +19,7 @@ AC_PROG_RANLIB
# Checks for libraries.
# Checks for header files.
-AC_CHECK_HEADERS([fcntl.h unistd.h])
+AC_CHECK_HEADERS([fcntl.h unistd.h sys/stat.h sys/types.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
@@ -30,6 +30,7 @@ AC_TYPE_UINT8_T
# Checks for library functions.
AC_HEADER_STDC
+AC_CHECK_FUNCS(fopen_s)
AC_ARG_ENABLE([debug],
[AS_HELP_STRING([--enable-debug],
diff --git a/include/glu3.h b/include/glu3.h
index 1c31c15..c8ac9dc 100644
--- a/include/glu3.h
+++ b/include/glu3.h
@@ -583,6 +583,31 @@ void gluTranspose4m(GLUmat4 *result, const GLUmat4 *m);
*/
extern const GLUmat4 gluIdentityMatrix;
+/**
+ * Load a text file from disk
+ *
+ * \param file_name Name of the file to be loaded
+ *
+ * Loads data from a named text file and returns a pointer to that data to the
+ * caller. This may be useful, for example, for loading shader code from flies
+ * on disk.
+ *
+ * The pointer returned by this function should later be released by calling
+ * \c gluUnloadTextFile.
+ *
+ * \note
+ * The data pointed to by the return value if this function really is
+ * constant. On some systems this function may be implemented by creating a
+ * read-only mapping of the file. Writes to such data will result in program
+ * termination.
+ */
+extern const GLchar *gluLoadTextFile(const char *file_name);
+
+/**
+ * Release data previously loaded with gluLoadTextFile.
+ */
+extern void gluUnloadTextFile(const GLchar *text);
+
#ifdef __cplusplus
};
#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index 974ffbd..9bd643c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,7 +24,7 @@ AM_CXXFLAGS=-I../include
AM_CFLAGS=-I../include
lib_LIBRARIES = libGLU3.a
-libGLU3_a_SOURCES = matrix.c
+libGLU3_a_SOURCES = matrix.c load_text.c
libGLU3includedir = ${includedir}
libGLU3include_HEADERS = ../include/glu3.h ../include/glu3_scalar.h
diff --git a/src/load_text.c b/src/load_text.c
new file mode 100644
index 0000000..c2a4c37
--- /dev/null
+++ b/src/load_text.c
@@ -0,0 +1,143 @@
+/*
+ * Copyright © 2007, 2008, 2009 Ian D. Romanick
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(HAVE_FCNTL_H) && defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_UNISTD_H)
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <fcntl.h>
+# include <unistd.h>
+#else
+# define USE_STDIO
+
+# ifdef _WIN32
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+# endif
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "glu3.h"
+
+
+const GLchar *
+gluLoadTextFile(const char *file_name)
+{
+ char *text = NULL;
+
+#if defined(USE_STDIO)
+
+# ifdef HAVE_FOPEN_S
+ FILE *fp;
+ errno_t err = fopen_s(&fp, file_name, "r");
+
+ if (err || (fp == NULL)) {
+ return NULL;
+ }
+# else
+ FILE *fp = fopen(file_name, "r");
+
+ if (fp == NULL) {
+ return NULL;
+ }
+# endif
+
+ if (fseek(fp, 0, SEEK_END) == 0) {
+ size_t len = (size_t) ftell(fp);
+ rewind(fp);
+
+ text = malloc(len + 1);
+ if (text != NULL) {
+ size_t total_read = 0;
+
+ do {
+ size_t bytes = fread(text + total_read, 1,
+ len - total_read, fp);
+
+ total_read += bytes;
+ if (feof(fp)) {
+ break;
+ }
+
+ if (ferror(fp)) {
+ free(text);
+ text = NULL;
+ break;
+ }
+ } while (total_read < len);
+
+ text[total_read] = '\0';
+ }
+ }
+
+ fclose(fp);
+ return (GLchar *) text;
+#else
+ struct stat st;
+ int fd = open(file_name, O_RDONLY);
+
+ if (fd < 0) {
+ return NULL;
+ }
+
+ if (fstat(fd, & st) == 0) {
+ ssize_t total_read = 0;
+
+ text = malloc(st.st_size + 1);
+ if (text != NULL) {
+ do {
+ ssize_t bytes = read(fd, text + total_read,
+ st.st_size - total_read);
+ if (bytes < 0) {
+ free(text);
+ text = NULL;
+ break;
+ }
+
+ if (bytes == 0) {
+ break;
+ }
+
+ total_read += bytes;
+ } while (total_read < st.st_size);
+
+ text[total_read] = '\0';
+ }
+ }
+
+ close(fd);
+
+ return (GLchar *) text;
+#endif
+}
+
+
+void
+gluUnloadTextFile(const GLchar *text)
+{
+ free((void *) text);
+}