Add more structure constructor tests.
[piglit/hramrach.git] / tests / util / shader-load.c
blobd0eaf76afff6dda161fd0b516ae2cd1abf18a815
1 /*
2 * Copyright © 2007, 2008 Ian D. Romanick
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "config.h"
26 #if defined(HAVE_FCNTL_H) && defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_UNISTD_H)
27 # include <sys/types.h>
28 # include <sys/stat.h>
29 # include <fcntl.h>
30 # include <unistd.h>
31 #else
32 # define USE_STDIO
34 # ifdef _WIN32
35 # define WIN32_LEAN_AND_MEAN
36 # include <windows.h>
37 # endif
38 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
44 char *piglit_load_text_file(const char *file_name, unsigned *size)
46 char *text = NULL;
48 #if defined(USE_STDIO)
49 FILE *fp;
51 # ifdef HAVE_FOPEN_S
52 errno_t err = fopen_s(&fp, file_name, "r");
54 if (err || (fp == NULL)) {
55 return NULL;
57 # else
58 fp = fopen(file_name, "r");
59 if (fp == NULL) {
60 return NULL;
62 # endif
64 if (fseek(fp, 0, SEEK_END) == 0) {
65 size_t len = (size_t) ftell(fp);
66 rewind(fp);
68 text = malloc(len + 1);
69 if (text != NULL) {
70 size_t total_read = 0;
72 do {
73 size_t bytes = fread(text + total_read, 1,
74 len - total_read, fp);
76 total_read += bytes;
77 if (feof(fp)) {
78 break;
81 if (ferror(fp)) {
82 free(text);
83 text = NULL;
84 break;
86 } while (total_read < len);
88 if (text != NULL) {
89 text[total_read] = '\0';
92 if (size != NULL) {
93 *size = total_read;
98 fclose(fp);
99 return text;
100 #else
101 struct stat st;
102 int fd = open(file_name, O_RDONLY);
104 if (fd < 0) {
105 return NULL;
108 if (fstat(fd, & st) == 0) {
109 ssize_t total_read = 0;
111 text = malloc(st.st_size + 1);
112 if (text != NULL) {
113 do {
114 ssize_t bytes = read(fd, text + total_read,
115 st.st_size - total_read);
116 if (bytes < 0) {
117 free(text);
118 text = NULL;
119 break;
122 if (bytes == 0) {
123 break;
126 total_read += bytes;
127 } while (total_read < st.st_size);
129 text[total_read] = '\0';
130 if (size != NULL) {
131 *size = total_read;
136 close(fd);
138 return text;
139 #endif