Permalink
Browse files

Add some helper methods and rewrite some of the string manipulation code

  • Loading branch information...
mniip committed May 1, 2018
1 parent 833383b commit a121c62c3b7fb871be93f36b2dfc2d0d7256acaa
@@ -1166,26 +1166,22 @@ int main(int argc, char * argv[])
ByteString ptsaveArg = arguments["ptsave"];
try
{
if (ptsaveArg.find("ptsave:"))
throw std::runtime_error("Invalid save link");
ByteString saveIdPart = "";
int saveId;
size_t hashPos = ptsaveArg.find('#');
if (hashPos != ByteString::npos)
ByteString saveIdPart;
if (ByteString::Split split = arguments["ptsave"].SplitBy(':'))
{
saveIdPart = ptsaveArg.substr(7, hashPos-7);
if (split.Before() != "ptsave")
throw std::runtime_error("Not a ptsave link");
saveIdPart = split.After().SplitBy('#').Before();
}
else
{
saveIdPart = ptsaveArg.substr(7);
}
if (!saveIdPart.length())
throw std::runtime_error("Invalid save link");
if (!saveIdPart.size())
throw std::runtime_error("No Save ID");
#ifdef DEBUG
std::cout << "Got Ptsave: id: " << saveIdPart << std::endl;
std::cout << "Got Ptsave: id: " << saveIdPart << std::endl;
#endif
saveId = format::ByteStringToNumber<int>(saveIdPart);
int saveId = format::ByteStringToNumber<int>(saveIdPart);
if (!saveId)
throw std::runtime_error("Invalid Save ID");
@@ -1227,7 +1223,7 @@ int main(int argc, char * argv[])
}
catch(exception& e)
{
BlueScreen(e.what());
BlueScreen(ByteString(e.what()).FromUtf8());
}
#endif
@@ -349,17 +349,9 @@ bool Client::DoInstallation()
#include "icondoc.h"
int success = 1;
ByteString filename = Platform::ExecutableName(), pathname = filename.substr(0, filename.rfind('/'));
for (size_t i = 0; i < filename.size(); i++)
{
if (filename[i] == '\'')
{
filename.insert(i, "'\\'");
i += 3;
}
}
filename.insert(filename.size(), "'");
filename.insert(0, "'");
ByteString filename = Platform::ExecutableName(), pathname = filename.SplitFromEndBy('/').Before();
filename.Substitute('\'', "'\\''");
filename = '\'' + filename + '\'';
FILE *f;
const char *mimedata =
@@ -539,18 +531,17 @@ std::vector<ByteString> Client::DirectorySearch(ByteString directory, ByteString
bool extensionMatch = !extensions.size();
for(std::vector<ByteString>::iterator extIter = extensions.begin(), extEnd = extensions.end(); extIter != extEnd; ++extIter)
{
size_t filenameLength = filename.length()-(*extIter).length();
if(filename.find(*extIter, filenameLength) == filenameLength)
if(filename.EndsWith(*extIter))
{
extensionMatch = true;
tempfilename = filename.substr(0, filenameLength);
tempfilename = filename.SubstrFromEnd(0, (*extIter).size());
break;
}
}
for (ByteString::iterator iter = tempfilename.begin(); iter != tempfilename.end(); ++iter)
*iter = toupper(*iter);
bool searchMatch = !search.size();
if(search.size() && tempfilename.find(search)!=ByteString::npos)
if(search.size() && tempfilename.Contains(search))
searchMatch = true;
if(searchMatch && extensionMatch)
@@ -1202,10 +1193,9 @@ void Client::RescanStamps()
stampIDs.clear();
while ((entry = readdir(directory)))
{
if(strncmp(entry->d_name, "..", 3) && strncmp(entry->d_name, ".", 2) && strstr(entry->d_name, ".stm") && strlen(entry->d_name) == 14)
{
stampIDs.push_front(ByteString(entry->d_name).substr(0, 10));
}
ByteString name = entry->d_name;
if(name != ".." && name != "." && name.EndsWith(".stm") && name.size() == 14)
stampIDs.push_front(name.Substr(0, 10));
}
closedir(directory);
updateStamps();
@@ -2084,11 +2074,10 @@ Json::Value Client::GetPref(Json::Value root, ByteString prop, Json::Value defau
{
try
{
size_t dot = prop.find('.');
if (dot == prop.npos)
return root.get(prop, defaultValue);
if(ByteString::Split split = prop.SplitBy('.'))
return GetPref(root[split.Before()], split.After(), defaultValue);
else
return GetPref(root[prop.substr(0, dot)], prop.substr(dot+1), defaultValue);
return root.get(prop, defaultValue);
}
catch (std::exception & e)
{
@@ -2277,29 +2266,25 @@ std::vector<bool> Client::GetPrefBoolArray(ByteString prop)
// and return it to SetPref to do the actual setting
Json::Value Client::SetPrefHelper(Json::Value root, ByteString prop, Json::Value value)
{
size_t dot = prop.find(".");
if (dot == prop.npos)
root[prop] = value;
else
if(ByteString::Split split = prop.SplitBy('.'))
{
Json::Value toSet = GetPref(root, prop.substr(0, dot));
toSet = SetPrefHelper(toSet, prop.substr(dot+1), value);
root[prop.substr(0, dot)] = toSet;
Json::Value toSet = GetPref(root, split.Before());
toSet = SetPrefHelper(toSet, split.After(), value);
root[split.Before()] = toSet;
}
else
root[prop] = value;
return root;
}
void Client::SetPref(ByteString prop, Json::Value value)
{
try
{
size_t dot = prop.find(".");
if (dot == prop.npos)
preferences[prop] = value;
if(ByteString::Split split = prop.SplitBy('.'))
preferences[split.Before()] = SetPrefHelper(preferences[split.Before()], split.After(), value);
else
{
preferences[prop.substr(0, dot)] = SetPrefHelper(preferences[prop.substr(0, dot)], prop.substr(dot+1), value);
}
preferences[prop] = value;
}
catch (std::exception & e)
{
@@ -678,7 +678,7 @@ void GameSave::readOPS(char * data, int dataLength)
{
if (!strcmp(bson_iterator_key(&signiter), "text") && bson_iterator_type(&signiter) == BSON_STRING)
{
tempSign.text = format::CleanString(ByteString(bson_iterator_string(&signiter)).FromUtf8(), true, true, true).substr(0, 45);
tempSign.text = format::CleanString(ByteString(bson_iterator_string(&signiter)).FromUtf8(), true, true, true).Substr(0, 45);
}
else if (!strcmp(bson_iterator_key(&signiter), "justification") && bson_iterator_type(&signiter) == BSON_INT)
{
@@ -1407,11 +1407,7 @@ void GameSave::readPSv(char * saveDataChar, int dataLength)
int bzStatus = 0;
if ((bzStatus = BZ2_bzBuffToBuffDecompress((char *)data, (unsigned *)&size, (char *)(saveData+12), dataLength-12, 0, 0)))
{
String::Stream bzStatusStr;
bzStatusStr << bzStatus;
throw ParseException(ParseException::Corrupt, "Cannot decompress: " + bzStatusStr.str());
}
throw ParseException(ParseException::Corrupt, "Cannot decompress: " + format::NumberToString(bzStatus));
dataLength = size;
#ifdef DEBUG
@@ -1952,7 +1948,7 @@ void GameSave::readPSv(char * saveDataChar, int dataLength)
x = 254;
memcpy(tempSignText, data+p, x);
tempSignText[x] = 0;
tempSign.text = format::CleanString(tempSignText, true, true, true).substr(0, 45);
tempSign.text = format::CleanString(tempSignText, true, true, true).Substr(0, 45);
tempSigns.push_back(tempSign);
p += x;
}
@@ -947,7 +947,7 @@ ByteString FindBoundary(std::map<ByteString, ByteString> parts, ByteString bound
{
// loop through every character in each part and search for the substring, adding 1 to map for every character found (character after the substring)
for (ssize_t j = 0; j < (ssize_t)((*iter).second.length()-blen); j++)
if (!blen || (*iter).second.substr(j, blen) == boundary)
if (!blen || (*iter).second.Substr(j, blen) == boundary)
{
unsigned char ch = (*iter).second[j+blen];
if (ch >= '0' && ch <= '9')
@@ -999,12 +999,11 @@ ByteString GetMultipartMessage(std::map<ByteString, ByteString> parts, ByteStrin
data << "Content-transfer-encoding: binary" << "\r\n";
// colon p
size_t colonP = name.find(':');
if (colonP != name.npos)
if (ByteString::Split split = name.SplitBy(':'))
{
// used to upload files (save data)
data << "content-disposition: form-data; name=\"" << name.substr(0, colonP) << "\"";
data << "filename=\"" << name.substr(colonP+1) << "\"";
data << "content-disposition: form-data; name=\"" << split.Before() << "\"";
data << "filename=\"" << split.After() << "\"";
}
else
{
Oops, something went wrong.

0 comments on commit a121c62

Please sign in to comment.