draw: Add assert to check input of memcpy.
[mesa.git] / src / gallium / auxiliary / os / os_stream_stdc.c
1 /**************************************************************************
2 *
3 * Copyright 2008-2010 VMware, Inc.
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 VMWARE 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_UNIX) || defined(PIPE_SUBSYSTEM_WINDOWS_USER)
36
37 #include <stdlib.h>
38 #include <stdio.h>
39
40 #include "os_stream.h"
41
42
43 struct os_stdc_stream
44 {
45 struct os_stream base;
46
47 FILE *file;
48 };
49
50
51 static INLINE struct os_stdc_stream *
52 os_stdc_stream(struct os_stream *stream)
53 {
54 return (struct os_stdc_stream *)stream;
55 }
56
57
58 static void
59 os_stdc_stream_close(struct os_stream *_stream)
60 {
61 struct os_stdc_stream *stream = os_stdc_stream(_stream);
62
63 fclose(stream->file);
64
65 free(stream);
66 }
67
68
69 static boolean
70 os_stdc_stream_write(struct os_stream *_stream, const void *data, size_t size)
71 {
72 struct os_stdc_stream *stream = os_stdc_stream(_stream);
73
74 return fwrite(data, size, 1, stream->file) == size ? TRUE : FALSE;
75 }
76
77
78 static void
79 os_stdc_stream_flush(struct os_stream *_stream)
80 {
81 struct os_stdc_stream *stream = os_stdc_stream(_stream);
82
83 fflush(stream->file);
84 }
85
86
87 struct os_stream *
88 os_file_stream_create(const char *filename)
89 {
90 struct os_stdc_stream *stream;
91
92 stream = (struct os_stdc_stream *)calloc(1, sizeof(*stream));
93 if(!stream)
94 goto no_stream;
95
96 stream->base.close = &os_stdc_stream_close;
97 stream->base.write = &os_stdc_stream_write;
98 stream->base.flush = &os_stdc_stream_flush;
99
100 stream->file = fopen(filename, "w");
101 if(!stream->file)
102 goto no_file;
103
104 return &stream->base;
105
106 no_file:
107 free(stream);
108 no_stream:
109 return NULL;
110 }
111
112
113 #endif