mirror of
https://github.com/libAthena/athena.git
synced 2025-12-09 13:38:03 +00:00
* Working on Wii Port
This commit is contained in:
@@ -24,6 +24,10 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef HW_RVL
|
||||
#include <malloc.h>
|
||||
#endif // HW_RVL
|
||||
|
||||
namespace zelda
|
||||
{
|
||||
namespace io
|
||||
@@ -55,7 +59,11 @@ BinaryReader::BinaryReader(const std::string& filename)
|
||||
fseek(in, 0, SEEK_END);
|
||||
length = ftell(in);
|
||||
fseek(in, 0, SEEK_SET);
|
||||
#ifdef HW_RVL
|
||||
m_data = (Uint8*)memalign(32, length);
|
||||
#else
|
||||
m_data = new Uint8[length];
|
||||
#endif
|
||||
|
||||
Uint32 done = 0;
|
||||
Uint32 blocksize = BLOCKSZ;
|
||||
@@ -101,7 +109,7 @@ Int16 BinaryReader::readInt16()
|
||||
if (m_position + sizeof(Int16) > m_length)
|
||||
throw error::IOException("BinaryReader::readInt16 -> Position outside stream bounds");
|
||||
Int16 ret = *(Int16*)(m_data + m_position);
|
||||
m_position += 2;
|
||||
m_position += sizeof(Int16);
|
||||
|
||||
if ((!utility::isSystemBigEndian() && m_endian == BigEndian) || (utility::isSystemBigEndian() && m_endian == LittleEndian))
|
||||
ret = utility::swap16(ret);
|
||||
@@ -118,7 +126,7 @@ Uint16 BinaryReader::readUInt16()
|
||||
if (m_position + sizeof(Uint16) > m_length)
|
||||
throw error::IOException("BinaryReader::readUint16 -> Position outside stream bounds");
|
||||
Uint16 ret = *(Uint16*)(m_data + m_position);
|
||||
m_position += 2;
|
||||
m_position += sizeof(Uint16);
|
||||
|
||||
if ((!utility::isSystemBigEndian() && m_endian == BigEndian) || (utility::isSystemBigEndian() && m_endian == LittleEndian))
|
||||
ret = utility::swapU16(ret);
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef HW_RVL
|
||||
#include <malloc.h>
|
||||
#endif // HW_RVL
|
||||
|
||||
namespace zelda
|
||||
{
|
||||
namespace io
|
||||
@@ -44,12 +48,27 @@ BinaryWriter::BinaryWriter(const std::string& filename)
|
||||
m_length = 0x10;
|
||||
m_bitPosition = 0;
|
||||
m_position = 0;
|
||||
#ifdef HW_RVL
|
||||
m_data = (Uint8*)memalign(32, m_length);
|
||||
#else
|
||||
m_data = new Uint8[m_length];
|
||||
#endif
|
||||
|
||||
if (!m_data)
|
||||
throw error::IOException("BinaryWriter::BinaryWriter -> Could not allocate memory!");
|
||||
memset(m_data, 0, m_length);
|
||||
}
|
||||
|
||||
void BinaryWriter::setFilepath(const std::string& filepath)
|
||||
{
|
||||
m_filepath = filepath;
|
||||
}
|
||||
|
||||
std::string BinaryWriter::filepath() const
|
||||
{
|
||||
return m_filepath;
|
||||
}
|
||||
|
||||
void BinaryWriter::save(const std::string& filename)
|
||||
{
|
||||
if (filename.empty() && m_filepath.empty())
|
||||
|
||||
@@ -19,6 +19,10 @@
|
||||
#include <string.h>
|
||||
#include <sstream>
|
||||
|
||||
#ifdef HW_RVL
|
||||
#include <malloc.h>
|
||||
#endif // HW_RVL
|
||||
|
||||
namespace zelda
|
||||
{
|
||||
namespace io
|
||||
@@ -49,7 +53,12 @@ Stream::Stream(const Uint8* data, Uint64 length) :
|
||||
m_data = (Uint8*)data;
|
||||
else
|
||||
{
|
||||
#ifdef HW_RVL
|
||||
m_data = (Uint8*)memalign(32, m_length);
|
||||
#else
|
||||
m_data = new Uint8[m_length];
|
||||
#endif
|
||||
|
||||
memset(m_data, 0, m_length);
|
||||
}
|
||||
}
|
||||
@@ -59,7 +68,11 @@ Stream::Stream(Int64 length) :
|
||||
m_position(0),
|
||||
m_length(length)
|
||||
{
|
||||
#ifdef HW_RVL
|
||||
m_data = (Uint8*)memalign(32, m_length);
|
||||
#else
|
||||
m_data = new Uint8[m_length];
|
||||
#endif
|
||||
memset(m_data, 0, m_length);
|
||||
}
|
||||
|
||||
@@ -68,6 +81,7 @@ Stream::Stream(Stream* stream)
|
||||
if (m_data)
|
||||
delete[] m_data;
|
||||
|
||||
m_data = NULL;
|
||||
m_data = stream->m_data;
|
||||
m_position = stream->m_position;
|
||||
m_length = stream->m_length;
|
||||
@@ -76,7 +90,11 @@ Stream::Stream(Stream* stream)
|
||||
Stream::~Stream()
|
||||
{
|
||||
if (m_data)
|
||||
#ifdef HW_RVL
|
||||
free(m_data);
|
||||
#else
|
||||
delete[] m_data;
|
||||
#endif
|
||||
|
||||
m_data = NULL;
|
||||
m_position = 0;
|
||||
@@ -196,7 +214,13 @@ Int8* Stream::readBytes(Int64 length)
|
||||
if (m_position + length > m_length)
|
||||
throw error::IOException("Stream::readBytes -> Position passed stream bounds: " + m_position);
|
||||
|
||||
Int8* ret = new Int8[length];
|
||||
Int8* ret;
|
||||
#ifdef HW_RVL
|
||||
ret = (Int8*)memalign(32, length);
|
||||
#else
|
||||
ret = new Int8[length];
|
||||
#endif
|
||||
|
||||
memcpy(ret, (const Int8*)(m_data + m_position), length);
|
||||
m_position += length;
|
||||
return ret;
|
||||
@@ -250,13 +274,22 @@ void Stream::resize(Uint64 newSize)
|
||||
throw error::InvalidOperationException("Stream::resize() -> New size cannot be less to the old size.");
|
||||
|
||||
// Allocate and copy new buffer
|
||||
Uint8* newArray = new Uint8[newSize];
|
||||
#ifdef HW_RVL
|
||||
Uint8* newArray = (Uint8*)memalign(32, newSize);
|
||||
#else
|
||||
Uint8* newArray = new Uint8[newSize];
|
||||
#endif
|
||||
|
||||
memset(newArray, 0, newSize);
|
||||
|
||||
memcpy(newArray, m_data, m_length);
|
||||
|
||||
// Delete the old one
|
||||
delete[] m_data;
|
||||
#ifdef HW_RVL
|
||||
free(m_data);
|
||||
#else
|
||||
delete[] m_data;
|
||||
#endif
|
||||
|
||||
// Swap the pointer and size out for the new ones.
|
||||
m_data = newArray;
|
||||
@@ -266,7 +299,11 @@ void Stream::resize(Uint64 newSize)
|
||||
void Stream::setData(const Uint8* data, Uint64 length)
|
||||
{
|
||||
if (m_data)
|
||||
#ifdef HW_RVL
|
||||
free(m_data);
|
||||
#else
|
||||
delete[] m_data;
|
||||
#endif
|
||||
|
||||
m_data = (Uint8*)data;
|
||||
m_length = length;
|
||||
@@ -281,17 +318,17 @@ Uint8* Stream::data() const
|
||||
return ret;
|
||||
}
|
||||
|
||||
Int64 Stream::length()
|
||||
Int64 Stream::length() const
|
||||
{
|
||||
return m_length;
|
||||
}
|
||||
|
||||
Int64 Stream::position()
|
||||
Int64 Stream::position() const
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
bool Stream::atEnd()
|
||||
bool Stream::atEnd() const
|
||||
{
|
||||
return m_position >= m_length;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace zelda
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
TextStream::TextStream(const std::string& filename, TextMode fileMode, AccessMode accessMode) :
|
||||
TextStream::TextStream(const std::string& filename, Uint32 fileMode, AccessMode accessMode) :
|
||||
m_filename(filename),
|
||||
m_textmode(fileMode),
|
||||
m_accessmode(accessMode),
|
||||
@@ -69,6 +69,10 @@ TextStream::TextStream(const std::string& filename, TextMode fileMode, AccessMod
|
||||
loadLines();
|
||||
}
|
||||
|
||||
TextStream::TextStream()
|
||||
: Stream()
|
||||
{}
|
||||
|
||||
void TextStream::save(const std::string& filename)
|
||||
{
|
||||
if (m_accessmode != WriteOnly && m_accessmode != ReadWrite)
|
||||
@@ -78,7 +82,10 @@ void TextStream::save(const std::string& filename)
|
||||
|
||||
// We need a new buffer to write the new lines
|
||||
if (m_data)
|
||||
{
|
||||
delete[] m_data;
|
||||
m_data = NULL;
|
||||
}
|
||||
|
||||
m_position = 0;
|
||||
m_length = 1;
|
||||
@@ -90,6 +97,8 @@ void TextStream::save(const std::string& filename)
|
||||
for (std::string s : m_lines)
|
||||
writeBytes((Int8*)s.c_str(), s.size());
|
||||
|
||||
writeByte('\n');
|
||||
|
||||
FILE* out = fopen(m_filename.c_str(), "wb");
|
||||
|
||||
if(out)
|
||||
@@ -117,14 +126,14 @@ void TextStream::writeLine(const std::string& str)
|
||||
{
|
||||
if (m_accessmode != WriteOnly && m_accessmode != ReadWrite)
|
||||
throw error::InvalidOperationException("TextStream::writeLine -> Stream not open for writing");
|
||||
else if (m_currentLine > m_lines.size())
|
||||
else if (m_currentLine >= m_lines.size())
|
||||
{
|
||||
m_lines.push_back(str);
|
||||
m_lines.push_back(str + "\n");
|
||||
m_currentLine++;
|
||||
return;
|
||||
}
|
||||
|
||||
m_lines[m_currentLine++] = str;
|
||||
m_lines[m_currentLine++] = str + "\n";
|
||||
}
|
||||
|
||||
void TextStream::writeLines(std::vector<std::string> strings)
|
||||
@@ -211,11 +220,16 @@ void TextStream::setTextMode(TextMode mode)
|
||||
m_textmode = mode;
|
||||
}
|
||||
|
||||
TextStream::TextMode TextStream::textMode() const
|
||||
Uint32 TextStream::textMode() const
|
||||
{
|
||||
return m_textmode;
|
||||
}
|
||||
|
||||
void TextStream::truncate()
|
||||
{
|
||||
m_lines.clear();
|
||||
}
|
||||
|
||||
bool TextStream::isOpenForReading() const
|
||||
{
|
||||
return ((m_accessmode == ReadOnly || m_accessmode == ReadWrite) && m_accessmode != WriteOnly);
|
||||
@@ -229,35 +243,43 @@ bool TextStream::isOpenForWriting() const
|
||||
// PRIVATE FUNCTIONS
|
||||
void TextStream::loadLines()
|
||||
{
|
||||
while (!atEnd())
|
||||
try
|
||||
{
|
||||
std::string line;
|
||||
Uint8 c;
|
||||
for (;;)
|
||||
{
|
||||
c = readByte();
|
||||
|
||||
if (c == '\r' || c == '\n')
|
||||
while (!atEnd())
|
||||
{
|
||||
std::string line;
|
||||
Uint8 c;
|
||||
for (;;)
|
||||
{
|
||||
m_currentLine++;
|
||||
line.push_back(c);
|
||||
if (*(Uint8*)(m_data + m_position + 1) == '\n')
|
||||
c = readByte();
|
||||
|
||||
if (c == '\r' || c == '\n')
|
||||
{
|
||||
m_currentLine++;
|
||||
line.push_back(c);
|
||||
if (*(Uint8*)(m_data + m_position + 1) == '\n')
|
||||
{
|
||||
line.push_back('\n');
|
||||
m_position++; // advance position past the new line character
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '\0')
|
||||
{
|
||||
line.push_back('\n');
|
||||
m_position++; // advance position past the new line character
|
||||
break;
|
||||
}
|
||||
break;
|
||||
line.push_back(c);
|
||||
}
|
||||
|
||||
if (c == '\0')
|
||||
{
|
||||
line.push_back('\n');
|
||||
break;
|
||||
}
|
||||
line.push_back(c);
|
||||
m_lines.push_back(line);
|
||||
}
|
||||
|
||||
m_lines.push_back(line);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
// The stream MAY throw an out of range error but we're not concerned with it
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
|
||||
namespace zelda
|
||||
{
|
||||
@@ -111,5 +114,96 @@ double swapDouble(double val)
|
||||
return (double)((Uint64)retVal);
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
|
||||
{
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, delim))
|
||||
elems.push_back(item);
|
||||
return elems;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> split(const std::string &s, char delim)
|
||||
{
|
||||
std::vector<std::string> elems;
|
||||
split(s, delim, elems);
|
||||
return elems;
|
||||
}
|
||||
|
||||
void tolower(std::string& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
||||
}
|
||||
|
||||
void toupper(std::string& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
|
||||
}
|
||||
|
||||
std::string sprintf(const char* fmt, ...)
|
||||
{
|
||||
int size = 512;
|
||||
char* buffer = 0;
|
||||
buffer = new char[size];
|
||||
va_list vl;
|
||||
va_start(vl, fmt);
|
||||
int nsize = vsnprintf(buffer, size, fmt, vl);
|
||||
if(size<=nsize)
|
||||
{ //fail delete buffer and try again
|
||||
delete[] buffer;
|
||||
buffer = 0;
|
||||
buffer = new char[nsize+1]; //+1 for /0
|
||||
nsize = vsnprintf(buffer, size, fmt, vl);
|
||||
}
|
||||
std::string ret(buffer);
|
||||
va_end(vl);
|
||||
delete[] buffer;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool parseBool(const std::string& boolean, bool &valid)
|
||||
{
|
||||
std::string val = boolean;
|
||||
// compare must be case insensitive
|
||||
// This is the cleanest solution since I only need to do it once
|
||||
std::transform(val.begin(), val.end(), val.begin(), ::tolower);
|
||||
|
||||
// Check for true first
|
||||
if (!val.compare("true") || !val.compare("1") || !val.compare("yes"))
|
||||
return (valid = true);
|
||||
|
||||
// Now false
|
||||
if (!val.compare("false") || !val.compare("0") || !val.compare("no"))
|
||||
{
|
||||
valid = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Well that could've gone better
|
||||
|
||||
return (valid = false);
|
||||
}
|
||||
|
||||
int countChar(const std::string &str, const char chr, int &lastOccur)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
int index = 0;
|
||||
for (char c : str)
|
||||
{
|
||||
if (c == chr)
|
||||
{
|
||||
lastOccur = index;
|
||||
ret++;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
} // utility
|
||||
} // zelda
|
||||
|
||||
Reference in New Issue
Block a user