13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Stream Library 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * --------------------------- 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Stream wrapper for deFile 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deFileStream.h" 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdlib.h> 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef struct FileStream_s 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deFile* file; 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deStreamStatus status; 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const char* error; 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} FileStream; 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic deStreamResult fileIOStream_read (deStreamData* stream, void* buf, deInt32 bufSize, deInt32* numRead) 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deInt64 _numRead = 0; 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = (FileStream*)stream; 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deFileResult result = deFile_read(fileStream->file, buf, bufSize, &_numRead); 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *numRead = (deInt32)_numRead; 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry switch (result) 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry case DE_FILERESULT_SUCCESS: 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_SUCCESS; 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry case DE_FILERESULT_ERROR: 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = "deFile: DE_FILERESULT_ERROR"; 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR; 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_ERROR; 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry case DE_FILERESULT_END_OF_FILE: 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_END_OF_STREAM; 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry default: 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = "Uknown: DE_FILERESULT"; 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR; 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_ERROR; 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry }; 653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic deStreamResult fileIOStream_write (deStreamData* stream, const void* buf, deInt32 bufSize, deInt32* numWritten) 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deInt64 _numWritten = 0; 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = (FileStream*)stream; 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deFileResult result = deFile_write(fileStream->file, buf, bufSize, &_numWritten); 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *numWritten = (deInt32)_numWritten; 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry switch (result) 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry case DE_FILERESULT_SUCCESS: 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_SUCCESS; 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry case DE_FILERESULT_ERROR: 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = "deFile: DE_FILERESULT_ERROR"; 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR; 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_ERROR; 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry case DE_FILERESULT_END_OF_FILE: 883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_END_OF_STREAM; 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry default: 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = "Uknown: DE_FILERESULT"; 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR; 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_ERROR; 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry break; 963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry }; 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const char* fileIOStream_getError (deStreamData* stream) 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = (FileStream*)stream; 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \note [mika] There is only error reporting through return value in deFile */ 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return fileStream->error; 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic deStreamResult fileIOStream_flush (deStreamData* stream) 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \todo mika deFile doesn't have flush, how should this be handled? */ 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_UNREF(stream); 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_SUCCESS; 1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic deStreamResult fileIOStream_deinit (deStreamData* stream) 1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = (FileStream*)stream; 1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deFile_destroy(fileStream->file); 1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry free(fileStream); 1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_STREAMRESULT_SUCCESS; 1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic deStreamStatus fileIOStrem_getStatus (deStreamData* stream) 1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = (FileStream*)stream; 1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return fileStream->status; 1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const deIOStreamVFTable fileIOStreamVFTable = { 1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_read, 1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_write, 1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_getError, 1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_flush, 1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_deinit, 1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStrem_getStatus 1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const deIOStreamVFTable fileInStreamVFTable = { 1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_read, 1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_NULL, 1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_getError, 1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_NULL, 1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_deinit, 1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStrem_getStatus 1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const deIOStreamVFTable fileOutStreamVFTable = { 1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_NULL, 1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_write, 1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_getError, 1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_flush, 1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStream_deinit, 1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileIOStrem_getStatus 1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid fileIOStream_init (deIOStream* stream, const char* filename, deFileMode mode) 1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = DE_NULL; 1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(stream); 1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream = malloc(sizeof(FileStream)); 1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \note mika Check that file is readable and writeable, currently not supported by deFile */ 1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry stream->vfTable = &fileIOStreamVFTable; 1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry stream->streamData = (deStreamData*)fileStream; 1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->file = deFile_create(filename, mode); 1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_GOOD; 1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = DE_NULL; 1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (!fileStream->file) 1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR; 1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deFileInStream_init (deInStream* stream, const char* filename, deFileMode mode) 1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = DE_NULL; 1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(stream); 1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream = malloc(sizeof(FileStream)); 1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \note mika Check that file is readable, currently not supported by deFile */ 1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry stream->ioStream.vfTable = &fileInStreamVFTable; 1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry stream->ioStream.streamData = (deStreamData*)fileStream; 1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->file = deFile_create(filename, mode); 1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_GOOD; 1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = DE_NULL; 1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (!fileStream->file) 1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR; 1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deFileOutStream_init (deOutStream* stream, const char* filename, deFileMode mode) 1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FileStream* fileStream = DE_NULL; 2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(stream); 2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream = malloc(sizeof(FileStream)); 2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \note mika Check that file is writeable, currently not supported by deFile */ 2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry stream->ioStream.vfTable = &fileOutStreamVFTable; 2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry stream->ioStream.streamData = (deStreamData*)fileStream; 2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->file = deFile_create(filename, mode); 2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_GOOD; 2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->error = DE_NULL; 2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (!fileStream->file) 2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry fileStream->status = DE_STREAMSTATUS_ERROR;; 2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 217