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