2014-07-29 10:49:06 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "3d/CCBundleReader.h"
|
2014-06-19 15:43:02 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-06-19 15:43:02 +08:00
|
|
|
|
2014-06-25 09:13:24 +08:00
|
|
|
BundleReader::BundleReader()
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_buffer = nullptr;
|
2014-08-29 10:43:26 +08:00
|
|
|
_position = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_length = 0;
|
2014-06-19 15:43:02 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
BundleReader::~BundleReader(){
|
|
|
|
|
2014-06-25 09:13:24 +08:00
|
|
|
};
|
2014-06-19 15:43:02 +08:00
|
|
|
|
2014-08-29 10:43:26 +08:00
|
|
|
void BundleReader::init(char* buffer, ssize_t length)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
_position = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_buffer = buffer;
|
|
|
|
_length = length;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 14:42:48 +08:00
|
|
|
ssize_t BundleReader::read(void* ptr, ssize_t size, ssize_t count)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
if (!_buffer || eof())
|
2014-11-09 06:38:44 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("warning: bundle reader out of range");
|
2014-06-19 15:43:02 +08:00
|
|
|
return 0;
|
2014-11-09 06:38:44 +08:00
|
|
|
}
|
2014-06-19 15:43:02 +08:00
|
|
|
|
2014-06-27 14:42:48 +08:00
|
|
|
ssize_t validCount;
|
2014-08-29 10:43:26 +08:00
|
|
|
ssize_t validLength = _length - _position;
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t needLength = size * count;
|
|
|
|
char* ptr1 = (char*)ptr;
|
|
|
|
if (validLength < needLength)
|
2014-06-27 14:42:48 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
validCount = validLength / size;
|
|
|
|
ssize_t readLength = size * validCount;
|
|
|
|
memcpy(ptr1, (char*)_buffer + _position, readLength);
|
2014-06-27 14:42:48 +08:00
|
|
|
ptr1 += readLength;
|
2014-08-29 10:43:26 +08:00
|
|
|
_position += readLength;
|
2014-06-27 14:42:48 +08:00
|
|
|
readLength = validLength - readLength;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (readLength > 0)
|
2014-06-27 14:42:48 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
memcpy(ptr1, (char*)_buffer + _position, readLength);
|
2014-08-29 10:43:26 +08:00
|
|
|
_position += readLength;
|
2021-12-25 10:04:45 +08:00
|
|
|
validCount += 1;
|
2014-06-27 14:42:48 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("warning: bundle reader out of range");
|
2014-06-27 14:42:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
memcpy(ptr1, (char*)_buffer + _position, needLength);
|
2014-08-29 10:43:26 +08:00
|
|
|
_position += needLength;
|
2014-06-27 14:42:48 +08:00
|
|
|
validCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return validCount;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
char* BundleReader::readLine(int num, char* line)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
if (!_buffer)
|
2016-06-23 11:39:23 +08:00
|
|
|
return nullptr;
|
2014-06-19 15:43:02 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
char* buffer = (char*)_buffer + _position;
|
|
|
|
char* p = line;
|
2014-06-27 14:42:48 +08:00
|
|
|
char c;
|
|
|
|
ssize_t readNum = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
while ((c = *buffer) != 10 && readNum < (ssize_t)num && _position < _length)
|
2014-06-27 14:42:48 +08:00
|
|
|
{
|
|
|
|
*p = c;
|
|
|
|
p++;
|
|
|
|
buffer++;
|
2014-08-29 10:43:26 +08:00
|
|
|
_position++;
|
2014-06-27 14:42:48 +08:00
|
|
|
readNum++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return line;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BundleReader::eof()
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
if (!_buffer)
|
2014-06-19 15:43:02 +08:00
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-27 14:42:48 +08:00
|
|
|
return ((ssize_t)tell()) >= length();
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 14:42:48 +08:00
|
|
|
ssize_t BundleReader::length()
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
return _length;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2014-07-01 09:26:21 +08:00
|
|
|
ssize_t BundleReader::tell()
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
if (!_buffer)
|
2014-06-19 15:43:02 +08:00
|
|
|
return -1;
|
2014-08-29 10:43:26 +08:00
|
|
|
return _position;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 13:39:28 +08:00
|
|
|
bool BundleReader::seek(int32_t offset, int origin)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
if (!_buffer)
|
2014-06-19 15:43:02 +08:00
|
|
|
return false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (origin == SEEK_CUR)
|
2014-06-27 14:42:48 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
_position += offset;
|
2014-06-27 14:42:48 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (origin == SEEK_SET)
|
2014-06-27 14:42:48 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
_position = offset;
|
2014-06-27 14:42:48 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (origin == SEEK_END)
|
2014-06-27 14:42:48 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_position = _length + offset;
|
2014-06-27 14:42:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BundleReader::rewind()
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
if (_buffer != nullptr)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
_position = 0;
|
2014-06-27 14:42:48 +08:00
|
|
|
return true;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BundleReader::readString()
|
|
|
|
{
|
|
|
|
unsigned int length;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (read(&length, 4, 1) != 1)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-09-19 14:30:02 +08:00
|
|
|
ssize_t validLength = _length - _position;
|
2015-01-29 17:40:00 +08:00
|
|
|
if (length > 0 && static_cast<ssize_t>(length) <= validLength)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
|
|
|
str.resize(length);
|
2014-06-27 14:42:48 +08:00
|
|
|
if (read(&str[0], 1, length) != length)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-19 15:43:02 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BundleReader::readMatrix(float* m)
|
|
|
|
{
|
|
|
|
return (read(m, sizeof(float), 16) == 16);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|