Merge branch 'master' into opengl-es-v2
[mesa.git] / src / gallium / auxiliary / util / u_stream_stdc.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Stream implementation based on the Standard C Library.
31 */
32
33 #include "pipe/p_config.h"
34
35 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE)
36
37 #include <stdio.h>
38
39 #include "util/u_memory.h"
40
41 #include "u_stream.h"
42
43
44 struct util_stream
45 {
46 FILE *file;
47 };
48
49
50 struct util_stream *
51 util_stream_create(const char *filename, size_t max_size)
52 {
53 struct util_stream *stream;
54
55 (void)max_size;
56
57 stream = CALLOC_STRUCT(util_stream);
58 if(!stream)
59 goto error1;
60
61 stream->file = fopen(filename, "w");
62 if(!stream->file)
63 goto error2;
64
65 return stream;
66
67 error2:
68 FREE(stream);
69 error1:
70 return NULL;
71 }
72
73
74 boolean
75 util_stream_write(struct util_stream *stream, const void *data, size_t size)
76 {
77 if(!stream)
78 return FALSE;
79
80 return fwrite(data, size, 1, stream->file) == size ? TRUE : FALSE;
81 }
82
83
84 void
85 util_stream_flush(struct util_stream *stream)
86 {
87 if(!stream)
88 return;
89
90 fflush(stream->file);
91 }
92
93
94 void
95 util_stream_close(struct util_stream *stream)
96 {
97 if(!stream)
98 return;
99
100 fclose(stream->file);
101
102 FREE(stream);
103 }
104
105
106 #endif