axmol/cocos/3d/CCBundleReader.cpp

159 lines
2.8 KiB
C++
Raw Normal View History

2014-06-19 15:43:02 +08:00
#include "CCBundleReader.h"
#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
{
m_buffer = nullptr;
2014-06-25 09:13:24 +08:00
m_position = 0;
m_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-06-30 17:10:07 +08:00
void BundleReader::init(char* lpbuffer, ssize_t length)
2014-06-19 15:43:02 +08:00
{
2014-06-25 09:13:24 +08:00
m_position = 0;
m_buffer = lpbuffer;
m_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
{
if (!m_buffer || eof())
return 0;
2014-06-27 14:42:48 +08:00
ssize_t validCount;
ssize_t validLength = m_length - m_position;
ssize_t needLength = size*count;
char* ptr1 = (char*)ptr;
if(validLength <= needLength)
{
validCount = validLength/size;
ssize_t readLength = size*validCount;
memcpy(ptr1,(char*)m_buffer+m_position,readLength);
ptr1 += readLength;
m_position += readLength;
readLength = validLength - readLength;
if(readLength>0)
{
memcpy(ptr1,(char*)m_buffer+m_position,readLength);
m_position += readLength;
validCount+=1;
}
}
else
{
memcpy(ptr1,(char*)m_buffer+m_position,needLength);
m_position += needLength;
validCount = count;
}
return validCount;
2014-06-19 15:43:02 +08:00
}
char* BundleReader::readLine(int num,char* line)
{
if (!m_buffer)
return 0;
2014-06-27 14:42:48 +08:00
char* buffer = (char*)m_buffer+m_position;
char* p = line;
char c;
ssize_t readNum = 0;
while((c=*buffer) != 10 && readNum < (ssize_t)num && m_position < m_length)
2014-06-27 14:42:48 +08:00
{
*p = c;
p++;
buffer++;
m_position++;
readNum++;
}
*p = '\0';
return line;
2014-06-19 15:43:02 +08:00
}
bool BundleReader::eof()
{
if (!m_buffer)
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
{
return m_length;
}
ssize_t BundleReader::tell()
2014-06-19 15:43:02 +08:00
{
if (!m_buffer)
return -1;
return m_position;
}
bool BundleReader::seek(long int offset, int origin)
{
if (!m_buffer)
return false;
2014-06-27 14:42:48 +08:00
if(origin == SEEK_CUR)
{
m_position += offset;
}
else if(origin == SEEK_SET)
{
m_position = offset;
}
else if(origin == SEEK_END)
{
m_position = m_length+offset;
}
else
return false;
return true;
2014-06-19 15:43:02 +08:00
}
bool BundleReader::rewind()
{
if (m_buffer != nullptr)
2014-06-19 15:43:02 +08:00
{
2014-06-27 14:42:48 +08:00
m_position = 0;
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;
if (length > 0)
{
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();
}
}
return str;
}
bool BundleReader::readMatrix(float* m)
{
return (read(m, sizeof(float), 16) == 16);
}
}