Tizen 2.0 Release
[profile/ivi/osmesa.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 static int
87 os_stdc_stream_vprintf (struct os_stream* _stream, const char *format, va_list ap)
88 {
89    struct os_stdc_stream *stream = os_stdc_stream(_stream);
90
91    return vfprintf(stream->file, format, ap);
92 }
93
94
95 struct os_stream *
96 os_file_stream_create(const char *filename)
97 {
98    struct os_stdc_stream *stream;
99
100    stream = (struct os_stdc_stream *)calloc(1, sizeof(*stream));
101    if(!stream)
102       goto no_stream;
103
104    stream->base.close = &os_stdc_stream_close;
105    stream->base.write = &os_stdc_stream_write;
106    stream->base.flush = &os_stdc_stream_flush;
107    stream->base.vprintf = &os_stdc_stream_vprintf;
108
109    stream->file = fopen(filename, "wb");
110    if(!stream->file)
111       goto no_file;
112
113    return &stream->base;
114
115 no_file:
116    free(stream);
117 no_stream:
118    return NULL;
119 }
120
121
122 #endif