Skip to content

Commit

Permalink
Merge pull request #4963 from leoetlino/sysconf
Browse files Browse the repository at this point in the history
SysConf: Use vectors instead of raw pointers
  • Loading branch information
Helios747 committed Feb 27, 2017
2 parents 832e050 + a1e16c4 commit 7bcff99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
25 changes: 11 additions & 14 deletions Source/Core/Common/SysConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <algorithm>
#include <cinttypes>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -33,9 +34,6 @@ SysConf::~SysConf()

void SysConf::Clear()
{
for (auto i = m_Entries.begin(); i < m_Entries.end() - 1; ++i)
delete[] i->data;

m_Entries.clear();
}

Expand Down Expand Up @@ -107,7 +105,7 @@ bool SysConf::LoadFromFileInternal(File::IOFile&& file)
SSysConfEntry tmpEntry;
file.ReadArray(&tmpEntry.offset, 1);
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
Expand All @@ -126,7 +124,7 @@ bool SysConf::LoadFromFileInternal(File::IOFile&& file)
file.ReadArray(curEntry.name, curEntry.nameLength);
curEntry.name[curEntry.nameLength] = '\0';
// Get length of data
curEntry.data = nullptr;
curEntry.data.clear();
curEntry.dataLength = 0;
switch (curEntry.type)
{
Expand Down Expand Up @@ -169,8 +167,8 @@ bool SysConf::LoadFromFileInternal(File::IOFile&& file)
// Fill in the actual data
if (curEntry.dataLength)
{
curEntry.data = new u8[curEntry.dataLength];
file.ReadArray(curEntry.data, curEntry.dataLength);
curEntry.data.resize(curEntry.dataLength);
file.ReadArray(curEntry.data.data(), curEntry.dataLength);
}
}

Expand All @@ -186,8 +184,7 @@ static unsigned int create_item(SSysConfEntry& item, SysconfType type, const std
item.nameLength = (u8)(name.length());
strncpy(item.name, name.c_str(), 32);
item.dataLength = data_length;
item.data = new u8[data_length];
memset(item.data, 0, data_length);
item.data.resize(data_length);
switch (type)
{
case Type_BigArray:
Expand Down Expand Up @@ -235,7 +232,7 @@ void SysConf::GenerateSysConf()
// IPL.NIK
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'};
memcpy(items[2].data, console_nick, 14);
memcpy(items[2].data.data(), console_nick, 14);

// IPL.AR
current_offset += create_item(items[3], Type_Byte, "IPL.AR", 1, current_offset);
Expand Down Expand Up @@ -358,19 +355,19 @@ void SysConf::GenerateSysConf()
{
const u16 tmpDataLength = Common::swap16(item.dataLength);
g.WriteBytes(&tmpDataLength, 2);
g.WriteBytes(item.data, item.dataLength);
g.WriteBytes(item.data.data(), item.dataLength);
g.WriteBytes(&null_byte, 1);
}
break;

case Type_SmallArray:
g.WriteBytes(&item.dataLength, 1);
g.WriteBytes(item.data, item.dataLength);
g.WriteBytes(item.data.data(), item.dataLength);
g.WriteBytes(&null_byte, 1);
break;

default:
g.WriteBytes(item.data, item.dataLength);
g.WriteBytes(item.data.data(), item.dataLength);
break;
}
}
Expand Down Expand Up @@ -410,7 +407,7 @@ bool SysConf::SaveToFile(const std::string& filename)
}

// Now write the actual data
f.WriteBytes(i->data, i->dataLength);
f.WriteBytes(i->data.data(), i->dataLength);
}

return f.IsGood();
Expand Down
18 changes: 9 additions & 9 deletions Source/Core/Common/SysConf.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ struct SSysConfHeader

struct SSysConfEntry
{
u16 offset;
u16 offset = 0;
SysconfType type;
u8 nameLength;
char name[32];
u16 dataLength;
u8* data;
u8 nameLength = 0;
char name[32] = {};
u16 dataLength = 0;
std::vector<u8> data;

template <class T>
T GetData()
{
return *(T*)data;
return *(T*)data.data();
}
bool GetArrayData(u8* dest, u16 destSize)
{
if (dest && destSize >= dataLength)
{
memcpy(dest, data, dataLength);
memcpy(dest, data.data(), dataLength);
return true;
}
return false;
Expand All @@ -70,7 +70,7 @@ struct SSysConfEntry
{
if (buffer)
{
memcpy(data, buffer, std::min<u16>(bufferSize, dataLength));
memcpy(data.data(), buffer, std::min<u16>(bufferSize, dataLength));
return true;
}
return false;
Expand Down Expand Up @@ -169,7 +169,7 @@ class SysConf
return false;
}

*(T*)index->data = newValue;
*(T*)index->data.data() = newValue;
return true;
}

Expand Down

0 comments on commit 7bcff99

Please sign in to comment.