2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/internal/adaptor-framework/common/file-stream-impl.h>
24 #include <dali/integration-api/debug.h>
28 FileStream::Impl::Impl(const std::string& filename, uint8_t mode)
29 : mFileName(filename),
35 DALI_ASSERT_DEBUG(!filename.empty() && "Can't open a empty filename.");
36 DALI_ASSERT_DEBUG(mode != 0 && "No mode is undefined behaviour");
39 FileStream::Impl::Impl(uint8_t* buffer, size_t dataSize, uint8_t mode)
45 DALI_ASSERT_DEBUG(buffer != 0 && "Can't open file on null buffer.");
46 DALI_ASSERT_DEBUG(dataSize > 0 && "Pointless to open file on empty buffer.");
47 DALI_ASSERT_DEBUG(mode != 0 && "No mode is undefined behaviour.");
50 FileStream::Impl::Impl(Dali::Vector<uint8_t>& vector, size_t dataSize, uint8_t mode)
56 // Resize the buffer to ensure any null that gets written by
57 // fmemopen is written past the end of any data that is written to the buffer.
58 // (Workaround for a bug in Ubuntu that overwrites null to the last byte of the
59 // data block regardless of whether binary mode was specified. Tizen doesn't write
60 // null if binary mode is specified).
63 vector.Resize(mDataSize);
66 DALI_ASSERT_DEBUG(mBuffer != nullptr && "Can't open file on null buffer.");
67 DALI_ASSERT_DEBUG(dataSize > 0 && "Pointless to open file on empty buffer.");
68 DALI_ASSERT_DEBUG(mode != 0 && "No mode is undefined behaviour.");
71 FileStream::Impl::~Impl()
75 const int closeFailed = fclose(mFile);
78 DALI_LOG_WARNING("File close failed for FILE: \"%p\".\n", static_cast<void*>(mFile));
84 if(mFileStream.is_open())
90 std::iostream& FileStream::Impl::GetStream()
94 // return empty stream if FILE stream is open to avoid simultaneous access to the same file
98 if(mFileStream.is_open())
103 if(mBufferStream.rdbuf()->in_avail())
105 return mBufferStream;
110 if(mMode & Dali::FileStream::APPEND)
112 openMode |= (std::ios::out | std::ios::app);
114 else if(mMode & Dali::FileStream::WRITE)
116 openMode |= (std::ios::out | std::ios::ate);
119 if(mMode & Dali::FileStream::READ)
121 openMode |= std::ios::in;
124 if(mMode & Dali::FileStream::BINARY)
126 openMode |= std::ios::binary;
129 if(!mFileName.empty())
131 mFileStream.open(mFileName, static_cast<std::ios_base::openmode>(openMode));
132 if(!mFileStream.is_open())
134 DALI_LOG_WARNING("stream open failed for: \"%s\", in mode: \"%d\".\n", mFileName.c_str(), openMode);
140 mBufferStream.rdbuf()->pubsetbuf(reinterpret_cast<char*>(mBuffer), mDataSize);
141 if(!mBufferStream.rdbuf()->in_avail())
143 DALI_LOG_WARNING("File open failed for memory buffer at location: \"%p\", of size: \"%u\", in mode: \"%d\".\n",
144 static_cast<void*>(mBuffer),
145 static_cast<unsigned>(mDataSize),
150 return mBufferStream;
153 FILE* FileStream::Impl::GetFile()
155 if(mFileStream.is_open() || mBufferStream.rdbuf()->in_avail())
157 // return empty FILE stream if the stream is open to avoid simultaneous access to the same file
166 char openMode[16] = {0};
169 if(mMode & Dali::FileStream::APPEND)
173 else if(mMode & Dali::FileStream::WRITE)
182 if(mMode & Dali::FileStream::BINARY)
189 if(!mFileName.empty())
191 mFile = fopen(mFileName.c_str(), openMode);
195 DALI_LOG_ERROR("file open failed for: \"%s\", in mode: \"%s\".\n", mFileName.c_str(), openMode);
196 DALI_LOG_ERROR("file open failed error : %s\n", strerror_r(errno, buf, 512));
201 mFile = fmemopen(mBuffer, mDataSize, openMode);
205 DALI_LOG_ERROR("File open failed for memory buffer at location: \"%p\", of size: \"%u\", in mode: \"%s\".\n",
206 static_cast<void*>(mBuffer),
207 static_cast<unsigned>(mDataSize),
209 DALI_LOG_ERROR("file open failed error : %s\n", strerror_r(errno, buf, 512));