StringUtil cleanup. Nothing seems broken.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6367 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2010-11-10 04:12:31 +00:00
parent 7b34def0fc
commit 26f84c1e74
20 changed files with 190 additions and 411 deletions
-1
View File
@@ -180,7 +180,6 @@ bool CreateFullPath(const char *fullPath)
// safety check to ensure we have good dir seperators
std::string strFullPath(fullPath);
NormalizeDirSep(&strFullPath);
const char *position = strFullPath.c_str();
while (true) {
+5 -5
View File
@@ -181,7 +181,7 @@ bool IniFile::Section::Get(const char* key, int* value, int defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParseInt(temp.c_str(), value))
if (retval && TryParse(temp.c_str(), value))
return true;
*value = defaultValue;
return false;
@@ -191,7 +191,7 @@ bool IniFile::Section::Get(const char* key, u32* value, u32 defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParseUInt(temp.c_str(), value))
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
@@ -201,7 +201,7 @@ bool IniFile::Section::Get(const char* key, bool* value, bool defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParseBool(temp.c_str(), value))
if (retval && TryParse(temp.c_str(), value))
return true;
*value = defaultValue;
return false;
@@ -211,7 +211,7 @@ bool IniFile::Section::Get(const char* key, float* value, float defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParseFloat(temp.c_str(), value))
if (retval && TryParse(temp.c_str(), value))
return true;
*value = defaultValue;
return false;
@@ -221,7 +221,7 @@ bool IniFile::Section::Get(const char* key, double* value, double defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParseDouble(temp.c_str(), value))
if (retval && TryParse(temp.c_str(), value))
return true;
*value = defaultValue;
return false;
+1 -1
View File
@@ -140,7 +140,7 @@ std::string MemUsage()
if (NULL == hProcess) return "MemUsage Error";
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
Ret = StringFromFormat("%s K", ThS((int)(pmc.WorkingSetSize / 1024), true, 7).c_str());
Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str());
CloseHandle(hProcess);
return Ret;
File diff suppressed because it is too large Load Diff
+36 -35
View File
@@ -22,22 +22,15 @@
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include "Common.h"
std::wstring StringFromFormat(const wchar_t* format, ...);
std::string StringFromFormat(const char* format, ...);
void ToStringFromFormat(std::string* out, const char* format, ...);
// WARNING - only call once with a set of args!
void StringFromFormatV(std::string* out, const char* format, va_list args);
// Cheap!
bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
// Good
std::string ArrayToString(const u8 *data, u32 size, u32 offset = 0, int line_len = 20, bool Spaces = true);
template<size_t Count>
inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
{
@@ -47,49 +40,57 @@ inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
va_end(args);
}
std::wstring StripSpaces(const std::wstring &s);
std::wstring StripQuotes(const std::wstring &s);
//std::wstring StripNewline(const std::string &s);
// Thousand separator. Turns 12345678 into 12,345,678
//std::wstring ThS(int a, bool b = true, int Spaces = 0);
// Good
std::string ArrayToString(const u8 *data, u32 size, int line_len = 20, bool spaces = true);
std::string StripSpaces(const std::string &s);
std::string StripQuotes(const std::string &s);
std::string StripNewline(const std::string &s);
// Thousand separator. Turns 12345678 into 12,345,678
std::string ThS(int a, bool b = true, int Spaces = 0);
std::wstring StringFromIntW(int value);
std::wstring StringFromBoolW(bool value);
// Thousand separator. Turns 12345678 into 12,345,678
template <typename I>
std::string ThousandSeparate(I value, int spaces = 0)
{
std::ostringstream oss;
oss.imbue(std::locale(""));
oss << std::setw(spaces) << value;
return oss.str();
}
std::string StringFromInt(int value);
std::string StringFromBool(bool value);
bool TryParseInt(const wchar_t* str, int* outVal);
bool TryParseBool(const wchar_t* str, bool* output);
bool TryParseUInt(const std::wstring& str, u32* output);
bool TryParseInt(const char* str, int* outVal);
bool TryParseBool(const char* str, bool* output);
bool TryParseUInt(const std::string& str, u32* output);
bool TryParseFloat(const char* str, float *output);
bool TryParseDouble(const char* str, double *output);
bool TryParse(const std::string &str, bool *output);
bool TryParse(const std::string &str, u32 *output);
template <typename N>
bool TryParse(const std::string &str, N *const output)
{
std::istringstream iss(str);
N tmp = 0;
if (iss >> tmp)
{
*output = tmp;
return true;
}
else
return false;
}
// TODO: kill this
bool AsciiToHex(const char* _szValue, u32& result);
u32 Ascii2Hex(std::string _Text);
std::string Hex2Ascii(u32 _Text);
std::string TabsToSpaces(int tab_size, const std::string &in);
void SplitString(const std::string& str, const std::string& delim, std::vector<std::string>& output);
int ChooseStringFrom(const char* str, const char* * items);
void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
// "C:\Windows\winhelp.exe" to "C:\Windows\", "winhelp", "exe"
// "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension);
// "C:\Windows\winhelp.exe" to "winhelp.exe"
std::string PathToFilename(std::string Path);
// "C:/Windows/winhelp.exe" to "winhelp.exe"
std::string PathToFilename(const std::string &Path);
void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename);
void NormalizeDirSep(std::string* str);
#endif // _STRINGUTIL_H_
+4 -4
View File
@@ -184,14 +184,14 @@ void LoadCodes(IniFile &ini, bool forceLoad)
continue;
}
SplitString(line, " ", pieces);
SplitString(line, ' ', pieces);
// Check if the AR code is decrypted
if (pieces.size() == 2 && pieces[0].size() == 8 && pieces[1].size() == 8)
{
AREntry op;
bool success_addr = TryParseUInt(std::string("0x") + pieces[0], &op.cmd_addr);
bool success_val = TryParseUInt(std::string("0x") + pieces[1], &op.value);
bool success_addr = TryParse(std::string("0x") + pieces[0], &op.cmd_addr);
bool success_val = TryParse(std::string("0x") + pieces[1], &op.value);
if (!(success_addr | success_val)) {
PanicAlert("Action Replay Error: invalid AR code line: %s", line.c_str());
if (!success_addr) PanicAlert("The address is invalid");
@@ -202,7 +202,7 @@ void LoadCodes(IniFile &ini, bool forceLoad)
}
else
{
SplitString(line, "-", pieces);
SplitString(line, '-', pieces);
if (pieces.size() == 3 && pieces[0].size() == 4 && pieces[1].size() == 4 && pieces[2].size() == 5)
{
// Encrypted AR code
+1 -1
View File
@@ -213,7 +213,7 @@ void FrameStepOnOff()
void WriteStatus()
{
std::string TmpStr = "Time: " + ReRecTimer.GetTimeElapsedFormatted();
TmpStr += StringFromFormat(" Frame: %s", ThS(g_FrameCounter).c_str());
TmpStr += StringFromFormat(" Frame: %s", ThousandSeparate(g_FrameCounter).c_str());
// The FPS is the total average since the game was booted
TmpStr += StringFromFormat(" FPS: %i", (g_FrameCounter * 1000) / ReRecTimer.GetTimeElapsed());
TmpStr += StringFromFormat(" FrameStep: %s", g_FrameStep ? "On" : "Off");
@@ -197,7 +197,7 @@ u32 CWII_IPC_HLE_Device_di::ExecuteCommand(u32 _BufferIn, u32 _BufferInSize, u32
VolumeHandler::RAWReadToPtr(Memory::GetPointer(_BufferOut), 0, _BufferOutSize);
INFO_LOG(WII_IPC_DVD, "DVDLowReadDiskID %s",
ArrayToString(Memory::GetPointer(_BufferOut), _BufferOutSize, 0, _BufferOutSize).c_str());
ArrayToString(Memory::GetPointer(_BufferOut), _BufferOutSize, _BufferOutSize).c_str());
}
break;
@@ -895,7 +895,7 @@ namespace Core
INFO_LOG(WIIMOTE, "====================");
INFO_LOG(WIIMOTE, "Callback_WiimoteInterruptChannel: (Wiimote: #%i)", _number);
DEBUG_LOG(WIIMOTE, " Data: %s", ArrayToString(pData, _Size, 0, 50).c_str());
DEBUG_LOG(WIIMOTE, " Data: %s", ArrayToString(pData, _Size, 50).c_str());
DEBUG_LOG(WIIMOTE, " Channel: %x", _channelID);
s_Usb->m_WiiMotes[_number].ReceiveL2capData(_channelID, _pData, _Size);
+10 -8
View File
@@ -30,6 +30,8 @@
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include "StringUtil.h"
#include "PatchEngine.h"
#include "HW/Memmap.h"
@@ -48,7 +50,6 @@ const char *PatchTypeStrings[] =
"byte",
"word",
"dword",
0
};
std::vector<Patch> onFrame;
@@ -88,14 +89,15 @@ void LoadPatchSection(const char *section, std::vector<Patch> &patches, IniFile
line.at(loc) = ':';
std::vector<std::string> items;
SplitString(line, ":", items);
SplitString(line, ':', items);
if (items.size() >= 3) {
PatchEntry pE;
bool success = true;
success = success && TryParseUInt(items[0], &pE.address);
success = success && TryParseUInt(items[2], &pE.value);
pE.type = (PatchType)ChooseStringFrom(items[1].c_str(), PatchTypeStrings);
success = success && (pE.type != (PatchType)-1);
success &= TryParse(items[0], &pE.address);
success &= TryParse(items[2], &pE.value);
pE.type = PatchType(std::find(PatchTypeStrings, PatchTypeStrings + 3, items[1]) - PatchTypeStrings);
success &= (pE.type != (PatchType)3);
if (success)
currentPatch.entries.push_back(pE);
}
@@ -131,8 +133,8 @@ static void LoadSpeedhacks(const char *section, std::map<u32, int> &hacks, IniFi
u32 address;
u32 cycles;
bool success = true;
success = success && TryParseUInt(std::string(key.c_str()), &address);
success = success && TryParseUInt(value, &cycles);
success &= TryParse(key, &address);
success &= TryParse(value, &cycles);
if (success) {
speedHacks[address] = (int)cycles;
}
@@ -195,7 +195,7 @@ void CBreakPointWindow::OnAddMemoryCheckMany()
{
std::string line = StripSpaces(*iter);
std::vector<std::string> pieces;
SplitString(line, " ", pieces); // split string
SplitString(line, ' ', pieces); // split string
TMemCheck MemCheck;
u32 sAddress = 0;
+2 -2
View File
@@ -157,13 +157,13 @@ void CMemoryWindow::SetMemoryValue(wxCommandEvent& event)
u32 addr;
u32 val;
if (!TryParseUInt(std::string("0x") + str_addr, &addr))
if (!TryParse(std::string("0x") + str_addr, &addr))
{
PanicAlert("Invalid Address: %s", str_addr.c_str());
return;
}
if (!TryParseUInt(std::string("0x") + str_val, &val))
if (!TryParse(std::string("0x") + str_val, &val))
{
PanicAlert("Invalid Value: %s", str_val.c_str());
return;
+1 -1
View File
@@ -90,7 +90,7 @@ static void SetSpecialRegValue(int reg, u32 value) {
void CRegTable::SetValue(int row, int col, const wxString& strNewVal)
{
u32 newVal = 0;
if (TryParseUInt(std::string(strNewVal.mb_str()), &newVal))
if (TryParse(std::string(strNewVal.mb_str()), &newVal))
{
if (row < 32) {
if (col == 1)
+1 -1
View File
@@ -103,7 +103,7 @@ void CheckFile(std::string File, u64 Size)
if (CurrentFile == File) return;
if (Size > 0) Size = (Size / 1000);
std::string Str = StringFromFormat("%s kB %s", ThS(Size, true, 7).c_str(), File.c_str());
std::string Str = StringFromFormat("%s kB %s", ThousandSeparate(Size, 7).c_str(), File.c_str());
if (ShowSound(File))
{
NOTICE_LOG(FILEMON, Str.c_str());
+2 -2
View File
@@ -112,7 +112,7 @@ void CARCodeAddEdit::SaveCheatData(wxCommandEvent& WXUNUSED (event))
std::vector<std::string> pieces;
std::string line_str = cheatValues.substr(line, cheatValues.find('\n', line) - line);
SplitString(line_str, " ", pieces);
SplitString(line_str, ' ', pieces);
if (pieces.size() == 2 && pieces[0].size() == 8 && pieces[1].size() == 8)
{
@@ -123,7 +123,7 @@ void CARCodeAddEdit::SaveCheatData(wxCommandEvent& WXUNUSED (event))
}
else
{
SplitString(line_str, "-", pieces);
SplitString(line_str, '-', pieces);
if (pieces.size() == 3 && pieces[0].size() == 4 && pieces[1].size() == 4 && pieces[2].size() == 5)
{
+5 -5
View File
@@ -811,7 +811,7 @@ void CFrame::LoadIniPerspectives()
ini.Load(File::GetUserPath(F_DEBUGGERCONFIG_IDX));
ini.Get("Perspectives", "Perspectives", &_Perspectives, "Perspective 1");
ini.Get("Perspectives", "Active", &ActivePerspective, 0);
SplitString(_Perspectives, ",", VPerspectives);
SplitString(_Perspectives, ',', VPerspectives);
for (u32 i = 0; i < VPerspectives.size(); i++)
{
@@ -833,17 +833,17 @@ void CFrame::LoadIniPerspectives()
Tmp.Perspective = wxString::FromAscii(_Perspective.c_str());
SplitString(_Width, ",", _SWidth);
SplitString(_Height, ",", _SHeight);
SplitString(_Width, ',', _SWidth);
SplitString(_Height, ',', _SHeight);
for (u32 j = 0; j < _SWidth.size(); j++)
{
int _Tmp;
if (TryParseInt(_SWidth[j].c_str(), &_Tmp)) Tmp.Width.push_back(_Tmp);
if (TryParse(_SWidth[j].c_str(), &_Tmp)) Tmp.Width.push_back(_Tmp);
}
for (u32 j = 0; j < _SHeight.size(); j++)
{
int _Tmp;
if (TryParseInt(_SHeight[j].c_str(), &_Tmp)) Tmp.Height.push_back(_Tmp);
if (TryParse(_SHeight[j].c_str(), &_Tmp)) Tmp.Height.push_back(_Tmp);
}
Perspectives.push_back(Tmp);
}
@@ -542,10 +542,8 @@ void CGameListCtrl::ScanForISOs()
if (FST_Temp.children.at(j).isDirectory)
{
bool duplicate = false;
NormalizeDirSep(&(FST_Temp.children.at(j).physicalName));
for (u32 k = 0; k < Directories.size(); k++)
{
NormalizeDirSep(&Directories.at(k));
if (strcmp(Directories.at(k).c_str(),
FST_Temp.children.at(j).physicalName.c_str()) == 0)
{
+1 -3
View File
@@ -1138,9 +1138,7 @@ void CISOProperties::PatchList_Save()
for (std::vector<PatchEngine::PatchEntry>::const_iterator iter2 = onFrame_it->entries.begin(); iter2 != onFrame_it->entries.end(); ++iter2)
{
std::string temp;
ToStringFromFormat(&temp, "0x%08X:%s:0x%08X", iter2->address, PatchEngine::PatchTypeStrings[iter2->type], iter2->value);
std::string temp = StringFromFormat("0x%08X:%s:0x%08X", iter2->address, PatchEngine::PatchTypeStrings[iter2->type], iter2->value);
lines.push_back(temp);
}
++index;
@@ -49,10 +49,8 @@ void Init(const char *gameCode)
if (FST_Temp.children.at(j).isDirectory)
{
bool duplicate = false;
NormalizeDirSep(&(FST_Temp.children.at(j).physicalName));
for (u32 k = 0; k < Directories.size(); k++)
{
NormalizeDirSep(&Directories.at(k));
if (strcmp(Directories.at(k).c_str(), FST_Temp.children.at(j).physicalName.c_str()) == 0)
{
duplicate = true;
+32
View File
@@ -93,11 +93,43 @@ void MathTests()
void StringTests()
{
EXPECT_EQ(StripSpaces(" abc "), "abc");
EXPECT_EQ(StripNewline(" abc \n"), " abc ");
EXPECT_EQ(StripNewline(" abc \n "), " abc \n ");
EXPECT_EQ(StripQuotes("\"abc\""), "abc");
EXPECT_EQ(StripQuotes("\"abc\" "), "\"abc\" ");
EXPECT_EQ(TabsToSpaces(4, "a\tb"), "a b");
EXPECT_EQ(ThousandSeparate(1234567, 15), " 1,234,567");
int i = 7;
EXPECT_EQ(false, TryParse("FF", &i));
EXPECT_EQ(7, i);
EXPECT_EQ(true, TryParse("22", &i));
EXPECT_EQ(22, i);
std::vector<std::string> strs;
SplitString("blah,foo,bar", ',', strs);
EXPECT_EQ(3, strs.size());
EXPECT_EQ("bar", strs[2]);
std::string path, fname, ext;
SplitPath("C:/some/path/test.jpg", &path, &fname, &ext);
EXPECT_EQ("C:/some/path/", path);
EXPECT_EQ("test", fname);
EXPECT_EQ(".jpg", ext);
SplitPath("C:/so.me/path/", &path, &fname, &ext);
EXPECT_EQ("C:/so.me/path/", path);
EXPECT_EQ("", fname);
EXPECT_EQ("", ext);
SplitPath("test.file.jpg", &path, &fname, &ext);
EXPECT_EQ("", path);
EXPECT_EQ("test.file", fname);
EXPECT_EQ(".jpg", ext);
}