diff options
author | Blaž Tomažič <blaz.tomazic@gmail.com> | 2012-06-21 20:09:37 +0200 |
---|---|---|
committer | Blaž Tomažič <blaz.tomazic@gmail.com> | 2012-06-28 03:55:01 +0200 |
commit | 34ef043d488ce58b68971422b18f042e674f149d (patch) | |
tree | ad4b51d6624324a26e6c0f43d6f9b5e1f0ced8cf | |
parent | ece51077e922aa28cd867cfd180250394e809275 (diff) |
util: Move file reading function to piglit-util
Move function for reading files from shader-load.c to piglit-util.c.
Delete shader-load.c because it had no other functionality.
Signed-off-by: Blaž Tomažič <blaz.tomazic@gmail.com>
Reviewed-by: Chad Versace <chad.versace@linux.intel.com>
-rw-r--r-- | tests/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/util/piglit-util.c | 113 | ||||
-rw-r--r-- | tests/util/shader-load.c | 146 |
3 files changed, 113 insertions, 147 deletions
diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt index 8d2587102..152b2d11e 100644 --- a/tests/util/CMakeLists.txt +++ b/tests/util/CMakeLists.txt @@ -16,7 +16,6 @@ set(UTIL_SOURCES piglit-util.c piglit-util-gl-common.c piglit-util-gl-enum.c - shader-load.c piglit-framework.c piglit-framework-fbo.c piglit-framework-glut.c diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c index 705f05511..f6c2ba02e 100644 --- a/tests/util/piglit-util.c +++ b/tests/util/piglit-util.c @@ -22,6 +22,7 @@ */ #if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN #include <windows.h> #endif @@ -39,6 +40,15 @@ #define USE_SETRLIMIT #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 +#endif + #include "piglit-util.h" @@ -221,3 +231,106 @@ piglit_merge_result(enum piglit_result *all, enum piglit_result subtest) } } +char *piglit_load_text_file(const char *file_name, unsigned *size) +{ + char *text = NULL; + +#if defined(USE_STDIO) + FILE *fp; + +# ifdef HAVE_FOPEN_S + errno_t err; + + if (file_name == NULL) { + return NULL; + } + + err = fopen_s(&fp, file_name, "r"); + + if (err || (fp == NULL)) { + return NULL; + } +# else + 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); + + if (text != NULL) { + text[total_read] = '\0'; + } + + if (size != NULL) { + *size = total_read; + } + } + } + + fclose(fp); + return 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'; + if (size != NULL) { + *size = total_read; + } + } + } + + close(fd); + + return text; +#endif +} diff --git a/tests/util/shader-load.c b/tests/util/shader-load.c deleted file mode 100644 index a6156e9c5..000000000 --- a/tests/util/shader-load.c +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright © 2007, 2008 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. - */ -#include "config.h" - -#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> - - -char *piglit_load_text_file(const char *file_name, unsigned *size) -{ - char *text = NULL; - -#if defined(USE_STDIO) - FILE *fp; - -# ifdef HAVE_FOPEN_S - errno_t err; - - if (file_name == NULL) { - return NULL; - } - - err = fopen_s(&fp, file_name, "r"); - - if (err || (fp == NULL)) { - return NULL; - } -# else - 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); - - if (text != NULL) { - text[total_read] = '\0'; - } - - if (size != NULL) { - *size = total_read; - } - } - } - - fclose(fp); - return 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'; - if (size != NULL) { - *size = total_read; - } - } - } - - close(fd); - - return text; -#endif -} |