• 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 			break;
48 
49 		case DE_FILERESULT_ERROR:
50 			fileStream->error	= "deFile: DE_FILERESULT_ERROR";
51 			fileStream->status	= DE_STREAMSTATUS_ERROR;
52 			return DE_STREAMRESULT_ERROR;
53 			break;
54 
55 		case DE_FILERESULT_END_OF_FILE:
56 			return DE_STREAMRESULT_END_OF_STREAM;
57 			break;
58 
59 		default:
60 			fileStream->error	= "Uknown: DE_FILERESULT";
61 			fileStream->status	= DE_STREAMSTATUS_ERROR;
62 			return DE_STREAMRESULT_ERROR;
63 			break;
64 	};
65 }
66 
fileIOStream_write(deStreamData * stream,const void * buf,deInt32 bufSize,deInt32 * numWritten)67 static deStreamResult fileIOStream_write (deStreamData* stream, const void* buf, deInt32 bufSize, deInt32* numWritten)
68 {
69 	deInt64	_numWritten = 0;
70 	FileStream* fileStream = (FileStream*)stream;
71 
72 	deFileResult result = deFile_write(fileStream->file, buf, bufSize, &_numWritten);
73 	*numWritten = (deInt32)_numWritten;
74 
75 	switch (result)
76 	{
77 		case  DE_FILERESULT_SUCCESS:
78 			return DE_STREAMRESULT_SUCCESS;
79 			break;
80 
81 		case DE_FILERESULT_ERROR:
82 			fileStream->error	= "deFile: DE_FILERESULT_ERROR";
83 			fileStream->status	= DE_STREAMSTATUS_ERROR;
84 			return DE_STREAMRESULT_ERROR;
85 			break;
86 
87 		case DE_FILERESULT_END_OF_FILE:
88 			return DE_STREAMRESULT_END_OF_STREAM;
89 			break;
90 
91 		default:
92 			fileStream->error	= "Uknown: DE_FILERESULT";
93 			fileStream->status	= DE_STREAMSTATUS_ERROR;
94 			return DE_STREAMRESULT_ERROR;
95 			break;
96 	};
97 }
98 
fileIOStream_getError(deStreamData * stream)99 static const char* fileIOStream_getError (deStreamData* stream)
100 {
101 	FileStream* fileStream = (FileStream*)stream;
102 	/* \note [mika] There is only error reporting through return value in deFile */
103 	return fileStream->error;
104 }
105 
fileIOStream_flush(deStreamData * stream)106 static deStreamResult fileIOStream_flush (deStreamData* stream)
107 {
108 	/* \todo mika deFile doesn't have flush, how should this be handled? */
109 	DE_UNREF(stream);
110 
111 	return DE_STREAMRESULT_SUCCESS;
112 }
113 
fileIOStream_deinit(deStreamData * stream)114 static deStreamResult fileIOStream_deinit (deStreamData* stream)
115 {
116 	FileStream* fileStream = (FileStream*)stream;
117 
118 	deFile_destroy(fileStream->file);
119 
120 	free(fileStream);
121 
122 	return DE_STREAMRESULT_SUCCESS;
123 }
124 
fileIOStrem_getStatus(deStreamData * stream)125 static deStreamStatus fileIOStrem_getStatus (deStreamData* stream)
126 {
127 	FileStream* fileStream = (FileStream*)stream;
128 	return fileStream->status;
129 }
130 
131 static const deIOStreamVFTable fileIOStreamVFTable = {
132 	fileIOStream_read,
133 	fileIOStream_write,
134 	fileIOStream_getError,
135 	fileIOStream_flush,
136 	fileIOStream_deinit,
137 	fileIOStrem_getStatus
138 };
139 
140 static const deIOStreamVFTable fileInStreamVFTable = {
141 	fileIOStream_read,
142 	DE_NULL,
143 	fileIOStream_getError,
144 	DE_NULL,
145 	fileIOStream_deinit,
146 	fileIOStrem_getStatus
147 };
148 
149 static const deIOStreamVFTable fileOutStreamVFTable = {
150 	DE_NULL,
151 	fileIOStream_write,
152 	fileIOStream_getError,
153 	fileIOStream_flush,
154 	fileIOStream_deinit,
155 	fileIOStrem_getStatus
156 };
157 
fileIOStream_init(deIOStream * stream,const char * filename,deFileMode mode)158 void fileIOStream_init (deIOStream* stream, const char* filename, deFileMode mode)
159 {
160 	FileStream* fileStream = DE_NULL;
161 
162 	DE_ASSERT(stream);
163 
164 	fileStream = malloc(sizeof(FileStream));
165 
166 	/* \note mika Check that file is readable and writeable, currently not supported by deFile */
167 	stream->vfTable		= &fileIOStreamVFTable;
168 	stream->streamData	= (deStreamData*)fileStream;
169 
170 	fileStream->file	= deFile_create(filename, mode);
171 	fileStream->status	= DE_STREAMSTATUS_GOOD;
172 	fileStream->error	= DE_NULL;
173 
174 	if (!fileStream->file)
175 		fileStream->status = DE_STREAMSTATUS_ERROR;
176 }
177 
deFileInStream_init(deInStream * stream,const char * filename,deFileMode mode)178 void deFileInStream_init (deInStream* stream, const char* filename, deFileMode mode)
179 {
180 	FileStream* fileStream = DE_NULL;
181 
182 	DE_ASSERT(stream);
183 
184 	fileStream = malloc(sizeof(FileStream));
185 
186 	/* \note mika Check that file is readable, currently not supported by deFile */
187 	stream->ioStream.vfTable		= &fileInStreamVFTable;
188 	stream->ioStream.streamData		= (deStreamData*)fileStream;
189 
190 	fileStream->file	= deFile_create(filename, mode);
191 	fileStream->status	= DE_STREAMSTATUS_GOOD;
192 	fileStream->error	= DE_NULL;
193 
194 	if (!fileStream->file)
195 		fileStream->status = DE_STREAMSTATUS_ERROR;
196 }
197 
deFileOutStream_init(deOutStream * stream,const char * filename,deFileMode mode)198 void deFileOutStream_init (deOutStream* stream, const char* filename, deFileMode mode)
199 {
200 	FileStream* fileStream = DE_NULL;
201 
202 	DE_ASSERT(stream);
203 
204 	fileStream = malloc(sizeof(FileStream));
205 
206 	/* \note mika Check that file is writeable, currently not supported by deFile */
207 	stream->ioStream.vfTable		= &fileOutStreamVFTable;
208 	stream->ioStream.streamData		= (deStreamData*)fileStream;
209 
210 	fileStream->file	= deFile_create(filename, mode);
211 	fileStream->status	= DE_STREAMSTATUS_GOOD;
212 	fileStream->error	= DE_NULL;
213 
214 	if (!fileStream->file)
215 		fileStream->status = DE_STREAMSTATUS_ERROR;;
216 }
217