diff options
author | Ian Romanick <ian.d.romanick@intel.com> | 2009-11-14 16:56:04 -0800 |
---|---|---|
committer | Ian Romanick <ian.d.romanick@intel.com> | 2009-11-14 16:56:04 -0800 |
commit | 7ae40f08d382499878b9a5175b53a05086fa993b (patch) | |
tree | 1ad6afbfa4f85433bdd67aabf64f7c7b55b4309e /src | |
parent | a44406cdfb1568b497ddf2176660f4fadf792085 (diff) |
Add function to load text file (for shaders) from disk
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/load_text.c | 143 |
2 files changed, 144 insertions, 1 deletions
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); +} |