• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Stream Library
3  * ---------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Stream wrapper for deFile
22  *//*--------------------------------------------------------------------*/
23 #include "deFileStream.h"
24 
25 #include <stdlib.h>
26 
27 typedef struct FileStream_s
28 {
29 	deFile*			file;
30 	deStreamStatus	status;
31 	const char*		error;
32 } FileStream;
33 
fileIOStream_read(deStreamData * stream,void * buf,deInt32 bufSize,deInt32 * numRead)34 static deStreamResult fileIOStream_read (deStreamData* stream, void* buf, deInt32 bufSize, deInt32* numRead)
35 {
36 	deInt64 _numRead = 0;
37 	FileStream* fileStream = (FileStream*)stream;
38 
39 
40 	deFileResult result = deFile_read(fileStream->file, buf, bufSize, &_numRead);
41 	*numRead = (deInt32)_numRead;
42 
43 	switch (result)
44 	{
45 		case  DE_FILERESULT_SUCCESS:
46 			return DE_STREAMRESULT_SUCCESS;
47 
48 		case DE_FILERESULT_ERROR:
49 			fileStream->error	= "deFile: DE_FILERESULT_ERROR";
50 			fileStream->status	= DE_STREAMSTATUS_ERROR;
51 			return DE_STREAMRESULT_ERROR;
52 
53 		case DE_FILERESULT_END_OF_FILE:
54 			return DE_STREAMRESULT_END_OF_STREAM;
55 
56 		default:
57 			fileStream->error	= "Uknown: DE_FILERESULT";
58 			fileStream->status	= DE_STREAMSTATUS_ERROR;
59 			return DE_STREAMRESULT_ERROR;
60 	}
61 }
62 
fileIOStream_write(deStreamData * stream,const void * buf,deInt32 bufSize,deInt32 * numWritten)63 static deStreamResult fileIOStream_write (deStreamData* stream, const void* buf, deInt32 bufSize, deInt32* numWritten)
64 {
65 	deInt64	_numWritten = 0;
66 	FileStream* fileStream = (FileStream*)stream;
67 
68 	deFileResult result = deFile_write(fileStream->file, buf, bufSize, &_numWritten);
69 	*numWritten = (deInt32)_numWritten;
70 
71 	switch (result)
72 	{
73 		case  DE_FILERESULT_SUCCESS:
74 			return DE_STREAMRESULT_SUCCESS;
75 
76 		case DE_FILERESULT_ERROR:
77 			fileStream->error	= "deFile: DE_FILERESULT_ERROR";
78 			fileStream->status	= DE_STREAMSTATUS_ERROR;
79 			return DE_STREAMRESULT_ERROR;
80 
81 		case DE_FILERESULT_END_OF_FILE:
82 			return DE_STREAMRESULT_END_OF_STREAM;
83 
84 		default:
85 			fileStream->error	= "Uknown: DE_FILERESULT";
86 			fileStream->status	= DE_STREAMSTATUS_ERROR;
87 			return DE_STREAMRESULT_ERROR;
88 	}
89 }
90 
fileIOStream_getError(deStreamData * stream)91 static const char* fileIOStream_getError (deStreamData* stream)
92 {
93 	FileStream* fileStream = (FileStream*)stream;
94 	/* \note [mika] There is only error reporting through return value in deFile */
95 	return fileStream->error;
96 }
97 
fileIOStream_flush(deStreamData * stream)98 static deStreamResult fileIOStream_flush (deStreamData* stream)
99 {
100 	/* \todo mika deFile doesn't have flush, how should this be handled? */
101 	DE_UNREF(stream);
102 
103 	return DE_STREAMRESULT_SUCCESS;
104 }
105 
fileIOStream_deinit(deStreamData * stream)106 static deStreamResult fileIOStream_deinit (deStreamData* stream)
107 {
108 	FileStream* fileStream = (FileStream*)stream;
109 
110 	deFile_destroy(fileStream->file);
111 
112 	free(fileStream);
113 
114 	return DE_STREAMRESULT_SUCCESS;
115 }
116 
fileIOStrem_getStatus(deStreamData * stream)117 static deStreamStatus fileIOStrem_getStatus (deStreamData* stream)
118 {
119 	FileStream* fileStream = (FileStream*)stream;
120 	return fileStream->status;
121 }
122 
123 static const deIOStreamVFTable fileIOStreamVFTable = {
124 	fileIOStream_read,
125 	fileIOStream_write,
126 	fileIOStream_getError,
127 	fileIOStream_flush,
128 	fileIOStream_deinit,
129 	fileIOStrem_getStatus
130 };
131 
132 static const deIOStreamVFTable fileInStreamVFTable = {
133 	fileIOStream_read,
134 	DE_NULL,
135 	fileIOStream_getError,
136 	DE_NULL,
137 	fileIOStream_deinit,
138 	fileIOStrem_getStatus
139 };
140 
141 static const deIOStreamVFTable fileOutStreamVFTable = {
142 	DE_NULL,
143 	fileIOStream_write,
144 	fileIOStream_getError,
145 	fileIOStream_flush,
146 	fileIOStream_deinit,
147 	fileIOStrem_getStatus
148 };
149 
fileIOStream_init(deIOStream * stream,const char * filename,deFileMode mode)150 void fileIOStream_init (deIOStream* stream, const char* filename, deFileMode mode)
151 {
152 	FileStream* fileStream = DE_NULL;
153 
154 	DE_ASSERT(stream);
155 
156 	fileStream = malloc(sizeof(FileStream));
157 
158 	/* \note mika Check that file is readable and writeable, currently not supported by deFile */
159 	stream->vfTable		= &fileIOStreamVFTable;
160 	stream->streamData	= (deStreamData*)fileStream;
161 
162 	fileStream->file	= deFile_create(filename, mode);
163 	fileStream->status	= DE_STREAMSTATUS_GOOD;
164 	fileStream->error	= DE_NULL;
165 
166 	if (!fileStream->file)
167 		fileStream->status = DE_STREAMSTATUS_ERROR;
168 }
169 
deFileInStream_init(deInStream * stream,const char * filename,deFileMode mode)170 void deFileInStream_init (deInStream* stream, const char* filename, deFileMode mode)
171 {
172 	FileStream* fileStream = DE_NULL;
173 
174 	DE_ASSERT(stream);
175 
176 	fileStream = malloc(sizeof(FileStream));
177 
178 	/* \note mika Check that file is readable, currently not supported by deFile */
179 	stream->ioStream.vfTable		= &fileInStreamVFTable;
180 	stream->ioStream.streamData		= (deStreamData*)fileStream;
181 
182 	fileStream->file	= deFile_create(filename, mode);
183 	fileStream->status	= DE_STREAMSTATUS_GOOD;
184 	fileStream->error	= DE_NULL;
185 
186 	if (!fileStream->file)
187 		fileStream->status = DE_STREAMSTATUS_ERROR;
188 }
189 
deFileOutStream_init(deOutStream * stream,const char * filename,deFileMode mode)190 void deFileOutStream_init (deOutStream* stream, const char* filename, deFileMode mode)
191 {
192 	FileStream* fileStream = DE_NULL;
193 
194 	DE_ASSERT(stream);
195 
196 	fileStream = malloc(sizeof(FileStream));
197 
198 	/* \note mika Check that file is writeable, currently not supported by deFile */
199 	stream->ioStream.vfTable		= &fileOutStreamVFTable;
200 	stream->ioStream.streamData		= (deStreamData*)fileStream;
201 
202 	fileStream->file	= deFile_create(filename, mode);
203 	fileStream->status	= DE_STREAMSTATUS_GOOD;
204 	fileStream->error	= DE_NULL;
205 
206 	if (!fileStream->file)
207 		fileStream->status = DE_STREAMSTATUS_ERROR;
208 }
209