axmol/cocos/3d/CCBundleReader.cpp

191 lines
4.2 KiB
C++
Raw Normal View History

2014-07-29 10:49:06 +08:00
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
#include "3d/CCBundleReader.h"
2014-06-19 15:43:02 +08:00
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
2014-06-25 09:13:24 +08:00
BundleReader::BundleReader()
2014-06-19 15:43:02 +08:00
{
2014-08-29 10:43:26 +08:00
_buffer = nullptr;
_position = 0;
_length = 0;
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
{
2014-06-27 14:42:48 +08:00
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;
_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())
{
CCLOG("warning: bundle reader out of range");
2014-06-19 15:43:02 +08:00
return 0;
}
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;
2014-06-27 14:42:48 +08:00
ssize_t needLength = size*count;
char* ptr1 = (char*)ptr;
if(validLength < needLength)
2014-06-27 14:42:48 +08:00
{
validCount = validLength/size;
ssize_t readLength = size*validCount;
2014-08-29 10:43:26 +08:00
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;
if(readLength>0)
{
2014-08-29 10:43:26 +08:00
memcpy(ptr1,(char*)_buffer+_position,readLength);
_position += readLength;
2014-06-27 14:42:48 +08:00
validCount+=1;
}
2014-08-29 10:43:26 +08:00
CCLOG("warning: bundle reader out of range");
2014-06-27 14:42:48 +08:00
}
else
{
2014-08-29 10:43:26 +08:00
memcpy(ptr1,(char*)_buffer+_position,needLength);
_position += needLength;
2014-06-27 14:42:48 +08:00
validCount = count;
}
return validCount;
2014-06-19 15:43:02 +08:00
}
char* BundleReader::readLine(int num,char* line)
{
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
2014-08-29 10:43:26 +08:00
char* buffer = (char*)_buffer+_position;
2014-06-27 14:42:48 +08:00
char* p = line;
char c;
ssize_t readNum = 0;
2014-08-29 10:43:26 +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;
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
}
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
}
bool BundleReader::seek(long int offset, int origin)
{
2014-08-29 10:43:26 +08:00
if (!_buffer)
2014-06-19 15:43:02 +08:00
return false;
2014-06-27 14:42:48 +08:00
if(origin == SEEK_CUR)
{
2014-08-29 10:43:26 +08:00
_position += offset;
2014-06-27 14:42:48 +08:00
}
else if(origin == SEEK_SET)
{
2014-08-29 10:43:26 +08:00
_position = offset;
2014-06-27 14:42:48 +08:00
}
else if(origin == SEEK_END)
{
2014-08-29 10:43:26 +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;
2014-06-27 14:42:48 +08:00
if(read(&length, 4, 1) != 1)
2014-06-19 15:43:02 +08:00
{
return std::string();
}
std::string str;
2014-09-19 14:30:02 +08:00
ssize_t validLength = _length - _position;
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();
}
}
2014-09-19 14:30:02 +08:00
2014-06-19 15:43:02 +08:00
return str;
}
bool BundleReader::readMatrix(float* m)
{
return (read(m, sizeof(float), 16) == 16);
}
NS_CC_END