Merge pull request #4963 from leoetlino/sysconf

SysConf: Use vectors instead of raw pointers
This commit is contained in:
Anthony 2017-02-26 16:09:17 -08:00 committed by GitHub
commit 7bcff99d89
2 changed files with 20 additions and 23 deletions

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <cinttypes> #include <cinttypes>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
@ -33,9 +34,6 @@ SysConf::~SysConf()
void SysConf::Clear() void SysConf::Clear()
{ {
for (auto i = m_Entries.begin(); i < m_Entries.end() - 1; ++i)
delete[] i->data;
m_Entries.clear(); m_Entries.clear();
} }
@ -107,7 +105,7 @@ bool SysConf::LoadFromFileInternal(File::IOFile&& file)
SSysConfEntry tmpEntry; SSysConfEntry tmpEntry;
file.ReadArray(&tmpEntry.offset, 1); file.ReadArray(&tmpEntry.offset, 1);
tmpEntry.offset = Common::swap16(tmpEntry.offset); tmpEntry.offset = Common::swap16(tmpEntry.offset);
m_Entries.push_back(tmpEntry); m_Entries.push_back(std::move(tmpEntry));
} }
// Last offset is an invalid entry. We ignore it throughout this class // Last offset is an invalid entry. We ignore it throughout this class
@ -126,7 +124,7 @@ bool SysConf::LoadFromFileInternal(File::IOFile&& file)
file.ReadArray(curEntry.name, curEntry.nameLength); file.ReadArray(curEntry.name, curEntry.nameLength);
curEntry.name[curEntry.nameLength] = '\0'; curEntry.name[curEntry.nameLength] = '\0';
// Get length of data // Get length of data
curEntry.data = nullptr; curEntry.data.clear();
curEntry.dataLength = 0; curEntry.dataLength = 0;
switch (curEntry.type) switch (curEntry.type)
{ {
@ -169,8 +167,8 @@ bool SysConf::LoadFromFileInternal(File::IOFile&& file)
// Fill in the actual data // Fill in the actual data
if (curEntry.dataLength) if (curEntry.dataLength)
{ {
curEntry.data = new u8[curEntry.dataLength]; curEntry.data.resize(curEntry.dataLength);
file.ReadArray(curEntry.data, curEntry.dataLength); file.ReadArray(curEntry.data.data(), curEntry.dataLength);
} }
} }
@ -186,8 +184,7 @@ static unsigned int create_item(SSysConfEntry& item, SysconfType type, const std
item.nameLength = (u8)(name.length()); item.nameLength = (u8)(name.length());
strncpy(item.name, name.c_str(), 32); strncpy(item.name, name.c_str(), 32);
item.dataLength = data_length; item.dataLength = data_length;
item.data = new u8[data_length]; item.data.resize(data_length);
memset(item.data, 0, data_length);
switch (type) switch (type)
{ {
case Type_BigArray: case Type_BigArray:
@ -235,7 +232,7 @@ void SysConf::GenerateSysConf()
// IPL.NIK // IPL.NIK
current_offset += create_item(items[2], Type_SmallArray, "IPL.NIK", 0x15, current_offset); current_offset += create_item(items[2], Type_SmallArray, "IPL.NIK", 0x15, current_offset);
const u8 console_nick[14] = {0, 'd', 0, 'o', 0, 'l', 0, 'p', 0, 'h', 0, 'i', 0, 'n'}; const u8 console_nick[14] = {0, 'd', 0, 'o', 0, 'l', 0, 'p', 0, 'h', 0, 'i', 0, 'n'};
memcpy(items[2].data, console_nick, 14); memcpy(items[2].data.data(), console_nick, 14);
// IPL.AR // IPL.AR
current_offset += create_item(items[3], Type_Byte, "IPL.AR", 1, current_offset); current_offset += create_item(items[3], Type_Byte, "IPL.AR", 1, current_offset);
@ -358,19 +355,19 @@ void SysConf::GenerateSysConf()
{ {
const u16 tmpDataLength = Common::swap16(item.dataLength); const u16 tmpDataLength = Common::swap16(item.dataLength);
g.WriteBytes(&tmpDataLength, 2); g.WriteBytes(&tmpDataLength, 2);
g.WriteBytes(item.data, item.dataLength); g.WriteBytes(item.data.data(), item.dataLength);
g.WriteBytes(&null_byte, 1); g.WriteBytes(&null_byte, 1);
} }
break; break;
case Type_SmallArray: case Type_SmallArray:
g.WriteBytes(&item.dataLength, 1); g.WriteBytes(&item.dataLength, 1);
g.WriteBytes(item.data, item.dataLength); g.WriteBytes(item.data.data(), item.dataLength);
g.WriteBytes(&null_byte, 1); g.WriteBytes(&null_byte, 1);
break; break;
default: default:
g.WriteBytes(item.data, item.dataLength); g.WriteBytes(item.data.data(), item.dataLength);
break; break;
} }
} }
@ -410,7 +407,7 @@ bool SysConf::SaveToFile(const std::string& filename)
} }
// Now write the actual data // Now write the actual data
f.WriteBytes(i->data, i->dataLength); f.WriteBytes(i->data.data(), i->dataLength);
} }
return f.IsGood(); return f.IsGood();

View File

@ -45,23 +45,23 @@ struct SSysConfHeader
struct SSysConfEntry struct SSysConfEntry
{ {
u16 offset; u16 offset = 0;
SysconfType type; SysconfType type;
u8 nameLength; u8 nameLength = 0;
char name[32]; char name[32] = {};
u16 dataLength; u16 dataLength = 0;
u8* data; std::vector<u8> data;
template <class T> template <class T>
T GetData() T GetData()
{ {
return *(T*)data; return *(T*)data.data();
} }
bool GetArrayData(u8* dest, u16 destSize) bool GetArrayData(u8* dest, u16 destSize)
{ {
if (dest && destSize >= dataLength) if (dest && destSize >= dataLength)
{ {
memcpy(dest, data, dataLength); memcpy(dest, data.data(), dataLength);
return true; return true;
} }
return false; return false;
@ -70,7 +70,7 @@ struct SSysConfEntry
{ {
if (buffer) if (buffer)
{ {
memcpy(data, buffer, std::min<u16>(bufferSize, dataLength)); memcpy(data.data(), buffer, std::min<u16>(bufferSize, dataLength));
return true; return true;
} }
return false; return false;
@ -169,7 +169,7 @@ public:
return false; return false;
} }
*(T*)index->data = newValue; *(T*)index->data.data() = newValue;
return true; return true;
} }